Beispiel #1
0
int main()
{
    //freopen("I.in","r",stdin);
    //freopen("I.out","w",stdout);
    int T;
    scanf("%d", &T);
    for(int cas=1;cas<=T;cas++)
    {
        mst.clear();
        int n, m, k, cnt = 0;
        long long  num;
        scanf("%d%d%d", &n, &m, &k);
        for(int i = 0; i < n; ++i)
        {
            scanf("%lld", &num);
            if(num <= k)
            {
                mst.insert(num);
            }
        }
        while(true)
        {
            int sz = mst.size();
            if(sz < 2)
                break;
            it1 = mst.end();
            it1--;
            long long  fst = *it1;
            mst.erase(it1);
            it2 = mst.lower_bound(min(fst, k - fst));
            if(it2 == mst.end())
                it2--;
            if(it2 == mst.begin() && *it2 > (k - fst))
                continue;
            if(*it2 <= k - fst)
            {
                ans[cnt++] = fst * ( *it2);
                mst.erase(it2);
            }
            else
            {
                it2 --;
                ans[cnt++] = fst * (*it2);
                mst.erase(it2);
            }
        }
        sort(ans, ans + cnt, cmp);
        long long  res = 0;
        for(int i = 0; i < min(m, cnt); ++i)
        {
            res += ans[i];
        }
        printf("CASE #%d: %lld\n",cas,res);
    }
    return 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;
}
Beispiel #3
0
Node AStar::getNodeFromSet( multiset<Node> l,Node n){

    multiset<Node> ::iterator it;
    for(it = l.begin();it!=l.end();it++){
        if(n.id == (*it).id){
            return *it;
        }

    }
}
Beispiel #4
0
int main(){
  int n;rit(n);
  for(int l,r;n;--n){
    rit(l,r);
    auto it=st.upper_bound(r);
    if(it!=st.end())st.erase(it);
    st.insert(l);
  }
  printf("%d\n",st.size());
}
Beispiel #5
0
	int update(int x){
		int u=V.back();
		int p=fa[u];
		if (p) D[p].erase(D[p].find(faW[u]+tree[0].mxR));
		ans.erase(ans.find(tree[0].mx));
		update_tree(0,n-1,at[x],0);
		ans.insert(tree[0].mx);
		if (p) D[p].insert(faW[u]+tree[0].mxR);
		return p;
	}
		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);
		}
