제출 #1369342

#제출 시각아이디문제언어결과실행 시간메모리
1369342sudama_beraPalembang Bridges (APIO15_bridge)C++20
22 / 100
29 ms10856 KiB
#include <bits/stdc++.h>
using namespace std;
//policy based data structure
// #include<bits/extc++.h>
// using namespace __gnu_pbds;
// using pii=pair<int,int>;
// typedef tree<
//  int,
//  null_type,
//  //less<int>,
//  greater<int>,
//  rb_tree_tag,
//  tree_order_statistics_node_update
// >ordered_set;
// os.find_by_order(k); iterator kth lowest element
// os.order_of_key(x);  number of elements strictly less than x


// some bits operations
//x&(x-1) unsets the rightmost set bit
//x&(-x) isloates the rightmost set bit
//x-(x&-(-x)) removing the last set bit one by one 
//x-1 unsets the rightmost set bit and sets all the trailing zeros
// x=100100110000;
// x-1==100100101111
// x|(x-1) all the trailing zeros are set
// sum(a,b)=(a&b)+(a|b);
// sum(a,b)=(a^b)+2*(a&b);
// a^b is addition without the carry and a&b is the carry hence multiply by 2 to shift

// product of factors = n^(x/2) where x=number of factors



//ax+by=g
//ax+by=c

// let Xg and Yg be value got from ax+by=g using ext_gcd
// solution of ax+by=c [X0,Y0]=[Xg*c/g,Yg*c/g]
// a(x+b/g)+b(y-a/g)=c;
// X1=X0+k*(b/g) Y1=Y0-k*(a/g)

int gcd_ext(int a,int b,int&x,int&y){
   //base case
    if(b==0){
        x=1,y=0;
        return a;
    }
    int x1,y1;
    int g=gcd_ext(b,a%b,x1,y1);
    x=y1;
    y=x1-(a/b)*y1;
    return g;

}
//chinese reminder theroem
 // x==(a1%m1)
 // x==(a2%m2)
 // ...
//m1,m2,m3... are pairwise coprime
// let m=m1*m2*m3...
// then there exist exactly one x in [0,m-1] that satisfies the congrunecy
// x= a1*(m/m1)*inv(m/m1)%m1+a2*(m/m2)*inv(m/m2)%m2....

#define f first
#define s second
using ll=long long;
using ld=long double;
const long long inf=1e18+1;


long long dist(int xi,int yi,int xj,int yj){
    return (xi-xj)*1ll*(xi-xj)+(yi-yj)*1ll*(yi-yj);
}

class dsu{
public:
    vector<int>par;
    vector<int>size;
    vector<list<int>>comp;
public:
    dsu(int n){
        par.resize(n);
        size.resize(n);
        comp.resize(n);
        for(int i=0;i<n;i++){
            par[i]=i;
            size[i]=1;
            comp[i].push_back(i);
        }
    }
  int find(int u){
    if(u==par[u]){return u;}
    return par[u]=find(par[u]);
  }
  void unite(int u,int v){
    
     u=find(u);
    v=find(v);
    if(u==v){return;}
   //assumig par_u is smaller
    if(size[u]>size[v]){
       swap(u,v);
    }
    size[v]+=size[u];
    par[u]=v;
    return;
     
     

  }
  bool connected(int u,int v){
    return (find(u)==find(v));
  }

}; 
class fenwick{
    public: 
        vector<long long>bit;
        int n;
    fenwick(int n,vector<int>&a){
        this->n=n;
        bit.assign(n+1,0LL);
        for(int i=0;i<n;i++){
            bit[i+1]=a[i];
        }
        for(int i=1;i<=n;i++){
            int r=i+(i&(-i));
             if(r<=n)bit[r]+=bit[i];
        }
    }
    void update(int r,int val){
        int cur_r=r+1;
        while(cur_r<=n){
            bit[cur_r]+=val;
            cur_r=cur_r+(cur_r&(-cur_r));
        }
        return ;
    }
    long long sum(int r){
        int cur_r=r+1;
        long long ret_sum=0;
        while(cur_r>0){
            ret_sum+=bit[cur_r];
            cur_r=cur_r-(cur_r&(-cur_r));
        }
        return ret_sum;
    }
    long long sum_qry(int l,int r){
        return sum(r)-sum(l-1);
    }
};

