VK Cup 2012 Qualification Round 2--E. Zebra Tower 离散化+优先队列

E. Zebra Tower
time limit per test
1.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Janet likes playing with cubes. Actually, she likes to play with anything whatsoever, cubes or tesseracts, as long as they are multicolored. Each cube is described by two parameters — color ci and size si. A Zebra Tower is a tower that consists of cubes of exactly two colors. Besides, the colors of the cubes in the tower must alternate (colors of adjacent cubes must differ). The Zebra Tower should have at least two cubes. There are no other limitations. The figure below shows an example of a Zebra Tower.

A Zebra Tower's height is the sum of sizes of all cubes that form the tower. Help little Janet build the Zebra Tower of the maximum possible height, using the available cubes.

Input

The first line contains an integer n (2 ≤ n ≤ 105) — the number of cubes. Next n lines contain the descriptions of the cubes, one description per line. A cube description consists of two space-separated integers ci and si (1 ≤ ci, si ≤ 109) — the i-th cube's color and size, correspondingly. It is guaranteed that there are at least two cubes of different colors.

Output

Print the description of the Zebra Tower of the maximum height in the following form. In the first line print the tower's height, in the second line print the number of cubes that form the tower, and in the third line print the space-separated indices of cubes in the order in which they follow in the tower from the bottom to the top. Assume that the cubes are numbered from 1 to n in the order in which they were given in the input.

If there are several existing Zebra Towers with maximum heights, it is allowed to print any of them.

Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cincout streams or the%I64d specificator.

Sample test(s)
input
4
1 2
1 3
2 4
3 3
output
9
3
2 3 1 
input
2
1 1
2 1
output
2
2

2 1

解决方案:这题算是蛮简单的啦,先是离散化颜色值,然后处理出s颜色的方块用n个的最大值F[s][n],然后对整个颜色进行遍历,并用一个f[n]数组来维护前面的颜色中选用n个的方块的最大值。然后就是当前颜色选出n个和f[n],f[n+1],f[n-1]匹配,筛选出最大值,其中我们还要记录结果,写起来是有点烦。

code:

#include <iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<queue>
#include<vector>
using namespace std;
const int maxn=100005;
map<long long ,long long>exit;
map<long long ,long long>rexit;
struct fnode
{
    long long value;
    int color;
} fun[maxn];
struct node
{
    friend bool operator<(node a,node b)
    {
        return a.size_<b.size_;
    }
    int index;
    long long size_;
};
int n;
vector<long long> F[maxn];
priority_queue<node> Q[maxn];
priority_queue<node> Save[maxn];
int main()
{
    while(~scanf("%d",&n))
    {
        long long c,s;
        exit.clear();
        rexit.clear();
        for(int i=0; i<=n; i++)
        {
            fun[i].value=0;
            F[i].clear();
            while(!Q[i].empty()) Q[i].pop();
            while(!Save[i].empty()) Save[i].pop();
        }
        long long index=1;
        for(int i=1; i<=n; i++)
        {
            node temp;
            scanf("%I64d%I64d",&c,&s);
            if(!exit[c])
            {
                exit[c]=index;
                rexit[index]=c;
                temp.size_=s;
                temp.index=i;
                Q[index].push(temp);
                Save[index++].push(temp);
            }
            else
            {
                temp.size_=s;
                temp.index=i;
                Q[exit[c]].push(temp);
                Save[exit[c]].push(temp);
            }
        }
        for(int i=1; i<index; i++)
        {
            long long sum=0;
            while(!Q[i].empty())
            {
                sum+=(Q[i].top()).size_;
                Q[i].pop();
                F[i].push_back(sum);
            }
        }
        int len=F[1].size();
        for(int i=0; i<len; i++)
        {
            fun[i+1].value=F[1][i];
            fun[i+1].color=rexit[1];
        }
        long long Max=0;
        int rescolor1,rescolor2;
        long long rescnt1,rescnt2;
        for(int i=2; i<index; i++)
        {
            int len=F[i].size();
            for(int l=0; l<len; l++)
            {
                if(Max<fun[l+1].value+F[i][l]&&l+1<=n)
                {
                    rescolor1=rexit[i];
                    rescnt1=l+1;
                    rescolor2=fun[l+1].color;
                    rescnt2=l+1;
                    Max=fun[l+1].value+F[i][l];
                }
                if(l>0)
                {
                    if(Max<fun[l].value+F[i][l])
                    {
                        rescolor1=rexit[i];
                        rescnt1=l+1;
                        rescolor2=fun[l].color;
                        rescnt2=l;
                        Max=fun[l].value+F[i][l];
                    }
                }
                if(Max<fun[l+2].value+F[i][l]&&l+2<=n)
                {
                    rescolor1=rexit[i];
                    rescnt1=l+1;
                    rescolor2=fun[l+2].color;
                    rescnt2=l+2;
                    Max=fun[l+2].value+F[i][l];
                }
            }
            for(int k=0; k<len; k++)
            {
                if(fun[k+1].value<F[i][k]){
                   fun[k+1].value=F[i][k];
                   fun[k+1].color=rexit[i];
                }
            }
        }
        printf("%I64d\n",Max);
        printf("%I64d\n",rescnt1+rescnt2);
       // cout<<rescnt1<<" "<<rescnt2<<endl;
        if(rescnt1==rescnt2)
        {
            int ff=0;
            while(1)
            {
                int ind=(Save[exit[rescolor1]].top()).index;
                Save[exit[rescolor1]].pop();
                printf("%d",ind);
                printf(" ");
                ind=(Save[exit[rescolor2]].top()).index;
                Save[exit[rescolor2]].pop();
                printf("%d",ind);
                ff++;
                if(ff==rescnt1) break;
                printf(" ");
            }
            printf("\n");
        }
        else if(rescnt1>rescnt2)
        {
           // cout<<Save[exit[rescolor1]].size()<<endl;
          ///  cout<<rescolor2<<" | "<<Save[exit[rescolor2]].size()<<endl;
            int ind=(Save[exit[rescolor1]].top()).index;
            Save[exit[rescolor1]].pop();
            printf("%d",ind);
            printf(" ");
            rescnt1--;
            int ff=0;
            while(1)
            {
                int ind=(Save[exit[rescolor2]].top()).index;
                Save[exit[rescolor2]].pop();
                printf("%d",ind);
                printf(" ");
                ind=(Save[exit[rescolor1]].top()).index;
                Save[exit[rescolor1]].pop();
                printf("%d",ind);
                ff++;
                if(ff==rescnt1) break;
                printf(" ");
            }
            printf("\n");
        }
        else if(rescnt1<rescnt2)
        {
            int ind=(Save[exit[rescolor2]].top()).index;
            Save[exit[rescolor2]].pop();
            printf("%d",ind);
            printf(" ");
            rescnt2--;
            int ff=0;
            while(1)
            {
                int ind=(Save[exit[rescolor1]].top()).index;
                Save[exit[rescolor1]].pop();
                printf("%d",ind);
                printf(" ");
                ind=(Save[exit[rescolor2]].top()).index;
                Save[exit[rescolor2]].pop();
                printf("%d",ind);
                ff++;
                if(ff==rescnt1) break;
                printf(" ");
            }
            printf("\n");
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值