int main(){
    int n, m, x;
    while( scanf("%d%d", &n, &m) != EOF ){
        st.clear();
        for(int i=0 ; i < n ; ++i){
            scanf("%d", &x);
            st.insert(x);
        }
        for(int i=0 ; i < m ; ++i)
            scanf("%d", &a[i].d);
        for(int i=0 ; i < m ; ++i)
            scanf("%d", &a[i].p);
        if( m < n ){
            puts("No"); continue;
        }
        sort( a, a+m, cmp );
        LL ans = 0;
        for(int i=0 ; i < m ; ++i){
            it = st.upper_bound( a[i].d );
            if( it != st.begin() ){
                it--;
                st.erase(it);
                ans += a[i].p;
                if( st.empty() ) break;
            }
        }
        if( st.empty() ) printf("%I64d\n", ans);
        else puts("No");
    }
    return 0;
}
Пример #2
0
int main() {
    ifstream in; ofstream out;
    in.open("input.txt"); out.open("output.txt");
   	int count=0;
    in >> M >> N;
   	
	char t;	
	for(int i=0; i<M; i++){
		in >> t;
		parolaChars.insert(t);
	}
	in >> stringa;



	for(int i=0; i<M; i++){
		
		it = parolaChars.find (stringa[i]);
		if(it!=parolaChars.end())
			parolaChars.erase (it);
		else
			parolaCharsUnused.insert(stringa[i]);
	}
	if(parolaChars.empty())
		count++;

	for(int i=M; i<N; i++){
		it = parolaCharsUnused.find (stringa[i-M]);
		if(it!=parolaCharsUnused.end())
			parolaCharsUnused.erase (it);
		else
			parolaChars.insert(stringa[i-M]);

		it = parolaChars.find (stringa[i]);
		if(it!=parolaChars.end())
			parolaChars.erase (it);
		else
			parolaCharsUnused.insert(stringa[i]);

		//cout << parolaChars.size() << " " << stringa[i-M] << " " << stringa[i] << endl; 
		if(parolaChars.empty())
			count++;
	}
	
	out << count;

    in.close(); out.close();
   
    return 0;
}
Пример #3
0
void adjust(){
	long long val;

	if(!minset.empty()){

		if(!maxset.empty()){

			if(maxset.size() > minset.size()){
				it = maxset.begin();
				val = *it;
				maxset.erase(it);
				minset.insert(val);
				adjust();
			}

			else if(minset.size() - maxset.size() > 1){
				it = minset.end();
				it--;
				val = *it;
				minset.erase(it);
				maxset.insert(val);
				adjust();
			}
		}

		else{

			if(minset.size() > 1){
				it = minset.end();
				it--;
				val = *it;
				minset.erase(it);
				maxset.insert(val);
				adjust();
			}
		}
	}

	else{
		if(!maxset.empty()){
			it = maxset.begin();
			val = *it;
			maxset.erase(it);
			minset.insert(val);
			adjust();
		}
	}
}
Пример #4
0
int main(){
	int T, n;
	scanf("%d", &T);
	while( T-- ){
		scanf("%d", &n);
		for(int i=0 ; i < n ; ++i)
			scanf("%d%d", &a[i].h, &a[i].w);
		for(int i=0 ; i < n ; ++i)
			scanf("%d%d", &b[i].h, &b[i].w);
		sort( a, a+n, cmp );
		sort( b, b+n, cmp );
		st.clear();
		int ans = 0;
		for(int i=0,j=0 ; i < n ; ++i){
			while( j < n && a[i].h >= b[j].h ){
				st.insert(b[j].w);
				j++;
			}
			it = st.upper_bound(a[i].w);
			if( it != st.begin() && !st.empty() ){
				--it;
				st.erase(it);
				ans++;
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}
Пример #5
0
int main()
{
	scanf("%d", &t);
	while(t--)
	{
		y.clear();
		scanf("%d", &n);
		for(int i = 0; i < n; ++i)
			scanf("%d%d", &a[i].h, &a[i].w);
		sort(a, a + n);
		for(int i = 0; i < n; ++i)
			scanf("%d%d", &b[i].h, &b[i].w);
		sort(b, b + n);
		ans = 0;
		for(int i = 0, j = 0; i < n; ++i)
		{
			while(j < n && b[j].h <= a[i].h)
				y.insert(b[j++].w);
			if(y.empty())
				continue;
			multiset<int>::iterator it = y.upper_bound(a[i].w);
			if(it != y.begin())
			{
				y.erase(--it);
				++ans;
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}
Пример #6
0
	void encodeAllSymbols(){
		if (treeSet.empty()) {
			return;
		}

		string pattern;
		HuffmanNode* root = *(treeSet.rbegin());
		_encodeAllSymbols(pattern, root);
	}
Пример #7
0
/* Function: PerformInsert(int value,
 *                         multiset<int>& minSet,
 *                         multiset<int>& maxSet)
 * Usage: bool success = PerformInsert(value, minSet, maxSet);
 * -----------------------------------------------------------------------------
 * Inserts one instance of the specified value into the minset or the maxset, 
 * according to the following logic: if the value exceeds the current maximum 
 * value in the minset, then add the value to the maxset; otherwise, add the 
 * value to the minset. The function returns true if the insert operation was 
 * successful, and false otherwise.
 */
bool PerformInsert(int value, multiset<int>& minSet, multiset<int>& maxSet)
{
  if (minSet.empty() || value <= *FindMaxElement(minSet))
    minSet.insert(value);
  else
    maxSet.insert(value);
  
  /* Always return true. */
  return true;
}
Пример #8
0
 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));
   }
 }
Пример #9
0
inline int Dijkstra()
{
    for (unsigned short int i = 1; i <= n; i++)
    {
        noduri[i].vizitat = false; //Nu am vizitat nici und nod
        noduri[i].cost = INFINIT; //Costul pt. a ajunge la restul nodurilor e infinit
    }
    short int nod_curent = sursa;
    noduri[sursa].cost = 0; //Costul pt. a ajunge la sursa e 0
    lista.insert(sursa);
    while (lista.empty() == false)
    {
        //Cautam nodul nevizitat cel mai apropiat de sursa
        nod_curent = *lista.begin();
        lista.erase(lista.begin());
        noduri[nod_curent].vizitat = false; //Marcam nodul curent ca nevizitat(in cazul revenirii pe un alt drum)
        for (unsigned short int i = 0; i < noduri[nod_curent].vecini.size(); i++) //Parcurgem toti vecinii nodului curent
        {
            if (capacitate[nod_curent][noduri[nod_curent].vecini[i].b] > flux[nod_curent][noduri[nod_curent].vecini[i].b])
            {
                if ((noduri[nod_curent].cost + noduri[nod_curent].vecini[i].cost) < noduri[noduri[nod_curent].vecini[i].b].cost)
                {
                    noduri[noduri[nod_curent].vecini[i].b].cost = noduri[nod_curent].cost + noduri[nod_curent].vecini[i].cost;
                    drum[noduri[nod_curent].vecini[i].b] = nod_curent; //In acest vecin ajungem din nodul curent
                    if (noduri[noduri[nod_curent].vecini.at(i).b].vizitat == false)
                    {
                        noduri[noduri[nod_curent].vecini.at(i).b].vizitat = true;
                        lista.insert(noduri[nod_curent].vecini[i].b); //Adaugam vecinul in coada
                    }
                }
            }
        }
    }
    if (noduri[destinatie].cost < INFINIT)
    {
        drum_ameliorare = true;
        int capacitate_reziduala = INFINIT;
        for (nod_curent = destinatie; nod_curent != sursa; nod_curent = drum[nod_curent])
            if (capacitate_reziduala > (capacitate[drum[nod_curent]][nod_curent] - flux[drum[nod_curent]][nod_curent]))
                capacitate_reziduala = capacitate[drum[nod_curent]][nod_curent] - flux[drum[nod_curent]][nod_curent];
        for (nod_curent = destinatie; nod_curent != sursa; nod_curent = drum[nod_curent])
        {
            flux[nod_curent][drum[nod_curent]] -= capacitate_reziduala;
            flux[drum[nod_curent]][nod_curent] += capacitate_reziduala;
        }
        return noduri[destinatie].cost * capacitate_reziduala;
    }
    else
    {
        drum_ameliorare = false;
        return 0;
    }
}
Пример #10
0
bool test(int first, multiset<int> s){
	int idx = 0, z;
	multiset<int>::iterator it;
	a[idx++] = first;
	while(!s.empty()){
		z = *s.begin() - first;
		FOR(i, idx){
			if((it = s.find(z + a[i])) == s.end()) return false;
			s.erase(it);
		}
		a[idx++] = z;
	}
	return true;
}
Пример #11
0
/* Function: PerformSingleOperation(string operation,
 *                                  int value,
 *                                  multiset<int>& minSet,
 *                                  multiset<int>& maxSet)
 * Usage: string median = PerformSingleOperation(op, x, minSet, maxSet)
 * -----------------------------------------------------------------------------
 * Performs the specified operation (insert or remove) with the specified value, 
 * and returns a formatted string representation of the new median value after 
 * performing the operation.
 */
string PerformSingleOperation(string operation, int value,
                              multiset<int>& minSet, multiset<int>& maxSet)
{
  bool operationSucceeded;
  
  /* Handle an insert operation. */
  if (operation == kInsertOperation)
    operationSucceeded = PerformInsert(value, minSet, maxSet);
  
  /* Handle a delete operation. */
  else if (operation == kDeleteOperation)
    operationSucceeded = PerformDelete(value, minSet, maxSet);
  
  /* Resize each set to be of size n/2. */
  ResizeSets(minSet, maxSet);
  
  /* Return an error message if necessary. */
  if (!operationSucceeded || (minSet.empty() && maxSet.empty()))
    return kErrorMessage;
  
  /* Otherwise return the median as a string. */
  return GetFormattedMedian(minSet, maxSet);
}
Пример #12
0
bool check()
{
    if(tcnt < st.size()) return 0 ;
    scnt=0 ;
    for(int i=0;!st.empty() && i<tcnt;i++)
    {
        auto it=st.upper_bound(tmp[i]) ;
        if(it!=st.begin())
            it-- , st_tmp[scnt++]=*it , st.erase(it) ;
    }
    int sz=st.size() ;
    for(int i=0;i<scnt;i++) st.insert(st_tmp[i]) ;
    return sz==0 ;
}
Пример #13
0
int main()
{
	int t,x;
	gint(t);
	while(t--)
	{
		while(!s1.empty())
			s1.erase(s1.begin());
		while(!s2.empty())
			s2.erase(s2.begin());
		while(1)
		{
			gint(x);
			if(x==0)
				break;
			else if(x==-1)
				median_ele();
			else if(x>0)
				add_ele(x);
		}
	}
	return 0;
}
Пример #14
0
void solve() {
    sort(c, c + n);
    sort(g, g + m);

    for(int i = 0; i < m; i ++)
        if(tmpCost != g[i].cost) {
            calc(tmpCost);
            tmpCost = g[i].cost;
            grass.push(g[i]);
        }
        else grass.push(g[i]);

    calc(tmpCost);

    if(active.empty()) cout << ans << endl;
    else cout << "-1\n";
}
Пример #15
0
// return path from 0,0 to (N-1, N-1)
vector<pair<int,int> > Dijkstra(multiset<Node, NodeCompare>& nodes, int N)
{
   vector<pair<int,int> > the_path;  //

   multiset<Node,NodeCompare>::iterator best = nodes.begin();

   while (!nodes.empty())
   {
      multiset<Node,NodeCompare>::iterator best = nodes.begin();

      if (best->x == N-1 && best->y == N-1)
      {
         the_path = best->path;
         the_path.push_back(pair<int,int>(N-1, N-1));
         return the_path;
      }
      int x = best->x;
      int y = best->y;
      double curr_cost = best->cost;
      vector<pair<int,int> >  curr_path = best->path;
      nodes.erase(best);

      //modify neighboring nodes
      for (int i = -2; i <= 2; ++i)
      {
         for (int j = -2; j <= 2; ++j)
         {
            if (i == 0 && j == 0 ) { continue;}
            multiset<Node,NodeCompare>::iterator next = FindNode(nodes, x + i, y + j);
            if (next != nodes.end())
            {
               if (next->cost > curr_cost + sqrt(i*i + j*j)*next->weight)  // update node based on a contrived measure of 'distance'
               {
                  Node replacer(next->x, next->y, curr_cost + sqrt(i*i + j*j)*next->weight, next->weight);
                  replacer.path = curr_path;
                  replacer.path.push_back(pair<int,int>(next->x, next->y));
                  nodes.erase(next);
                  nodes.insert(replacer);
               }
            }
         }
      }
   }
   return the_path;
}
Пример #16
0
void generiraj_p() {

    ++koliko_p;
    if( rand() % 2 && !M.empty() ) { // obrisi k-ti
        int k = get_rand( 1, M.size() );
        printf( "BRISI %d\n", k );

        for( multiset<int> :: iterator it = M.begin(); it != M.end(); ++it ) {
             --k;
             if( k == 0 ) {
                 M.erase( it );
                 break;
             }
        }
    } else {
        printf( "DODAJ %d\n", get_rand( 0, MAXA-1 ) );
    }

}
Пример #17
0
 void erase(int num)
 {
     // assert median != -1
     if(median == num)
     {
         // assert small_number_set.size() <= big_number_set.size()
         median = -1;
         if(small_number_set.size() < big_number_set.size())    
         {
             auto median_iter = big_number_set.begin();
             median = *median_iter;
             big_number_set.erase(median_iter);
         }
         else if(!small_number_set.empty())
         {
             auto median_iter = prev(small_number_set.end());
             median = *median_iter;
             small_number_set.erase(median_iter);
         }
     }
     else if(num < median)
     {
         small_number_set.erase(small_number_set.find(num));
         if(small_number_set.size() + 1 < big_number_set.size())
         {
             small_number_set.insert(median);
             auto median_iter = big_number_set.begin();
             median = *median_iter;
             big_number_set.erase(median_iter);
         }
     }
     else // assert num > median
     {
         big_number_set.erase(big_number_set.find(num));
         if(small_number_set.size() > big_number_set.size())
         {
             big_number_set.insert(median);
             auto median_iter = prev(small_number_set.end());
             median = *median_iter;
             small_number_set.erase(median_iter);
         }
     }
 }
Пример #18
0
 // Adds a number into the data structure.
 void addNum(int num) {
     if (first.empty() || num <= *(first.rbegin()) ) {
         first.insert(num);
     } else {
         second.insert(num);
     }
     
     if (first.size() > second.size() + 1) {
         auto it = first.end();
         it--;
         second.insert(*(it));
         first.erase(it);
     }
     
     if ( first.size() < second.size() ) {
         first.insert(*(second.begin()));
         second.erase(second.begin());
     }
 }
Пример #19
0
 // Adds a number into the data structure.
 void addNum(int num) {
     if (firstHalf.empty()) {
         firstHalf.insert(num);
         return;
     }
     int firstHalfLargest = *firstHalf.begin();
     if (num <= firstHalfLargest) {
         firstHalf.insert(num);
         if (firstHalf.size() > lastHalf.size()+1) {
             firstHalf.erase(firstHalf.begin());
             lastHalf.insert(firstHalfLargest);
         }
     } else {
         lastHalf.insert(num);
         if (lastHalf.size() > firstHalf.size()) {
             firstHalf.insert(*lastHalf.begin());
             lastHalf.erase(lastHalf.begin());
         }
     }
 }
Пример #20
0
int main()
{
    freopen("input.txt","r",stdin);
    int n,q;
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;++i)
        scanf("%d",&hehe[i]);
    int num=1;
    for(int i=1;i<=q;++i)
    {
        int t;
        scanf("%d",&t);
        while(t>=num)
        {
            if(!b.empty())
            {
                multiset<int>::iterator b_it=b.end();
                --b_it;   //这里一定要注意!要切记,C++有太多的东西是左闭右开的了!
                if(hehe[num]<*b_it)
                {
                    int temp=*b_it;
                    b.erase(b_it);
                    b.insert(hehe[num]);
                    a.insert(temp);
                }
                else a.insert(hehe[num]);
            }
            else
            {
                a.insert(hehe[num]);
            }
            ++num;
        }
        multiset<int>::iterator a_it=a.begin();
        printf("%d\n",*a_it);
        int temp=*a_it;
        a.erase(a_it);
        b.insert(temp);
    }
    return 0;
}
Пример #21
0
ll rollback(){
	//cout << "Rollbacking " _ pop << endl;
	if(full.find(B[pop]) != full.end()){
		full.erase(full.find(B[pop]));
		ttime -= B[pop];
		val -= A[pop];
	}
	else if(half.find(B[pop]) != half.end()){
		half.erase(half.find(B[pop]));
		val -= A[pop];
		ttime -= (B[pop] + 1) / 2;
		if(!full.empty()){
			half.insert(*full.rbegin());
			ttime -= (*full.rbegin() ) / 2;
			auto it = full.end(); it--;
			full.erase(it);
			// full.erase(full.rbegin());
		}
	}
	pop++;
}
Пример #22
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;
		}
		
		
	}
}
Пример #23
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;
	}
}
Пример #24
0
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) ;
}
Пример #25
0
		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);
		}
