Beispiel #1
0
int main()
{
    int n, m, a;

    while ( scanf("%d", &n), n )
    {
        long long ans = 0LL;
        urn.clear();
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &m);
            for (int j = 0; j < m; j++)
            {
                scanf("%d", &a);
                urn.insert( a );
            }
            ans += *urn.rbegin() - *urn.begin();
            urn.erase( urn.begin() );
            urn.erase( urn.find(*urn.rbegin()) );
        }
        printf("%lld\n", ans);
    }

    return 0;
}
int main()
{
	int n;
	while (scanf("%d", &n) != EOF)
	{
		if (n==0) break;
		LL ans = 0;
		num.clear();
		for (int i = 0; i < n; i++)
		{
			int k;
			scanf("%d", &k);
			for (int j = 0; j < k; j++)
			{
				int x;
				scanf("%d", &x);
				num.insert(x);
			}
			ans += *num.rbegin() - *num.begin();
			num.erase(num.find(*num.begin()));
			num.erase(num.find(*num.rbegin()));
		}
		printf("%I64d\n", ans);
	}
	return 0;
}
int main() {
    int n, elm, k, t;
    cin >> n >> t >> k;
    for (int i = 0; i < n; ++i) {
        cin >> elm;
        v.push_back(elm);
    }
    for (int i = 0; i < k; ++i) {
        S.insert(v[i]);
    }
    it = S.rbegin();
    ans.push_back(*it);
    for (int i = k; i < n; ++i) {
        S.erase(S.find(v[i - k]));
        S.insert(v[i]);
        it = S.rbegin();
        ans.push_back(*it);
    }
    int res = 0;
    for (int i = 0; i < ans.size(); ++i) {
        if (ans[i] <= t)
            ++res;
    }
    cout << res;
    return 0;
}
 // Returns the median of current data stream
 double findMedian() {
     if (first.size()> second.size()) {
         return *(first.rbegin());
     }
     double x = *first.rbegin();
     double y = *second.begin();
     return (x+y)/2;
 }
