B. Lorry
time limit per test

2 seconds

memory limit per test

64 megabytes

input

standard input

output

standard output

A group of tourists is going to kayak and catamaran tour. A rented lorry has arrived to the boat depot to take kayaks and catamarans to the point of departure. It's known that all kayaks are of the same size (and each of them occupies the space of 1 cubic metre), and all catamarans are of the same size, but two times bigger than kayaks (and occupy the space of 2 cubic metres).

Each waterborne vehicle has a particular carrying capacity, and it should be noted that waterborne vehicles that look the same can have different carrying capacities. Knowing the truck body volume and the list of waterborne vehicles in the boat depot (for each one its type and carrying capacity are known), find out such set of vehicles that can be taken in the lorry, and that has the maximum total carrying capacity. The truck body volume of the lorry can be used effectively, that is to say you can always put into the lorry a waterborne vehicle that occupies the space not exceeding the free space left in the truck body.

Input

The first line contains a pair of integer numbers n and v (1 ≤ n ≤ 105; 1 ≤ v ≤ 109), where n is the number of waterborne vehicles in the boat depot, and v is the truck body volume of the lorry in cubic metres. The following n lines contain the information about the waterborne vehicles, that is a pair of numbers ti, pi (1 ≤ ti ≤ 2; 1 ≤ pi ≤ 104), where ti is the vehicle type (1 – a kayak, 2 – a catamaran), and pi is its carrying capacity. The waterborne vehicles are enumerated in order of their appearance in the input file.

Output

In the first line print the maximum possible carrying capacity of the set. In the second line print a string consisting of the numbers of the vehicles that make the optimal set. If the answer is not unique, print any of them.

Examples
input
3 2
1 2
2 7
1 3
output
7
2

解题思路:设t=1的载具为a,t=2的载具为b。
     将a和b的两种载具存储在两个数组nua,nub里面,长度分别为lea,leb,按p从大到小排序。
     如果v是奇数则减一(如果lea不为0,从nua中选出第一个,我是为了方便后面两个a与一个b的比较)
     否则进入循环,将两个a与一个b的元素进行比较。
     最后如果还剩下1v,则补上一个a。
     (代码写的好乱啊)
这题有个数据坑了我好久。
3 1
2 1
2 2
2 3

 1 #include <cstdio>
2 #include <cstring>
3 #include <algorithm>
4 #include <iostream>
5 #include <cmath>
6 using namespace std;
7 const int maxn = 100005;
8 struct nod {
9 int num;
10 int p;
11 }nua[maxn],nub[maxn];
12 bool cmp(nod a,nod b) {
13 if(a.p>b.p) return 1;
14 return 0;
15 }
16 int pu[maxn];
17 string s;
18 int main() {
19 ios::sync_with_stdio(false);
20 int n,v;
21 cin>>n>>v;
22 int lea=0,leb=0,cnt=0;
23 for(int i=1;i<=n;i++) {
24 int a,b;
25 cin>>a>>b;
26 if(a==1) {
27 nua[++lea].p=b;
28 nua[lea].num=i;
29 }
30 else {
31 nub[++leb].p=b;
32 nub[leb].num=i;
33 }
34 }
35 sort(nua+1,nua+lea+1,cmp);
36 sort(nub+1,nub+leb+1,cmp);
37 int i=1,j=1;
38 int ans=0;
39 if(lea && (v&1)) {
40 --v;
41 pu[++cnt]=nua[i].num;
42 ans+=nua[i++].p;
43 // cout<<1<<endl;
44 }
45 // cout<<v<<endl<<endl;;
46 while(i<=lea&&v>0) {
47 int u=nua[i].p+nua[i+1].p;
48 if(u>=nub[j].p) {
49 ans+=u;
50 pu[++cnt]=nua[i].num; i++;
51 if(i<=lea) {
52 pu[++cnt]=nua[i].num;
53 i++;
54 }
55
56 }
57 else {
58 ans+=nub[j].p;
59 pu[++cnt]=nub[j].num;
60 // cout<<2<<endl;
61 j++;
62 }
63 v-=2;
64 }
65 while(j<=leb&&v>1) {
66 ans+=nub[j].p;
67 pu[++cnt]=nub[j].num;
68 j++;
69 v-=2;
70 }
71 cout<<ans<<endl;
72 for( i=1;i<=cnt;i++) cout<<pu[i]<<" ";
73 return 0;
74 }