Пример #26
0
int main () {
    scanf ("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf ("%d", &v[i]);
    }

    acc[0] = 0;
    for (int i = 0; i < n; i++) 
        acc[i+1] = acc[i] + v[i];

    for (int i = 0; i < n; i++) 
        scanf ("%d", &destroy[i]);

    inter[pii (1, n)] = acc[n];
    sum.insert (acc[n]);
    printf ("maior %lld\n", *sum.rbegin ());

    for (int i = 0; i < n; i++) {
        printf ("\ni %d\n", i);
        int a, ans, l, r;
        pii dead;

        a = destroy[i];
        gone.insert (a);
        // l, r sao os nao destruidos mais longe de a, antes dos primeiros destruidos
        l = *gone.find (gone.find (a) - 1);
        r = *gone.find (gone.find (a) + 1);
        dead = pii (l+1, r-1);

        printf ("antes i %d maior %lld\n", i, *sum.rbegin ());
        sum.erase (inter[dead]);
        if (!sum.empty ()) printf ("depois i %d maior %lld\n\n", i, *sum.rbegin ());
        else printf ("depois i %d maior %lld\n", i, 0);

        inter[dead] = 0;

        int ant, dep;
        ant = *gone.find (gone.find (l) - 1);
        dep = *gone.find (gone.find (l) + 1);
        if (dep - ant >= 0) {
            printf ("l\n");
            pii nw = pii (ant, dep);
            int val = acc[dep] - acc[ant-1];
            printf ("ff %d ss %d val %lld\n", nw.ff, nw.ss, val);

            inter[nw] = val;
            sum.insert (val);
        }
        ant = *gone.find (gone.find (r) - 1);
        dep = *gone.find (gone.find (r) + 1);
        if (dep - ant >= 0) {
            printf ("l\n");
            pii nw = pii (ant, dep);
            int val = acc[dep] - acc[ant-1];
            printf ("ff %d ss %d val %lld\n", nw.ff, nw.ss, val);

            inter[nw] = val;
            sum.insert (val);
        }

        if (!sum.empty ()) {
            printf ("i %d size %d\n", i, sum.size ());
            ans = *sum.rbegin ();
        }
        else ans = 0;
        printf ("%lld\n", ans);
    }
}
Пример #27
0
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;
}
Пример #28
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);
}