void median_ele()
{
	//cout<<*s1.rbegin()<<endl;
	//printf("%d\n",(*s1.rbegin()));
	dint(*s1.rbegin());
	s1.erase(s1.find(*s1.rbegin()));
	if(s1.size()<s2.size())
	{
		s1.insert(*s2.begin());
		s2.erase(s2.begin());
	}
}
void add_ele(int x)
{
	if(s1.size()==s2.size())
	{
		s2.insert(x);
		s1.insert(*s2.begin());
		s2.erase(s2.begin());
	}
	else if(s1.size()>s2.size())
	{
		s1.insert(x);
		s2.insert(*s1.rbegin());
		s1.erase(s1.find(*s1.rbegin()));
	}
}
Beispiel #7
0
int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  for (int i = 0; i < n; ++i) {
    scanf("%d", a+i);
    b[i] = a[i];
  }
  sort(a, a+n);
  for (int i = 0; i < n; ++i) {
    val.insert(a[i]);
    if (i)
      dif.insert(a[i] - a[i-1]);
  }
  while (q--) {
    int t;
    scanf("%d", &t);
    if (t == 2) {
      long long ans = *val.rbegin() - *val.begin();
      ans -= *dif.begin();
      printf("%lld\n", ans);
    }
    else {
      int x, y;
      scanf("%d %d", &x, &y);
      --x;
      if (b[x] != y) {
        del(b[x]);
        add(b[x] = y);
      }
    }
  }
  return 0;
}
Beispiel #8
0
int main() {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i].first);
    }
    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i].second);
        tot += a[i].second;
    }
    sort(a, a + n);
    int ans = ~0U >> 1;
    for (int i = 0; i < n; i++) {
        int j = i, sum = a[i].second;
        while (j + 1 < n && a[j+1].first == a[i].first)
            sum += a[++j].second;
        int cnt = j - i + 1;
        multiset<int>::reverse_iterator it = st.rbegin();
        for (int k = 0; k < cnt - 1 && it != st.rend(); k++, it++) {
            sum += *it;
        }
        ans = min(ans, tot - sum);
        for (int k = i; k <= j; k++) {
            st.insert(a[k].second);
        }
        i = j;
    }
    printf("%d\n", ans);
    return 0;
}
vector<bool> Mochila(const multiset<Elemento> & elementos, double m) {
    Nodo inic = NodoInicial(elementos, m);
    double C = Greedy01(elementos,m);
    priority_queue<Nodo> LNV;
    LNV.push(inic);
    double s = numeric_limits<double>::min();
    vector<bool> resultado;

    multiset<Elemento>::const_reverse_iterator raux = elementos.rbegin();

    while (!LNV.empty()) {
        Nodo x = (LNV.top());
        LNV.pop();
        if (x.CS >= C) {
            for (unsigned k = 0; k < 2; k++) {
                bool elec = (k==0) ? true : false;
                Nodo y = Generar(x, elec, m,elementos);
                if (y.nivel == elementos.size()-1 && y.valor_actual > s) {
                    s = y.valor_actual;
                    C = (C >= s) ? C : s;
                    resultado = y.tupla;
                }

                else if (y.nivel < elementos.size()-1 && y.CS >= C){
                    C = (C >= y.CI) ? C : y.CI;
                    LNV.push(y);
                }
            }
        }
        ++raux;
    }
    return resultado;
}
Beispiel #10
0
int main(int argc, char *argv[]) {
   std::ios_base::sync_with_stdio(false);
   int n, k;
   cin >> n >> k;
   for(int i = 0; i < n; ++i)
      cin >> a[i];
   S.insert(a[0]);
   int ma = 0, lo = 0, hi = 1;
   while(true) {
      if(*S.rbegin() - *S.begin() <= k) {
         if(hi - lo > ma) {
            ma = hi - lo;
            ans.clear();
         }
         if(hi - lo == ma) 
            ans.push_back(lo);
         if(hi == n) 
            break;
         S.insert(a[hi++]);
      } else {
         S.erase(S.find(a[lo++]));
      }
   }
   cout << ma << ' ' << ans.size() << endl;
   for(int i = 0; i < (int)ans.size(); ++i)
      cout << ans[i] + 1 << ' ' << ans[i] + ma << endl;
   return 0;
}
Beispiel #11
0
int main(int argc, char const *argv[])
{
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%I64d", A + i);
        s += A[i];
        ps[i + 1] = ps[i] + A[i];
    }
    sum.insert(s);
    qu.insert(Pii(n, 0));
    for (int i = 0, p; i < n; ++i) {
        scanf("%d", &p); --p;
        auto it = qu.upper_bound(Pii(p, 0));
        // cout << it->second << " " << it->first << endl;
        ll tmp = ps[it->first] - ps[it->second];
        // cout << "tmp " << tmp << endl;
        sum.erase(sum.lower_bound(tmp));

        tmp = ps[p] - ps[it->second];
        // cout << "tmp " << tmp << endl;
        sum.insert(tmp); qu.insert(Pii(p, it->second));

        tmp = ps[it->first] - ps[p + 1];
        // cout << "tmp " << tmp << endl;
        sum.insert(tmp); qu.insert(Pii(it->first, p + 1));

        qu.erase(it);
        printf("%I64d\n", *sum.rbegin());
    }
    return 0;
}
Beispiel #12
0
	void encodeAllSymbols(){
		if (treeSet.empty()) {
			return;
		}

		string pattern;
		HuffmanNode* root = *(treeSet.rbegin());
		_encodeAllSymbols(pattern, root);
	}
		void pushup() {
			if (this == EMPTY)	return ;
			mxv = val;
			if (s.size())
				mxv = max(mxv, *s.rbegin());
			if (ch[0] != EMPTY)
				mxv = max(mxv, ch[0]->mxv);
			if (ch[1] != EMPTY)
				mxv = max(mxv, ch[1]->mxv);
		}
 void balance() {
     if (right.size() > left.size() + 1) {
         left.insert(*right.begin());
         right.erase(right.begin());
     }
     if (left.size() > right.size()) {
         right.insert(*left.rbegin());
         left.erase(--left.end());
     }
 }