Beispiel #7
0
void inline cut(set<int> &s, multiset<int> &ss, int t) {
    s.insert(t);
    auto l = s.find(t), r = l;
    --l, ++r;
    int cur = *r - *l;
    //cout<<"cur: "<<cur<<endl;
    ss.erase(ss.find(cur));
    ss.insert(*r - t);
    ss.insert(t - *l);
}
Beispiel #8
0
void scan(int i, int n, int k, ll sum){
    if(i>k){
        mys.erase(mys.find(sum));
        return;
    }
    for(int j=pos[i-1]; j<=n; ++j){
        pos[i]=j;
        scan(i+1,n,k,sum+a[j]);
    }
}
int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> t;
  while(t--){
    S.clear();
    cin >> n;
    for(int i=0;i<n;i++){
      cin >> x;
      it = S.upper_bound(x);
      if(it==S.end()){
        S.insert(x);
      }
      else{
        S.erase(it);
        S.insert(x);
        //(*it) = x;
      }
    }
    cout << S.size() << " ";
    for(multiset<int>::iterator it=S.begin();it!=S.end();it++){
      cout << (*it)  << " ";
    }
    cout << endl;
  }
  return 0;
}
Beispiel #10
0
int main() {
	freopen("b.in", "r", stdin);
	while (~scanf("%d %lld", &n, &p)) {
		s.clear();
		long long x;
		for (int i = 0; i < n; i++) {
			scanf("%lld", &x);
			arr[i] = x;
			s.insert(x);
		}
		long long ans = 0;
		for (int i = 0; i < n; i++) {
			long long now = arr[i];
			set<long long>::iterator it = s.lower_bound(now);

			s.erase(it);
			it = s.lower_bound(p - now);
			if (it != s.begin()) it--;
			ans = max(ans, (now + *it) % p);
			it = s.lower_bound(p + p - now);
			if (it != s.begin()) it--;
			ans = max(ans, (now + *it) % p);

			s.insert(now);
		}


		cout << ans << endl;
	}
	


	return 0;
}
Beispiel #11
0
int main()
{
    int n,d,r;
    while(scanf("%d%d%d",&n,&d,&r)!=EOF&&n||d||r)
    {
        int k;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&k);
            s1.insert(k);
        }
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&k);
            s2.insert(k);
        }
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            int num=*s1.begin();
            int index=d-num;
            __typeof(s2.begin()) it=s2.lower_bound(index);
            if(it==s2.end())it--;
            ans+=r*max(0,num+*it-d);
            s1.erase(s1.begin());
            s2.erase(it);
        }
        printf("%d\n",ans);
    }
    return 0;
}
Beispiel #12
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;
}
Beispiel #13
0
int main()
{
    srand(time(NULL));
    for(int i=6; i<21; ++i) {
        char fin[55],out[55];
        sprintf(fin,"teste/grader_test%d.in",i);
        sprintf(out,"teste/grader_test%d.ok",i);
        ofstream in(fin);
        ofstream g(out);
        n=rand()%DN+1;k=rand()%n+1;
        in<<n<<' '<<k<<'\n';
        for(int i=1; i<=n; ++i) {
            p[i]=rand()%DK+1; c[i]=rand()%DK+1;
            in<<p[i]<<' '<<c[i]<<'\n';
        }
        pt=0; rst.clear(); sol.clear(); ssol=0; rez=0;
        for(int i=1; i<=k; ++i) {
            pt+=p[i];
            rst.insert(p[i]+c[i]);
        }
        rez=max(rez,pt);
        for(int i=k+1; i<=n; ++i) {
            pt+=p[i];
            rst.insert(p[i]+c[i]);
            ssol+=*rst.begin();
            sol.insert(*rst.begin());
            rst.erase(rst.begin());
            bst[i]=pt-ssol;
            rez=max(rez,bst[i]);
        }
        g<<rez;
    }
    return 0;
}
Beispiel #14
0
multiset<Node,NodeCompare>::iterator FindNode(const multiset<Node, NodeCompare>& nodes, int i, int j)
{
   for (set<Node, NodeCompare>::iterator it = nodes.begin(); it != nodes.end(); ++it)
   {
      if (it->x == i && it->y == j) 
      { 
         return it;
      }
   }
   return nodes.end(); 
}
Beispiel #15
0
inline void modify(int u) {
    col[u] ^= true;
    sum += col[u] ? -1 : 1;
    while(u) {
        d[u] = getD(u);
        if(!col[u]) d[u] = make_pair(max(0, d[u].first), max(0, d[u].second));
        int val = tree[root[pathR[u]]].opt;ans.erase(ans.lower_bound(-val));
        modify(root[pathR[u]], pos[pathR[u]], pos[pathR[u]]+csz[pathR[u]]-1, u);
        adjust(pathR[u]), ans.insert(-tree[root[pathR[u]]].opt), u = fa[pathR[u]];
    }
}
Beispiel #16
0
void rec(int m, int idx, int at, long long sum) {
	if (at == k - 1) {
		s.erase(s.find(sum));
	} else {
		rec(m, idx, at + 1, sum + a[idx]);

		if (idx + 1 < m) {
			rec(m, idx + 1, at, sum);
		}
	}
}
Beispiel #17
0
int main(){
    int n;cin>>n;
    while(n--){
        int l,r;cin>>l>>r;
        auto it=st.upper_bound(r);
        if(it!=st.end())st.erase(it);
        st.insert(l);
    }
    cout<<st.size()<<endl;
    return 0;
}
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());
	}
}
Beispiel #19
0
bool AStar::findInSet( multiset<Node> l,Node n){

    multiset<Node> ::iterator it;
    for(it = l.begin();it!=l.end();it++){
        if(n.id == (*it).id){
            return true;
        }

    }

    return false;
}
Beispiel #20
0
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin >> n >> m;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        uf[i] = i;
        ms.emplace(a[i], i);
    }

    for (int i = 0; i < m; ++i) {
        int u, v;
        ll w;
        cin >> u >> v >> w;
        --u; --v;
        edges[i] = make_tuple(w, u, v);
    }
    sort(edges, edges + m);

    ll ans = 0LL;
    int cc = n;
    int ptr = 0;
    while (cc > 1) {
        ll ax, ay;
        int comp_a, comp_b;
        // find the two minimum as
        tie(ax, comp_a) = *ms.begin();
        tie(ay, comp_b) = *next(ms.begin());
        
        ll tot_comp = ax + ay;
        while (ptr < m and get<0>(edges[ptr]) <= tot_comp) {
            ll w;
            int u, v;
            tie(w, u, v) = edges[ptr];
            if (merge(u, v)) {
                --cc;
                ans += w;
            }

            ++ptr;
        }

        if (merge(comp_a, comp_b)) {
            --cc;
            ans += tot_comp;
        }
    }

    cout << ans << '\n';
    
    return 0;
}
Beispiel #21
0
int main() {
	multiset<int>::iterator it;
	while (scanf("%d", &n) != EOF) {
		while (!S.empty())
			S.pop();
		lef.clear();
		righ.clear();
		for (int i = 0; i < n; ++i) {
			scanf("%s", buf);
			if (strcmp(buf, "Pop") == 0) {
				if (S.empty()) {
					printf("Invalid\n");
					continue;
				}
				int u = S.top();
				S.pop();
				if (u <= med) {
					it = lef.find(u);
					lef.erase(it);

				}
				else {
					it = righ.find(u);
					righ.erase(it);
				}
				adjust();
				printf("%d\n", u);
			}
			else if (strcmp(buf, "PeekMedian") == 0) {
				if (S.empty()) {
					printf("Invalid\n");
					continue;
				}
				printf("%d\n", med);
			}
			else if (strcmp(buf, "Push") == 0) {
				int u;
				scanf("%d", &u);
				if (S.empty()) {
					med = u;
					lef.insert(u);
					
				}
				else if (u > med) {
					righ.insert(u);
				}
				else {
					lef.insert(u);
				}
				S.push(u);
				adjust();
			}
			else
				printf("Invalid\n");
		}
	}
	return 0;
}
Beispiel #22
0
void add(ll x){
    bool odd = is_odd();
    s.insert(x);

    if (s.size() == 1) median = s.begin();
    else if (*median == x) recalc();
    else {
        bool right = is_right(x);
        if (odd && !right) median--;
        if (!odd && right) median++;
    }
    print();
}
Beispiel #23
0
void debug()
{
      multiset<pair<int,int> > ::iterator it;
      puts("- - - - - - - - - -debug   begin-- - - - - - - - - - - - ");

      for(it=apple.begin();it!=apple.end();++it)
      {
            cout<<(*it).first<<" "<<(*it).second<<endl;
      }

      puts("- - - - - - - - - -debug   end-- - - - - - - - - - - - ");

}
Beispiel #24
0
Node AStar::getMinimumNode( multiset<Node> l){
    if(l.size()>=1){
        multiset<Node>::iterator it = l.begin();
#if DEBUG
        printf("Minimum f_score node is %lld \n", (*it).id);
#endif
        return *it;
    }
    else{
        printf("Set is empty, no nodes left to expand");
        //exit(0);
    }
}
bool TSP::is_BiASGS_goal( const search_node& s, const multiset<search_node, less<search_node> >& frontier )
{
  multiset<search_node>::iterator it;
  for ( it = frontier.begin(); it != frontier.end(); it++ )
  {
    if ( s.get_state() == (*it).get_state() )
    {
      return true;
    }
  }
    
  return false;
}
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 #27
0
int two_array(multiset<int> &a, multiset<int> &b, int k)
{
	for(int i : a)
	{
		multiset<int>::iterator it = b.lower_bound(k-i);
		if(it == b.end())
			return 0;	
		
		b.erase(it);
	}
	
	return 1;
}
Beispiel #28
0
inline void Process(int l, int r) {
	top = 0;
	for (int i = l; i <= r; i++) {
		for (int j = i + 1; j <= r; j++) {
			Intersect(a[i], a[j], i, j);
		}
	}
	for (int i = 1; i <= m; i++) {
		if (c[i].tl <= l && c[i].tr >= r) {
			stack[++top].type = 1, stack[top].id = i, stack[top].w = c[i].l;
			stack[++top].type = 1, stack[top].id = i, stack[top].w = c[i].r;
		}
	}
	stack[++top].type = 0; stack[top].w = inf;
	stack[++top].type = 0; stack[top].w = -inf;
	sort(stack + 1, stack + top + 1);
	assert(stack[1].w == -inf);
	assert(stack[top].w == inf);

	// Sweep
	NOW = -inf; S.clear();
	for (int i = l; i <= r; i++) fim[i] = S.insert(Para(a[i].a, a[i].b, a[i].c, i));
	int next = 1;
	for (int i = 2; i < top;) {
		while (sgn(stack[next].w - stack[i].w) < 1) next++;
		T.clear();
		for (int j = i; j < next; j++) if (stack[j].type == 0) {
			T.insert(stack[j].x);
			T.insert(stack[j].y);
		}
		for (set<int>::iterator it = T.begin(); it != T.end(); ++it)
			S.erase(fim[*it]);
		NOW = (stack[i].w + stack[next].w) / 2.0;
		for (set<int>::iterator it = T.begin(); it != T.end(); ++it)
			fim[*it] = S.insert(Para(a[*it].a, a[*it].b, a[*it].c, *it));

		fim[0] = S.end(); fim[0]--; double tmp = fim[0]->getVal(stack[i].w);
		for (int j = i; j < next; j++) if (stack[j].type == 1) {
			Ans[stack[j].id] = max(Ans[stack[j].id], tmp);
		}
		i = next;
	}

	// Segmetn Tree
	data = POOL; root = Build(0, L);
	for (int i = l; i <= r; i++) root->Insert(a[i].w, a[i].top);
	for (int i = 1; i <= m; i++) if (c[i].tl <= l && c[i]. tr >= r) {
		double tmp = root->Cal(c[i].l, c[i].r);
		Ans[i] = max(Ans[i], tmp);
	}
}
int main()
{
    int num;
    while(cin>>num&&num)
    {
        int key;
        vec.clear();
        for(int i=0;i<num;i++)
        {
            cin>>key;
            vec.insert(key);
        }
        int sum=0;
        int x,y,s;
        while(vec.size()>=2)
        {
            multiset<int>::iterator it1=vec.begin();
            x=*it1;
            vec.erase(it1);
            multiset<int>::iterator it2=vec.begin();
            y=*it2;
            vec.erase(it2);
            sum=sum+x+y;
            vec.insert(x+y);
        }
        cout<<sum<<endl;
    }
    return 0;
}
Beispiel #30
0
void solve(int idx)
{
	if(flag)
		puts("");
	flag=1;
	scanf("%d",&N);
	printf("Case #%d:\n", idx);
	Ms.clear();
	while(N--)
	{
		int L,R;
		scanf("%d %d",&L,&R);
		Point tmp;
		tmp.L=L;tmp.R=R;
		it=Ms.lower_bound(tmp);
		if(it==Ms.begin()||(--it)->R>R)
		{
			Ms.insert(tmp);
			it=Ms.upper_bound(tmp);
			while(it!=Ms.end()&&it->R>=tmp.R)
			{
				Ms.erase(it++);
			}
		}
		printf("%d\n",Ms.size());
	}
}