class sgt{
public:
    vector<long long>t;
    sgt(int n){
        t.assign(4*n, 0);
    }
    void build(int tl,int tr,int v,vector<long long>&a){
        if(tl==tr){
            t[v]=a[tl];
            return;
        }
        int mid=(tl+tr)/2;
        build(tl,mid,v*2+1,a);
        build(mid+1,tr,v*2+2,a);
        t[v] =t[v*2+1]+t[v*2+2];
    }
    void update(int v,long long val,int pos,int tl,int tr){
        if(tl==tr){
            t[v]=val;
            return;
        }
        int mid = (tl + tr)/2;
        if(pos<=mid) update(v*2+1,val,pos,tl,mid);
        else update(v*2+2,val,pos,mid+1,tr);
        t[v]=t[v*2+1]+t[v*2+2];
    }
    long long query(int l,int r,int tl,int tr,int v){
        if(l>r) return 0;
        if(l==tl&&r==tr)return t[v];
        int mid=(tl+tr)/2;
        return query(l,min(r,mid),tl,mid,v*2+1)
             + query(max(l,mid+1),r,mid+1,tr,v*2+2);
    }
};

// phi[i]=i*multiply(1-1/prime_factors)
//phi(i*j)=phi(i)*phi(j)*g/phi(g)
//(a^(n))%m==== (a^(n%phi(m)))%m
const int mx=1e6;
vector<int>phi(mx+1);
void cal_phi(){
    for(int i=0;i<=mx;i++){
        phi[i]=i;
    }
    for(int i=2;i<=mx;i++){
        if(phi[i]==i){
            for(int j=i;j<=mx;j=j+i){
                phi[j]=(phi[j])-(phi[j]/i);
            }
        }
    }
}
vector<bool>prime(mx,true),lpf(mx);
void seiv_algo(){
    prime[0]=prime[1]=false;
    for(int i=2;i<mx;i++){
        if(prime[i]){
            lpf[i]=i;
            for(int j=2*i;j<mx;j=j+i){
                prime[j]=false;
                if(lpf[j]==0)lpf[j]=i;
            }
        }
    }
}

// number of dearrangement== n!(sum(-1^k/k!)) i.e n!(1/0!-1/1!+1/2!-1/3!...)
// let 1st person get ith hat
// then case1: i also get the 1st hat .... i can have n-1 values
//              then now there are n-2 hats to solve
//              giving as (n-1)*(f(n-2))
//     case2: i doesn't get the 1st hat and i can take n-1 values, so now we need to get f(n-1) values
//               giving us n-1*f(n-1);

// hence f(n)=(n-1)*(f(n-1)+f(n-2)); dearrangements

//STARS AND BAR (k stars and n bars) ((k+n-1)C(n-1))  x1+x2+x3...xn=k Xi>=0; or N boxes and K balls, can put multiple balls in a box
// x1+p+x2+p+x3+p....=k
//x1+x2+...=k-p-p-p...
//x1+x2+x3..=k'
// k'+(n-1)C(n-1)

//pos[0]=true;
//for(k=0;k<n;k++){for(x=W;x>=0;x--;){if(pos[x]){pos[x+wt[k]]=true;}}}


//NESW

ll dist(pair<int,int>a,pair<int,int>b){
    return (a.first-b.first)*1ll*(a.first-b.first)+(a.second-b.second)*1ll*(a.second-b.second);
}

// min number of Increaing/non intersecting  Subsequnces==max len of non increaing subsequnce
//dilworths theorem

int cc=0;
using ull=unsigned long long int;

const long long M=998244353;
long long exp(long long x,long long y){
    long long res=1;
    while(y){
      if(y&1){
        res=(res*x)%M;
      }
      y=y>>1;
      x=(x*x)%M;
    }
    return res;
}
ll mod_inv(ll x){
    return( M+exp(x,M-2))%M;
}

int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};
string dir="RDLU";

using ll=long long;
void solve() {
   int k,n;cin>>k>>n;
   vector<vector<int>>v(n);
   // 0/1 , start,end
   //0 for self zone, 1 for different zone
   vector<int>vec;
   long long ans=0;
   for(int i=0;i<n;i++){
    char a,b;int x,y;
     cin>>a>>x>>b>>y;
     if(a==b){
        ans+=abs(x-y);
     }else{
        vec.push_back(x);
        vec.push_back(y);
        v[i]={1,x,y};
     }
   }
  sort(vec.begin(),vec.end());
  if(k==1){
      int x=vec[vec.size()/2];
      for(auto&y:vec){
            ans=ans+(abs(x-y));
       
      }
  }else{
    ans=1;
  }
  ans+=(vec.size())/2;
  cout<<ans;
}


//dp=move(next_dp) 
int main(){
// freopen("art2.in","r",stdin);
// freopen("art2.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t=1;
  // cin>>t;
    while((t--)){       
       solve();
    }
}
/*
1 5 2 3 9 6 4
6 1
1 6
1 3 2 4
1 4 3 2 5 6 7


*/

// for(int mask=0;mask<(1<<n);mask++){
//     for(int submask=mask;submask!=0;submask=(submask-1)&(mask)){
//         int subset=(mask^(submask));
//     }
// }
// struct cmp{
//     bool operator()(cosnt pair<int,int>&a,const pair<int,int>&b)const{
//      // > for decreasing
       // < for increasing
       // incase of pq, reverse
//     }
// };
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…