因为上面我的思路太乱了,第二天我看了看别人的思路,重做此题。

新思路:设t=1的载具为a,t=2的载具为b.依然分成nua,nub两组,按p从大到小排序。从大到小,能放多少b就放多少,如果v还有剩余,就放a。

然后把放进去的b载具从小到大遍历,每一个b与每两个a的p的和作比较,如果两a之和大于b,就更新此处。

思路很简单,代码也很好模拟。

 1 #include <cstdio>
2 #include <cstring>
3 #include <algorithm>
4 #include <iostream>
5 #include <cmath>
6 using namespace std;
7 const int maxn = 100005;
8 struct nod {
9 int num;
10 int p;
11 }nua[maxn],nub[maxn];
12 bool cmp(nod a,nod b) {
13 if(a.p>b.p) return 1;
14 return 0;
15 }
16 int pu[maxn];
17 string s;
18 int main() {
19 ios::sync_with_stdio(false);
20 int n,v;
21 cin>>n>>v;
22 int lea=0,leb=0,cnt=0;
23 for(int i=1;i<=n;i++) {
24 int a,b;
25 cin>>a>>b;
26 if(a==1) {
27 nua[++lea].p=b;
28 nua[lea].num=i;
29 }
30 else {
31 nub[++leb].p=b;
32 nub[leb].num=i;
33 }
34 }
35 sort(nua+1,nua+lea+1,cmp);
36 sort(nub+1,nub+leb+1,cmp);
37
38 int i=1,j=1;
39 int ans=0;
40 while(j<=leb && v>=2) { //先存t=2的载具
41 v-=2;
42 j++;
43 }
44 while(i<=lea && v>=1) { //v有剩余,就存t=1
45 --v;
46 i++;
47 }
48 int l=0; //记录有哪些个b被取代
49 for(int k=j-1;k>=1;k--) { //放进去的b从小到大遍历
50 int u=nua[i].p+nua[i+1].p;
51 if(u>nub[k].p) { //每两个a与一个b比较
52 if(i+1<=lea) {
53 i+=2;
54 }
55 else {
56 ++i;
57 break;
58 }
59 ++l;
60 }
61 }
62 j-=l;
63 for(int k=1;k<j;k++) {
64 ans+=nub[k].p;
65 pu[++cnt]=nub[k].num;
66 }
67 for(int k=1;k<i;k++) {
68 ans+=nua[k].p;
69 pu[++cnt]=nua[k].num;
70 }
71 sort(pu+1,pu+cnt+1);
72 cout<<ans<<endl;
73 for(int i=1;i<=cnt;i++) {
74 cout<<pu[i]<<" ";
75 }
76 return 0;
77 }