Beispiel #15
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++;
}
Beispiel #16
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;
		}
		
		
	}
}
Beispiel #17
0
ll insert(){
	//cout << "Inserting " _ curr	 << endl;
	full.insert(B[curr]);
	val += A[curr];
	ttime += B[curr++];
	if(half.size() < w){
		auto x = *full.rbegin();
		auto it = full.end(); it--;
		half.insert(x);
		full.erase(it);
		ttime -= (x) / 2 ;
	}
}
double Greedy01(const multiset<Elemento> & objetos, const double pesoM){
    double pesoact = 0, beneficio = 0;

    multiset<Elemento>::const_reverse_iterator it;

    for(it = objetos.rbegin(); it != objetos.rend() && pesoact < pesoM; ++it)
        if((pesoact + (*it).peso) <= pesoM){
            beneficio += (*it).beneficio;
            pesoact += (*it).peso;
        }

    return beneficio;
}
Beispiel #19
0
int main() {
  int n; scanf( "%d", &n ); 
  for( int i = 0; i < n; ++i ) {
    int x, y, h;
    scanf( "%d%d%d", &x, &y, &h );
    v.push_back( event( x+0, h, +1 ) );
    v.push_back( event( y+1, h, -1 ) );
  }
  insert(); 
  insert();

  sort( v.begin(), v.end() ); 

  int curr = 0, st = 0, ed, sol = 0;

  s.insert( 0 );
  for( int i = 0; v[i].type != 2; ++i ) {
    if( v[i].type == +1 ) s.insert( v[i].height );
    else s.erase( s.find(v[i].height) );
    curr = *s.rbegin();
    st = i+1;
  }

  for( ed = st+1; v[ed].type != 2; ++ed );
  
  for( int i = st+1, j; i < ed; i = j ) {
    for( j = i; j < ed && v[i].position == v[j].position; ++j )
      if( v[j].type == +1 ) s.insert( v[j].height );
      else s.erase( s.find(v[j].height) );

    sol += abs( curr - *s.rbegin() );
    curr = *s.rbegin();
  }

  printf( "%d\n", sol );

  return 0;
}
Beispiel #20
0
int main()
{
	int t = SI;
	ull ret = 0;
	till( t )
	{
		int n = SI;
		till( n ) s.insert( SI );
		int lo = *s.begin() , hi = *s.rbegin();
		ret += ( hi - lo );
		s.erase( s.begin() );
		s.erase( s.find( hi ) );
	}
	printf( "%llu\n" , ret );
	return 0;
}
int main() {
	// your code goes here
	int n,q,u,v,ans;
	scanf("%d%d",&n,&q);
	ms(n);
	while(q--)
	{
		scanf("%d%d",&u,&v);
		uni(u,v);
		rit=se.rbegin();
		it=se.begin();
		ans=*rit-*it;
		printf("%d\n",ans);
	}
	return 0;
}
Beispiel #22
0
int main()
{
	scanf("%d",&N);
	for(int i=0;i<N;i++)
	{
		scanf("%lld %lld %lld",&A[i],&B[i],&H[i]);
		vx.pb(A[i]);
		vx.pb(B[i]);
	}
	vx.pb(0);
	sort(vx.begin(),vx.end());
	vx.erase(unique(vx.begin(),vx.end()),vx.end());
	for(int i=0;i<N;i++)
	{
		A[i]=lower_bound(vx.begin(),vx.end(),A[i])-vx.begin();
		B[i]=lower_bound(vx.begin(),vx.end(),B[i])-vx.begin();
		in.pb(P((int)A[i],H[i]));
		out.pb(P((int)B[i],H[i]));
	}
	sort(in.begin(),in.end());
	sort(out.begin(),out.end());
	ll ans = 0ll;
	s.insert(0ll);
	int in_id=0,out_id=0;
	for(int i=1;i<vx.size();i++)
	{
		ans += (vx[i]-vx[i-1])*(*s.rbegin());
		//printf("%lld %lld %lld\n",vx[i-1],vx[i],*s.rbegin());
		while(in_id<N&&in[in_id].first==i)
		{
			s.insert(in[in_id].second);
			//printf("insert %lld\n",in[in_id].second);
			in_id++;
		}
		while(out_id<N&&out[out_id].first==i)
		{
			multiset<ll>::iterator it;
			it = s.find(out[out_id].second);
			s.erase(it);
			//printf("erase %lld\n",out[out_id].second);
			out_id++;
		}
	}
	printf("%lld\n",ans);
	return 0;
}
Beispiel #23
0
inline i64 get(int cnt) {
    if (cnt > (int)S1.size() + (int)S2.size())
        return inf;
    while((int)S1.size() < cnt) {
        int x = *S2.begin();
        S2.erase(S2.begin());
        S1.insert(x);
        sum += x;
    }
    while(cnt < (int)S1.size()) {
        int val = *S1.rbegin();
        sum -= val;
        S1.erase(S1.find(val));
        S2.insert(val);
    }
    return sum;
}
double GreedyFraccional(const double lim_peso, const multiset<Elemento> & objetos){
    double pesoact = 0, beneficio = 0;

    multiset<Elemento>::const_reverse_iterator it;

    for(it = objetos.rbegin(); it != objetos.rend() && pesoact < lim_peso; ++it){
        if((pesoact + (*it).peso) <= lim_peso){
            beneficio += (*it).beneficio;
            pesoact += (*it).peso;
        }
        else {
            double fraccion = (lim_peso-pesoact)/(*it).peso;
            beneficio += fraccion * (*it).beneficio;
            pesoact = lim_peso;
        }
    }

    return beneficio;
}
 // 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());
     }
 }
