关闭

CF 3B Lorry

标签: codeforces
211人阅读 评论(0) 收藏 举报
分类:

sort + greedy 

为了满足各种判断条件 这样做应该是比较优的

全都用1的 全都用2的 1 2混合使用的

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
const int MAX = 1e5 + 1000;

struct bb
{
    int t,p,num;
} kay[MAX],aca[MAX];
int cmp(const bb & a,const bb & b)
{
    return a.p > b.p;
}
int n,v,num[MAX],sum_num;
int main()
{
    while(~scanf("%d%d",&n,&v))
    {
        int t,p;
        int num_kay = 0,num_aca = 0;
        for(int i = 0; i < n; i ++)
        {
            scanf("%d%d",&t,&p);
            if(t%2 == 0)
            {
                aca[num_aca].t = t;
                aca[num_aca].p = p;
                aca[num_aca].num = i + 1;
                num_aca ++;
            }
            else
            {
                kay[num_kay].t = t;
                kay[num_kay].p = p;
                kay[num_kay].num = i + 1;
                num_kay ++;
            }
        }
        sort(kay,kay+num_kay,cmp);
        sort(aca,aca+num_aca,cmp);
        LL sum1[MAX],sum2[MAX];
        sum1[0] = 0;
        sum2[0] = 0;
        for(int i = 1; i <= num_kay; i ++)
            sum1[i] = sum1[i-1] + (LL)kay[i-1].p;
        for(int i = 1; i <= num_aca; i ++)
            sum2[i] = sum2[i-1] + (LL)aca[i-1].p;

        int h1 = 0,h2 = 0,p1,p2;
        LL ans = 0,sum = 0;
        for(int i = 0; i <= num_kay; i ++)
        {
            if(i > v) break;
            int rest = v - i;
            if(rest >= num_aca*2) p2 = num_aca;
            else p2 = rest /2;
            sum = sum1[i] + sum2[p2];
            if(sum > ans)
            {
                ans = sum;
                h1 = i;
                h2 = p2;
            }
        }
        cout<<ans<<endl;
        sum_num = 0;
        for(int i = 0;i < h2;i ++)
        {
            num[sum_num ++] = aca[i].num;
        }
        for(int i = 0;i < h1;i ++)
        {
            num[sum_num ++] = kay[i].num;
        }
        sort(num,num+sum_num);
        for(int i = 0;i < sum_num;i ++)
            cout<<num[i]<<" ";
            cout<<endl;


    }
    return 0;
}
/**
  3 2
  1 2
  2 7
  1 3
  sum1  3 2
  sum2  7
  4 3
  1 4
  2 6
  1 1
  1 1.
  sum1 4 1 1
  sum2 6
  2 2
  1 4
  2 6
  sum1 4
  sum2 6
*/


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:15295次
    • 积分:900
    • 等级:
    • 排名:千里之外
    • 原创:79篇
    • 转载:4篇
    • 译文:0篇
    • 评论:0条
    文章分类