codeforce 3C的更多相关文章

  1. Codeforce - Street Lamps

    Bahosain is walking in a street of N blocks. Each block is either empty or has one lamp. If there is ...

  2. Codeforce Round #216 Div2

    e,还是写一下这次的codeforce吧...庆祝这个月的开始,看自己有能,b到什么样! cf的第二题,脑抽的交了错两次后过了pretest然后system的挂了..脑子里还有自己要挂的感觉,果然回头 ...

  3. 华为3C抢购难度

    上周小米2S降价到1299买了一个,今天突然想体验一下抢购红米和3C的难度.万一抢到了,拿到手机市场贵100块钱卖掉,然后可以请女神吃个饭~~~哈哈哈哈! 结果确实不怎么好抢.刚刚试了一下3C: 验证 ...

  4. 1.C#基础学习笔记3---C#字符串(转义符和内存存储无关)

    技术qq交流群:JavaDream:251572072  教程下载,在线交流:创梦IT社区:www.credream.com ------------------------------------- ...

  5. Codeforce 水题报告(2)

    又水了一发Codeforce ,这次继续发发题解顺便给自己PKUSC攒攒人品吧 CodeForces 438C:The Child and Polygon: 描述:给出一个多边形,求三角剖分的方案数( ...

  6. codeforce 375_2_b_c

    codeforce 375_2 标签: 水题 好久没有打代码,竟然一场比赛两次卡在边界条件上....跪 b.题意很简单...纯模拟就可以了,开始忘记了当字符串结束的时候也要更新两个值,所以就错了 #i ...

  7. codeforce 367dev2_c dp

    codeforce 367dev2_c dp 标签: dp 题意: 你可以通过反转任意字符串,使得所给的所有字符串排列顺序为字典序,每次反转都有一定的代价,问你最小的代价 题解:水水的dp...仔细想 ...

  8. 三维dp&codeforce 369_2_C

    三维dp&codeforce 369_2_C 标签: dp codeforce 369_2_C 题意: 一排树,初始的时候有的有颜色,有的没有颜色,现在给没有颜色的树染色,给出n课树,用m种燃 ...

  9. 强连通分量&hdu_1269&Codeforce 369D

    强连通分量 标签: 图论 算法介绍 还记得割点割边算法吗.回顾一下,tarjan算法,dfs过程中记录当前点的时间戳,并通过它的子节点的low值更新它的low,low值是这个点不通过它的父亲节点最远可 ...

随机推荐

  1. Http中的options请求

    引自:https://www.jianshu.com/p/5cf82f092201.https://www.cnblogs.com/mamimi/p/10602722.html 一.options是什 ...

  2. 通过js给某个标签添加内容或者删除标签

    添加内容 //先保存div中原来的html var tag = document.getElementById("tag").innerHTML; //构造新的内容 var cou ...

  3. HTTPS请求HTTP接口被浏览器阻塞,python实现websocket客户端,websocket服务器,跨域问题,dwebsocket,https,拦截,服务端

    HTTPS请求HTTP接口被浏览器阻塞,python实现websocket客户端,websocket服务器,跨域问题,dwebsocket,https,拦截,服务端 发表时间:2020-03-05 1 ...

  4. FPGA仿真的概念及语法特点

    以下是特权同学<FPGA设计+实战演练>书中的描述:      一个正规的设计需要花费在验证上的工作量,往往可能会占到整个开发流程的70%左右.验证通常分为仿真验证和板机验证.      ...

  5. 浅析Asp.Net Core框架IConfiguration配置

    目录 一.建造者模式(Builder Pattern) 二.核心接口与配置存储本质 三.简易QueryString配置源实现 四.宿主配置与应用配置 一.建造者模式 为什么提建造者模式?在阅读.NET ...

  6. SDNU_ACM_ICPC_2021_Winter_Practice_4th [个人赛]

    传送门 D - Odd Divisor 题意: 给你一个n,问你n是否至少有一个奇数因子(这里题意没说清,没说是不是只有一个还是可以有多个!AC以后才发现是不止一个 思路: 如果这个数没有奇数因子,那 ...

  7. oracle创建表并加索引

    一个语句创建Oracle所有表的序列 -- 动态创建序列 2 declare 3 cursor c_job is 4 select TABLE_NAME from user_tables; 5 6 c ...

  8. py, pyc, pyw, pyo, pyd Compiled Python File (.pyc) 和Java或.NET相比,Python的Virtual Machine距离真实机器的距离更远

    https://my.oschina.net/renwofei423/blog/17404 1.      PyCodeObject与Pyc文件 通常认为,Python是一种解释性的语言,但是这种说法 ...

  9. 【LinuxShell】ps 命令浅析

    前言 Linux上查看进程状态最常用的命令,本文对 ps 命令参数以及状态做一下简单介绍. 参数 ps a 显示现行终端机下的所有程序,包括其他用户的程序. ps -A 显示所有程序. ps c 列出 ...

  10. C#编写一个在asp.net core 3.1下的简单的corn模式的计划任务和一个更简单的定时器类

    asp.net core 下,新增了一个BackgroundService用来实现能在后台跑一个长久运行的任务,因此,也可以用来替换掉原来使用的static的Timer组件, Timer组件主要有以下 ...