Beispiel #26
0
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;
}
Beispiel #27
0
	~HuffmanTree() {
		delete *(treeSet.rbegin());
	}
Beispiel #28
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;
}
Beispiel #29
0
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);

	int w,h,n;
	char c[10];
	int t;
	int temp;

	scanf("%d %d %d",&w,&h,&n);

	Sh1.insert(0);
	Sh1.insert(h);
	Sw1.insert(0);
	Sw1.insert(w);

	Sh2.insert(h);
	Sw2.insert(w);

	while(n--)
	{
		scanf("%s %d",c,&t);

		//cout<<*Sh2.rbegin()<<endl;

		if(c[0]=='H')
		{
			Sh1.insert(t);
			iter=Sh1.find(t);
			iter++;
			temp=*iter;
			iter--;
			iter--;
			temp=temp-*iter;

			iter1=Sh2.find(temp);

			Sh2.erase(iter1);
			Sh2.insert(t-*iter);
			iter++;
			iter++;
			Sh2.insert(*iter-t);
		}
		else
		{
			Sw1.insert(t);
			iter=Sw1.find(t);

			iter++;
			temp=*iter;
			iter--;
			iter--;
			temp=temp-*iter;

//			cout<<"@"<<temp<<endl;

			iter1=Sw2.find(temp);

			Sw2.erase(iter1);
			Sw2.insert(t-*iter);
			iter++;
			iter++;
			Sw2.insert(*iter-t);
		}

	//	temp=*Sh2.rbegin();
	//	cout<<temp<<' ';
	//	temp=*Sw2.rbegin();
	//	cout<<temp<<endl;
//		cout<<Sh2.rbegin()<<endl<<Sw2.rbegin()<<endl;

		cout<<((long long)(*Sh2.rbegin()))*(*Sw2.rbegin())<<endl;
	}

	return 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);
    }
}