void change(int x){ int t; typeof(S.begin()) it; if(col[x])er(S,mp(x,0));else S.insert(mp(x,0)); for(col[x]^=1;x;x=jump[x]){ n=bel[x]; if(jump[x]&&!S.empty()){ it=S.find(mp(jump[x],Gm(D1,sl[n],sl[n+1]-1)-sl[n]+1)); if(it!=S.end())S.erase(it); } if(!S.empty()){ it=S.lower_bound(mp(x,-INF)); t=(it!=S.end()&&it->first==x)?it->second:INF; }else t=INF; D0[pos[x]+tn-1]=t-pos[x],D1[pos[x]+tn-1]=t+pos[x]; for(t=(pos[x]+tn-1)>>1;t;t>>=1) D0[t]=max(D0[L(t)],D0[R(t)]),D1[t]=max(D1[L(t)],D1[R(t)]); if(jump[x]) S.insert(mp(jump[x],Gm(D1,sl[n],sl[n+1]-1)-sl[n]+1)); } }
pair<int, int> cover() { multiset<pair<int, int> >::iterator it = milk.begin(); int sum = 1, st = it->first, ed = 0, res1 = 0, res2 = 0; it++; for (; it != milk.end(); it++) { if (it->second == 0) { // left if (sum == 0) { res2 = max(res2, it->first - ed); st = it->first; } sum++; } else if (it->second == 1){ // right sum--; if (sum == 0) { res1 = max(res1, it->first - st); ed = it->first; } } } return MP(res1, res2); }
int main() { std::ios::sync_with_stdio(false); while(cin >> n && n) { sum = 0; bills.clear(); for(int idx=0; idx<n; ++idx) { cin >> k; for(int jdx=0; jdx<k; ++jdx) { cin >> x; bills.insert(x); } multiset<int>::iterator hit = bills.end(), lot = bills.begin(); hit--; sum += (*hit)-(*lot); bills.erase(hit); bills.erase(lot); } cout << sum <<endl; } return 0; }
int main() { ll t; get(t); while(t--) { ll n, k, sum = 0, mx = 0; get(n); get(k); v.clear(); rep(n) { ll x; get(x); v.insert(x); sum += x; mx = max(mx,x); } if( sum%k != 0 ) { printf("no\n"); continue; } else if( mx <= sum/k ){ ll val = sum/k, z; for( z = 1; z <= k; z++ ) { if( v.empty() ) { printf("no\n"); break; } flag = 0; ll r = *v.rbegin(); r = val - r; v.erase( v.find(*v.rbegin()) ); rec( v.begin(), v, 0, r ); // cout << r << "---------\n"; if( !flag ) { printf("no\n"); break; } } // cout << " END: " << z << endl; if( z == k+1 ) printf("yes\n"); } else { printf("no\n"); continue; } } }
int main() { ifstream in("planificare.in"); ofstream out("planificare.out"); in >> N >> K; for ( int i = 1; i <= N; ++i ) in >> v[i].x >> v[i].y; sort(v + 1, v + N + 1); for ( int i = 1; i <= K; ++i ) MS.insert(0); for ( int i = 1; i <= N; ++i ) { auto it = MS.lower_bound(v[i].x); if ( it != MS.end() && *it == v[i].x ) { sol++; MS.erase(it); MS.insert(v[i].y); continue; } if ( it == MS.begin() ) continue; it--; sol++; MS.erase(it); MS.insert(v[i].y); } out << sol << "\n"; return 0; }
int main() { cout.precision(32); int T; cin >> T; for(int t = 0; t < T; ++t) { char op; cin >> op; int x; cin >> x; if (op == 'a') { xs.insert(x); if (xs.size() == 1) { pmedian = xs.begin(); } else { if (x >= *pmedian && (xs.size() % 2 == 1)) { pmedian++; } if (x < *pmedian && (xs.size() % 2 == 0)) { pmedian--; } } } else { multiset<int>::iterator px = xs.find(x); if (px == xs.end()) { cout << "Wrong!" << endl; continue; } else { xs.erase(px); } if (x >= *pmedian && (xs.size() % 2 == 0)) { pmedian--; } if (x < *pmedian && (xs.size() % 2 == 1)) { pmedian++; } } if (!xs.empty()) cout << median() << endl; else cout << "Wrong!" << endl; } }
int main() { int n,k,t,i; cin>>t; while(t-- && cin>>n) { if(n<10){cout<<n<<endl;continue;} do { k=n; for(i=9;i>=2;i--)if(n%i==0){n/=i;num.insert(i);} if(n==k){cout<<"-1";n=-1;break;} }while(n>=10); if(n!=-1) for(it=num.begin();it!=num.end();it++)cout<<*it; if(n>1)cout<<n; cout<<endl; num.clear(); } return 0; }
int main() { i64 sum = 0; int n, i, j, p,m; scanf("%d", &n); for(i=0; i<n; i++) { scanf("%d", &m); for(j=0; j<m; j++) { scanf("%d", &p); S.insert(p); } fwd = S.begin(); rev = S.end(); rev--; sum += (*rev - *fwd); S.erase(fwd); S.erase(rev); } printf(FS, sum); return 0; }
int main() { #ifndef ONLINE_JUDGE freopen("528a.in", "r", stdin); freopen("528a.out", "w", stdout); #endif cin >> w >> h >> n; rx.insert(0); rx.insert(w); ry.insert(0); ry.insert(h); distx.insert(-w); disty.insert(-h); for(int i = 1 ; i <= n ; ++ i) { char op; int x; cin >> op >> x; if(op == 'V') { auto p1 = rx.lower_bound(x); auto p2 = rx.lower_bound(x); if(p1 != rx.begin()) -- p1; // cout << "x \n"; // cout << *p1 << ' ' << *p2 << '\n'; rx.insert(x); distx.erase(distx.find(-(*p2 - *p1))); distx.insert(-(x - *p1)); distx.insert(-(*p2 - x)); cout << 1LL * (*distx.begin()) * (*disty.begin()) << '\n'; } else { auto p1 = ry.lower_bound(x); auto p2 = ry.lower_bound(x); if(p1 != ry.begin()) -- p1; // cout << "y \n"; // cout << *p1 << ' ' << *p2 << '\n'; ry.insert(x); disty.erase(disty.find(-(*p2 - *p1))); disty.insert(-(x - *p1)); disty.insert(-(*p2 - x)); cout << 1LL * (*distx.begin()) * (*disty.begin()) << '\n'; } } }
void insert(vec &p){ if(p.x==0) return; set<vec>::iterator loc=s.insert(p),it1=loc,it2=loc,next; --it1; ++it2; if(area(*it1,*it2,p)>0){ s.erase(loc); return; } ans-=dist(*it1,*it2); for(next=it1;it1!=s.begin();it1=next){ next--; if(area(*next,*it1,p)>=0) break; ans-=dist(*next,*it1); s.erase(it1); } ans+=dist(*it1,p); for(next=it2,next++;next!=s.end();it2=next,next++){ if(area(*next,*it2,p)<=0) break; ans-=dist(*next,*it2); s.erase(it2); } ans+=dist(*it2,p); }
main() { int n ; scanf("%d",&n) ; for(int i=1;i<=n;i++) scanf("%d",&h[i]) ; for(int i=1;i<n;i++) { int x,y ; scanf("%d%d",&x,&y) ; v[x].push_back(y) ; v[y].push_back(x) ; } dfs0(1,-1,h[1]) ; int k ; scanf("%d",&k) ; for(int i=1,x;i<=k;i++) scanf("%d",&x) , st.insert(x) ; for(int i=k+1;i<=n;i++) st.insert(0) ; for(int i=1;i<=n;i++) cave[i]=(P){i,hm[i]} ; sort(cave+1,cave+n+1) ; LIM=-1 ; for(int i=1;i<=n;i++) { auto it=st.upper_bound(cave[i].hm) ; if(it!=st.begin()) st.erase(--it) ; else if(LIM==-1) LIM=cave[i].hm ; } if(st.empty()) {printf("0\n") ; return 0;} if(!dfs(1,-1,INF)) {printf("-1\n") ; return 0 ;} int l=0 , r=INF ; while(r-l>1) { int mid=(r+l)/2 ; if(dfs(1,-1,mid)) r=mid ; else l=mid ; } printf("%d\n",r) ; }
bool AStar::removeNodeFromSet( multiset<Node>& l,Node n){ n.operationEqual = true; //printf("Equality check before removing\n"); multiset<Node> ::iterator it; for(it = l.begin();it!=l.end();it++){ if(n.id == (*it).id){ break; } } #if DEBUG printf("Trying to remove node %lld\n",n.id); #endif if(it==l.end()){ std::cout<<"Shouldn't come here, should be called after ensuring that the element exists in the list\n"; return false; } else{ l.erase(it); return true; } }
REP(i, n) { if (a[i] >= 0) s += a[i]; else if (ps.size() < k) { s += -a[i]; ps.insert(a[i]); }else if (k && a[i] < *(--ps.end())) { int v = *(--ps.end()); ps.erase(--ps.end()); ns.insert(v); s -= -v, s += v; r = v; s += -a[i]; ps.insert(a[i]); }else { s += a[i]; ns.insert(a[i]); } int j = i - l; if (j >= 0) if (a[j] >= 0) s -= a[j]; else if (ps.find(a[j])!=ps.end()) { ps.erase(ps.find(a[j])); s -= -a[j]; if (!ns.empty()) { int v = *ns.begin(); ns.erase(ns.begin()); ps.insert(v); s -= v, s += -v; } }else { s -= a[j]; ns.erase(ns.find(a[j])); } if (j >= -1) ans = max(ans, s); }
int main() { freopen("grass.in", "r", stdin); freopen("grass.out", "w", stdout); ios_base::sync_with_stdio(false); int N, M, K, Q; cin >> N >> M >> K >> Q; for (int i = 0; i < M; ++i) cin >> Y[i].a >> Y[i].b >> Y[i].c; sort(Y, Y + M); for (int i = 1; i <= N; ++i) { f[i] = i; d[i] = 1; } int cnt = 0; for (int i = 0; i < M; ++i) { if (find(Y[i].a) == find(Y[i].b)) continue; int a = Y[i].a, b = Y[i].b, c = Y[i].c; X[a].push_back(make_pair(b, c)); X[b].push_back(make_pair(a, c)); unionn(a, b); ++cnt; if (cnt == N - 1) break; } for (int i = 1; i <= N; ++i) cin >> c[i]; dfs(1, -1); for (int i = 0; i < Q; ++i) { int a, b; cin >> a >> b; if (c[a] != b) update(a, b); cout << *S.begin() << endl; } return 0; }
/***************************************** * Set reconstruction from pairwise sums * ***************************************** * Given a multiset S of integers, reconstructs * the multiset V such that S is the multiset of * pairwise sums of V. Fills in V in sorted order. * Returns true if successful and false if confused. * #include <set> * ACKNOWLEDGEMENTS: Thanks to Stefan Pochmann for * the idea, David Arthur for the generalized * algorithm and Bartholomew Furrow for a * reference implementation. **/ bool pairsums( int *ans, multiset< int > &seq ) { int N = seq.size(); if( N < 3 ) return false; __typeof( seq.end() ) it = seq.begin(); int a = *it++, b = *it++, i = 2; for( ; i * ( i - 1 ) < 2 * N && it != seq.end(); i++, ++it ) { // assume seq[i] = ans[1] + ans[2] ans[0] = a + b - *it; if( ans[0] & 1 ) continue; ans[0] >>= 1; // try ans[0] as a possible least element multiset< int > seq2 = seq; int j = 1; while( seq2.size() ) { ans[j] = *seq2.begin() - ans[0]; for( int k = 0; k < j; k++ ) { __typeof( seq2.end() ) jt = seq2.find( ans[k] + ans[j] ); if( jt == seq2.end() ) goto hell; seq2.erase( jt ); } j++; } hell:; if( j * ( j - 1 ) < 2 * N ) continue; // it worked! [modify this to deal with multiple answers] return true; } return false; }
void HuffmanTree::buildTreeFromLeafNodes(multiset<HuffmanNode*, NodeLess>& nodes) { while (nodes.size() > 1) { string key1; string key2; int freq1 = 0; int freq2 = 0; HuffmanNode* left = nullptr; HuffmanNode* right = nullptr; processMinimumNode(nodes, key1, freq1, left); processMinimumNode(nodes, key2, freq2, right); HuffmanBranchNode* newNode = new HuffmanBranchNode(); newNode->setKey(key1 + key2); newNode->setFrequency(freq1 + freq2); newNode->setLeftNode(left); newNode->setRightNode(right); nodes.insert(newNode); } HuffmanNode* root = *(nodes.begin()); treeSet.insert(root); nodes.erase(root); }
int main(){ scanf("%d",&n); while(n--){ scanf("%d",&k); while(k--){ scanf("%d",&x); s.insert(x); } it1=s.begin(); it2=s.end(); it2--; sum+=(*it2-*it1); s.erase(it1); s.erase(it2); } printf("%lld\n",sum); return 0; }
int main() { LL sum=0; int n,k,value; n = read_int(); for(int i=0;i<n;i++) { k = read_int(); for(int j=0;j<k;j++) { value = read_int(); my.insert(value); } head = my.begin(); tail = my.end(); tail--; sum += (*tail - *head); my.erase(head); my.erase(tail); } write_int(sum); write_flush(); return 0; }
void push_sum(const multiset<int> &ms) { int sum = 0; multiset<int>::const_iterator i = ms.begin(); for (; i != ms.end(); i++) { sum += 1 << (*i); } solution.insert(sum); }
/* main.. //*/ int main() { using namespace std; cout << "init\n"; int arr_a[] = { 2, 4, 4, 4, 6, 6, 8 }; int arr_b[] = { 3, 4, 4, 6, 9, 9 }; const multiset< int > multi_a(arr_a, arr_a+7); const multiset< int > multi_b(arr_b, arr_b+6); ostream_iterator< int > os_iter(cout, " "); cout << "multi_a: \t"; copy(multi_a.begin(), multi_a.end(), os_iter); cout << "\nmulti_b: \t"; copy(multi_b.begin(), multi_b.end(), os_iter); cout << endl << endl; cout << "set_union()\n"; set_union(multi_a.begin(), multi_a.end(), multi_b.begin(), multi_b.end(), os_iter); cout << endl << endl; cout << "set_intersection()\n"; set_intersection(multi_a.begin(), multi_a.end(), multi_b.begin(), multi_b.end(), os_iter); cout << endl << endl; cout << "set_difference()\n"; set_difference(multi_a.begin(), multi_a.end(), multi_b.begin(), multi_b.end(), os_iter); cout << endl << endl; cout << "set_symmetric_difference()\n"; set_symmetric_difference(multi_a.begin(), multi_a.end(), multi_b.begin(), multi_b.end(), os_iter); cout << endl << endl; cout << "includes()\n"; cout << "multi_a includes multi_b: " << boolalpha << includes(multi_a.begin(), multi_a.end(), multi_b.begin(), multi_b.end()) << endl; cout << endl; cout << "READY.\n"; return 0; }
void remove(ll x){ multiset<ll>::iterator it; multiset<ll>::iterator jt; if(cnt == 0){ printf("Wrong!\n"); return; } if (cnt == 1){ it = m2.find(x); if (it != m2.end()){ m2.erase(x); c2--; cnt--; } printf("Wrong!\n"); return; } else { if (cnt%2){ it = m2.find(x); jt = m1.find(x); if (it != m2.end()){ c2--; m2.erase(it); cnt--; print(); } else if (jt != m1.end()) { c2--; m1.erase(jt); it = m2.begin(); m1.insert(*it); m2.erase(it); cnt--; print(); } else { printf("Wrong!\n"); } } else { it = m2.find(x); jt = m1.find(x); if (jt != m1.end()){ c1--; m1.erase(jt); cnt--; print(); } else if (it != m2.end()) { c1--; m2.erase(it); jt = m1.end(); jt--; m2.insert(*jt); m1.erase(jt); cnt--; print(); } else { printf("Wrong!\n"); } } } }
void MAIN() { int cases; pree(); scanf("%d",&cases); int tn,cct,vct; int i,j; for(int t=1;t<=cases;++t) { ansc.clear(); ansv.clear(); scanf("%d",&n); cct=n/2; vct=n-cct; tn=n; for(i=0;i<v.size();++i) { for(j=0;j<min(21,vct);++j) { //printf("%c\n",v[i].second); ansv.insert(v[i].second); } vct-=j; } for(i=0;i<c.size();++i) { for(j=0;j<min(5,cct);++j) { //printf("%c\n",c[i].second); ansc.insert(c[i].second); } cct-=j; } printf("Case %d: ",t); sit1=ansv.begin(); sit2=ansc.begin(); //printf("%d\t%d\n",anv.size()) for(i=0;i<n;++i) { if(i&1) { printf("%c",*sit2); ++sit2; } else { printf("%c",*sit1); ++sit1; } } printf("\n"); } }
void displayFiles(const multiset<char*, classComp>& sortedFiles, bool hasL, string dirName, int maxFileSize, int compareSize){ if(!hasL){ struct stat buf; string copy = ""; int rows = ceil (((double) compareSize) / maxFileSize) ; int columns = ceil(((double) sortedFiles.size()) / rows); int sizeSet = sortedFiles.size(); for(int i = 0; i < sizeSet; i++){ if(rows * columns < sizeSet){ columns += 1; } else break; } int maxColumnWord = 0; int i = 0, j = 0; bool needsColumns = compareSize > 80; vector<vector<char*> > table(rows); vector<char*> tColumns(columns); vector<int> columnWidths; for(multiset<char*, classComp>::iterator it = sortedFiles.begin(); it != sortedFiles.end(); it++){ copy = dirName + "/"; copy += *it; //Output for no or -a flag options with color if(stat((char*) copy.c_str(), &buf) == -1){ perror("stat()"); exit(-1); } if(!needsColumns && (*it)[0] == '.'){ cout << "\033[2;47;33m"; } if(!needsColumns && S_ISDIR(buf.st_mode)){ cout << "\033[1;34m"; } else if(!needsColumns && (buf.st_mode & S_IRWXU & S_IXUSR)) cout << "\033[1;32m"; if(!needsColumns) cout << *it << "\033[0m" << " " << flush; else{ tColumns.at(i) = *it; if(maxColumnWord < (int) strlen(*it)) maxColumnWord = strlen(*it); i++; if(i >= columns){ i = 0; table.at(j) = tColumns; j++; columnWidths.push_back(maxColumnWord); maxColumnWord = 0; } } } if(needsColumns){ if(columns * rows != sizeSet){ tColumns.resize(i); table.at(j) = tColumns; columnWidths.push_back(maxColumnWord); } printColumns(table, columnWidths, dirName); } } else{ struct stat buf; int max = 0; string copy = ""; if(maxFileSize != 0 && compareSize != 0){ for(multiset<char*, classComp>::iterator it = sortedFiles.begin(); it != sortedFiles.end(); it++){ copy = dirName + "/"; copy += *it; if(stat((char*) copy.c_str(), &buf) == -1){ perror("stat():"); exit(-1); } max += buf.st_blocks; } } max /= 2; cout << "total " << max << endl; copy = ""; for(multiset<char*, classComp>::iterator it = sortedFiles.begin(); it != sortedFiles.end(); it++){ if(maxFileSize == 0 && compareSize == 0){ copy = *it; } else{ copy = dirName + "/"; copy += *it; } if(stat((char*) copy.c_str(), &buf) == -1){ perror("stat():"); exit(-1); } //checks whether directory or file if(S_ISREG(buf.st_mode)) cout << '-'; else if(S_ISDIR(buf.st_mode)) cout << 'd'; //this will check for permissions for user (buf.st_mode & S_IRWXU & S_IRUSR) ? cout << 'r' : cout << '-'; (buf.st_mode & S_IRWXU & S_IWUSR) ? cout << 'w' : cout << '-'; (buf.st_mode & S_IRWXU & S_IXUSR) ? cout << 'x' : cout << '-'; //this will check for permissions for groups (buf.st_mode & S_IRWXG & S_IRGRP) ? cout << 'r' : cout << '-'; (buf.st_mode & S_IRWXG & S_IWGRP) ? cout << 'w' : cout << '-'; (buf.st_mode & S_IRWXG & S_IXGRP) ? cout << 'x' : cout << '-'; //this will check for permissions for all others (buf.st_mode & S_IRWXO & S_IROTH) ? cout << 'r' : cout << '-'; (buf.st_mode & S_IRWXO & S_IWOTH) ? cout << 'w' : cout << '-'; (buf.st_mode & S_IRWXO & S_IXOTH) ? cout << 'x' : cout << '-'; //Used to get user name from user id struct passwd *pass; pass = getpwuid(buf.st_uid); if(pass == NULL){ perror("getpwuid"); exit(-1); } char* usr = pass->pw_name; //Used to get group name from group id struct group *gpass; gpass = getgrgid(buf.st_gid); if(gpass == NULL){ perror("getgrgid"); } char* grp = gpass->gr_name; //Used to get time format from time_t struct tm myTime; if(localtime_r(&buf.st_mtime, &myTime) == NULL){ perror("localtime_r"); exit(-1); } char month[5]; char day[3]; char time[6]; if(strftime(month, sizeof(month), "%b", &myTime) == 0 || strftime(day, sizeof(day), "%d", &myTime) == 0 || strftime(time, sizeof(time), "%R", &myTime) == 0){ perror("strftime"); exit(-1); } //Output for -l flag with color cout << right << ' ' << buf.st_nlink << ' ' << usr << ' ' << grp << ' ' << setw (6) << buf.st_size << ' ' << month << ' ' << day << ' ' << time << ' '; if(S_ISDIR(buf.st_mode)){ cout << "\033[1;34m"; } if((*it)[0] == '.'){ cout << "\033[2;47;33m"; } cout << *it << "\033[0m" << endl; } } }
int main() { freopen("in", "r", stdin); freopen("std", "w", stdout); tree.clear(); while (1) { int operation; scanf("%d", &operation); if (operation == -1) { return 0; } /* else if (operation == 0) { tree.clear(); } */ else if (operation == 0) { int key; scanf("%d", &key); tree.insert(key); } else if (operation == 1) { int key; scanf("%d", &key); multiset<int>::iterator it = tree.find(key); if (it != tree.end()) { tree.erase(it); printf("OK\n"); } else printf("Delete Error\n"); } else if (operation == 2) { int key; scanf("%d", &key); multiset<int>::iterator it = tree.find(key); if (it != tree.end()) printf("Yes\n"); else printf("No\n"); } /* else if (operation == 4) { int key; scanf("%d", &key); multiset<int>::iterator it = tree.find(key); if (it != tree.end()) { -- it; printf("%d\n", *it); } else printf("Prev Error!\n"); } else if (operation == 5) { int key; scanf("%d", &key); multiset<int>::iterator it = tree.find(key); if (it != tree.end()) { ++ it; printf("%d\n", *it); } else printf("Succ Error!\n"); } */ else if (operation == 3) { if (tree.empty()) printf("NULL\n"); else { multiset<int>::reverse_iterator it = tree.rbegin(); printf("%d\n", *it); } } else if (operation == 4) { if (tree.empty()) printf("NULL\n"); else { multiset<int>::iterator it = tree.begin(); printf("%d\n", *it); } } } return 0; }
int main(){ while (~scanf("%d%d",&n,&m)){ S.clear(); for (int i=1;i<=n;i++){ scanf("%d",&a[i]); S.insert(a[i]); } if (n==1){ printf("1\n%d\n",a[1]); continue; } if (n==2){ if (a[1]+a[2]>m) printf("2\n%d %d\n",a[1],a[2]); else printf("1\n%d %d\n",a[1],a[2]); continue; } memset(bi,true,sizeof(bi)); sort(a+1,a+n+1); b[2]=a[n]; multiset<int>::iterator it; /* it=S.lower_bound(b[2]); S.erase(it); it=S.upper_bound(m-b[2]); if (it==S.end()) --it; b[1]=(*it); S.erase(it); for (int i=2;i<n;++i){ it=S.upper_bound(m-b[i]); if (it==S.end()) --it; b[i+1]=(*it); S.erase(it); } */ bool flag=true; for (int i=1;i<=n;i++) { if (flag) { if (i<n) { it=S.end();it--; b[i+1]=(*it); S.erase(it); it=S.upper_bound(m-b[i+1]); if (it==S.end()) it=S.begin(); b[i]=(*it); S.erase(it); i++; if (b[i-1]+b[i]<=m) flag=true; else flag=false; } else { it=S.begin(); b[i]=(*it); S.erase(it); } } else { it=S.upper_bound(m-b[i-1]); if (it==S.end()) { it=S.begin(); flag=true; } else flag=false; b[i]=(*it); S.erase(it); } } int ans=0; b[n+1]=0; for (int i=1;i<=n;++i){ if (b[i]+b[i+1]<=m) i++; ans++; } printf("%d\n",ans); for (int i=1;i<=n;++i) printf("%d%c",b[i],i==n?'\n':' '); } return 0; }
double median(char c,long long x){ double retval; if(DEBUG){ for(it = minset.begin();it!=minset.end();it++) cout<<*it<<" "; cout<<":"; for(it = maxset.begin();it!=maxset.end();it++) cout<<*it<<" "; cout<<endl; } if(c == 'r'){ if(minset.empty() && maxset.empty()){ flag++; return -1; } else if(minset.empty()){ if(maxset.count(x) == 0){ flag++; return -1; } else{ // TODO - Done it = maxset.find(x); if(it != maxset.end()) maxset.erase(it); } } else if(maxset.empty()){ if(minset.count(x) == 0){ flag++; return -1; } else{ // TODO - Done it = minset.find(x); if(it != minset.end()) minset.erase(it); } } else if(minset.count(x) == 0 && maxset.count(x) == 0){ flag++; return -1; } else{ // TODO - Done if(minset.count(x) != 0){ it = minset.find(x); if(it != minset.end()) minset.erase(it); } else{ it = maxset.find(x); if(it != maxset.end()) maxset.erase(it); } } adjust(); } else if(c == 'a'){ it = maxset.begin(); if(*it <= x) maxset.insert(x); else minset.insert(x); adjust(); } if(minset.empty()){ flag++; return -1; } else{// TODO if(minset.size() > maxset.size()){ it = minset.end(); it--; retval = *it; } else{ it = minset.end(); it--; retval = *it; it = maxset.begin(); retval = (retval+(*it))/2; } } if(DEBUG){ cout<<"Minset not empty"<<endl; for(it = minset.begin();it!=minset.end();it++) cout<<*it<<" "; cout<<":"; for(it = maxset.begin();it!=maxset.end();it++) cout<<*it<<" "; } return retval; }
int main () { scanf(" %d %d %d", &n, &s, &t); for (int i = 0; i < n; i++) { scanf(" %d", &v[i]); st.insert(v[i]); } ll _min = inf, _max = 0, aver = 0; for (auto i = st.begin(); i != st.end(); i++) { int from = *i; for (int j = 0; j < n; j++) ms.insert(v[j]); int cur = from; ll cont = 0; while (!ms.empty()) { if (ms.find(cur) != ms.end()) { ms.erase(ms.find(cur)); cur = (cur + t) % s; cont += ll(t); } else { auto it = ms.lower_bound(cur); if (it == ms.end()) it = ms.begin(); int nx = *it; if (nx > cur) cont += ll(nx - cur); else cont += ll(s - cur + nx); cur = nx; } } cost[from] = cont; _min = min(_min, cont); _max = max(_max, cont); aver += cont; } for (int i = 0; i < n; i++) ms.insert(v[i]); for (int i = 0; i < s; i++) { if (ms.find(i) != ms.end()) continue; auto it = ms.lower_bound(i); if (it == ms.end()) it = ms.begin(); int nx = *it; ll cont = 0; if (nx > i) cont = ll(nx - i + cost[nx]); else cont = ll(s - i + nx + cost[nx]); _min = min(_min, cont); _max = max(_max, cont); aver += cont; } ll d = gcd(aver, s); printf("%lld %lld %lld/%lld\n", _min, _max, aver / d, s / d); }
int main() { freopen("C:\\Users\\crazyacking\\Desktop\\cin.txt","r",stdin); // freopen("C:\\Users\\crazyacking\\Desktop\\cout.txt","w",stdout); ios_base::sync_with_stdio(false); cin.tie(0); int cas; cin>>cas; while(cas--) { cin>>L>>N>>K; int x,m; int cnt=1; for(int i=0;i<N;++i) { cin>>x>>m; while(x!=0 && m--) { apple.insert(make_pair(x,cnt++)); } } debug(); LL ans=0; LL mid=L/2; int container; multiset<pair<int,int> >::iterator ibe,it,tmp; for(ibe=apple.begin();ibe!=apple.end() && (*ibe).first<mid;) { container=0; while(container!=K && (*ibe).first<mid) { ibe++; container++; } --ibe; if(container==K) { it=ibe; ans+=(*it).first*2; for(int i=0;i<K;++i) { it=apple.begin(); apple.erase(*it); } } else break; } debug(); multiset<pair<int,int> > :: reverse_iterator rev=apple.rbegin(),rit; for(rev=apple.rbegin();rev!=apple.rend() && (*rev).first>=mid;) { container=0; while(container!=K && (*rev).first>=mid) { rev++; container++; } rev--; if(container==K) { rit=rev; cout<<"hehe="<<(*rit).first<<endl; ans+=(L-(*rit).first)*2; for(int i=0;i<K;++i) { rit=apple.rbegin(); apple.erase(*rit); } } else break; } if(apple.size()<K) { ans+=L; // run a cycle } else // 3 case { int r=-1-0x7fffffff,l=0x7fffffff; multiset<pair<int,int> > :: iterator it; for(it=apple.begin();it!=apple.end();++it) { if((*it).first<mid) r=max(r,(*it).first); else l=min(l,(*it).first); } LL dis1=0,dis2=0,dis3=0; if(r!=-1-0x7fffffff) { dis1+=r; } if(l!=0x7fffffff) { dis1+=L-l; } for(container=0,it=apple.begin();it!=apple.end()&&container !=K;++it,++container){} dis2+=L+(L-(*it).first)*2; for(container=0,it=apple.end();it!=apple.begin()&&container!=K;--it,container++){} dis3+=L+(*it).first*2; LL dis=min(dis1,min(dis2,dis3)); ans+=dis; } cout<<ans<<endl; } return 0; }
bool operator < (const anagram &o) const { if(todas.size() == o.todas.size()) return *todas.begin() < *(o.todas.begin()); return todas.size() > o.todas.size(); }
void printMSet(const multiset<int>& s, char *c) { cout << c << ": "; for (multiset<int>::const_iterator i = s.begin(); i != s.end(); i++) cout << *i << ' '; cout << endl; }