void sol(){ if(s2<s1){ vi t=a;a=b;b=t; ll x=s1;s1=s2;s2=x; t=ida;ida=idb;idb=t; fliped=!fliped; } if(a.empty() || b.empty())return ; if(abs(s1-s2)==0){return ;} ll diff = abs(s2 - s1); ll Y = diff; int x,y; x=y=0; // printf("diff = %lld\n",diff); for (int i = 0; i < a.size(); ++i) { vi::iterator it = lower_bound(b.begin(), b.end(), diff/2 + a[i]); int j = it-b.begin(); if(j && j == b.size())j--; // printf("i,j=%d,%d\n",i,j); ll ns1 = s1 - a[i] + b[j]; ll ns2 = s2 - b[j] + a[i]; // printf("debug a[%d] = %d b[%d] = %d\n",i,a[i],j,b[j]); // printf("debug ns1 = %lld ns2 = %lld\n",ns1,ns2); if(abs(ns1 - ns2) < Y){ x = i; y = j; // printf("debug %d %d\n",x,y); Y = abs(ns1 - ns2); } } if(Y<diff){ // printf("swaping a[%d] b[%d] diff = %lld\n", x,y, Y); if(fliped) ans.push_back(ii(idb[y],ida[x])); else ans.push_back(ii(ida[x],idb[y])); s1 = s1 - a[x] + b[y]; s2 = s2 + a[x] - b[y]; } // printf("x,y = %d %d\n",x,y); a.erase(move(a,x)); ida.erase(move(ida,x)); b.erase(move(b,y)); idb.erase(move(idb,y)); }
void convex_hull(vector<Point> & a) { if (a.size() == 1) return; sort(a.begin(), a.end(), &cmp); Point p1 = a[0], p2 = a.back(); vector<Point> up, down; up1.clear(); down1.clear(); up.push_back(p1); up1.pb(0); down.push_back(p1); down1.pb(0); for (size_t i = 1; i < a.size(); ++i) { if (i == a.size() - 1 || ccw(p1, a[i], p2)) { while (up.size() >= 2 && !ccw(up[up.size() - 2], up[up.size() - 1], a[i])) up.pop_back(),up1.pop_back(); up.push_back(a[i]),up1.push_back(i); } if (i == a.size() - 1 || cw(p1, a[i], p2)) { while (down.size() >= 2 && !cw(down[down.size() - 2], down[down.size() - 1], a[i])) down.pop_back(),down1.pop_back(); down.push_back(a[i]),down1.push_back(i); } } if (up[0].x == up.back().x && up[0].y == up.back().y) up.erase(up.end() - 1),up1.erase(up1.end()-1); a.clear();a1.clear(); for (size_t i = 0; i < up.size(); ++i) a.push_back(up[i]),a1.push_back(up1[i]); for (size_t i = down.size() - 2; i > 0; --i) a.push_back(down[i]),a1.push_back(down1[i]); }
int main() { FILE *in = fopen (PROBLEM_NAME".in","r"); FILE *out = fopen (PROBLEM_NAME".out","w"); int k; fscanf(in, "%d %d ", &n, &k); for (int i = 0; i < n; ++i) { used.push_back(false); tin.push_back(0); fup.push_back(0); vi l; g.push_back(l); } for (int i = 0; i < k; ++i) { int x, y; fscanf(in, "%d %d ", &x, &y); g[x-1].push_back(y-1); g[y-1].push_back(x-1); } timer = 0; dfs(0); sort(ans.begin(), ans.end()); ans.erase( unique( ans.begin(), ans.end() ), ans.end() ); fprintf(out, "%d\n", (int)ans.size()); for (size_t i = 0; i < ans.size(); ++i) { fprintf(out, "%d\n", ans[i]+1); } fclose(in); fclose(out); return 0; }
void hapusRadius(vi & Lingkaran, int inp){ vi::const_iterator a = Lingkaran.begin(); for (vi::const_iterator i = Lingkaran.begin(); i<Lingkaran.end();++i){ if (*i == inp){ Lingkaran.erase(Lingkaran.begin()+(i-a)); i=Lingkaran.end(); } } }
bool eat(vi &initial, int from, int limit_left, int goal, bool reversed, bool variation){ int size = initial.size(); int new_eaten, eaten_r, eaten_l; bool cond_left, cond_right; cond_left = (from-1 > limit_left) && (initial[from] > initial[from-1]); cond_right = (from+1 < size) && (initial[from] > initial[from+1]); // There could be a better in front if(variation && (cond_left || cond_left) && from + 1 < size && initial[from+1] > initial[from]) from++; cond_left = (from-1 > limit_left) && (initial[from] > initial[from-1]); cond_right = (from+1 < size) && (initial[from] > initial[from+1]); if(cond_left){ new_eaten = initial[from-1] + initial[from]; initial.erase(initial.begin()+(from-1)); initial[from-1] = new_eaten; if(reversed){ moves.push_back(si("R",size-from)); }else{ moves.push_back(si("L",from+1)); } return true; } if(cond_right){ new_eaten = initial[from+1] + initial[from]; initial.erase(initial.begin()+(from+1)); initial[from] = new_eaten; if(reversed){ moves.push_back(si("L",size-from)); }else{ moves.push_back(si("R",from+1)); } return true; } return false; }
int call(int time,int mx) { // if(dp[time][mx]!=-1) return dp[time][mx]; if(time>maxt) return INF; sort(ALLR(v)); if(v[0]<=0) {debug(time);return time;} int p1=INF,p2=INF; for(int i=0;i<v.size();i++) { v[i]--; } p1=call(time+1,v[0]-1); for(int i=0;i<v.size();i++) { v[i]++; } vi temp; temp=v; int i=v[0]; int cnt=0; if(i%2==0) { int vag=i/2; while(v[0]==i) { v.erase(v.begin()); v.PB(vag); v.PB(vag); time++; cnt++; } } else if(i%2==1) { int vag=i/2; while(v[0]==i) { v.erase(v.begin()); v.PB(vag); v.PB(vag+1); time++; cnt++; } } sort(ALLR(v)); p2=call(time,v[0]); v=vi(); v=temp; // vi :: iterator it; // if(i%2==0) // { // int vag=i/2; // while(cnt--) // { // it=find(ALL(v),vag); // v.erase(it); // it=find(ALL(v),vag); // v.erase(it); // v.PB(i); // } // } return (min(p1,p2)); }