这道题目网上有几个题解,均有问题。其实就是简单的贪心+排序,没必要做的那么复杂。
一旦tot+curv > v时,显然curv==2, 有三种可能:
(1)取出最小的curv==1的pp,装入当前的p;
(2)取出后续最大的curv==1的p,并且装入;
(3)当前已经是最优的(即后续不存在curv==1的类型),同时前一个的pp比当前的p更优。(这种情况不需要特判)

 /* 3B */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 typedef struct node_t {
int t, p, id;
friend bool operator< (const node_t& a, const node_t& b) {
if (a.p == b.p)
return a.t < b.t;
return a.p > b.p;
}
} node_t; const int maxn = 1e5+;
node_t nd[maxn]; int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int n, v; scanf("%d %d", &n, &v);
rep(i, , n+) {
scanf("%d %d", &nd[i].t, &nd[i].p);
nd[i].id = i;
if (nd[i].t == )
nd[i].p += nd[i].p;
} sort(nd+, nd++n); int i, j, k, p, pp = , pid, tot = ;
int ans = , tmp;
vi vc; i = ;
while (i <= n) {
if (nd[i].t == ) {
k = ;
p = nd[i].p>>;
pp = p;
pid = nd[i].id;
} else {
k = ;
p = nd[i].p;
} if (tot+k <= v) {
tot += k;
ans += p;
vc.pb(nd[i].id);
} else if (pp) {
// tot+k > v
// must be k == 2
// and pre has a 1
j = i+;
while (j<=n && nd[j].t!=)
++j; // no type 1 but may be pp < p (becase p is half if the nd[pid].p).
if (j > n) {
tmp = ;
} else {
tmp = nd[j].t>>;
}
// two way to solve
if (p-pp > tmp) {
// erase pid and add new id
for (vi::iterator iter=vc.begin(); iter!=vc.end(); ++iter) {
if (*iter == pid) {
vc.erase(iter);
break;
}
}
vc.pb(nd[i].id);
ans += p-pp;
} else if (tmp) {
// add nd[j].id;
vc.pb(nd[j].id);
ans += tmp;
} break;
} if (tot == v)
break;
++i;
} printf("%d\n", ans);
rep(i, , SZ(vc)) {
printf("%d ", vc[i]);
}
putchar('\n'); #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

【CF】3B Lorry的更多相关文章

  1. 【CF】438E. The Child and Binary Tree

    http://codeforces.com/contest/438/problem/E 题意:询问每个点权值在 $c_1, c_2, ..., c_m$ 中,总权值和为 $s$ 的二叉树个数.请给出每 ...

  2. 【CF】148D Bag of mice

    http://codeforces.com/problemset/problem/148/D 题意:w个白b个黑,公主和龙轮流取,公主先取,等概率取到一个.当龙取完后,会等概率跳出一只.(0<= ...

  3. 【CF】328 D. Super M

    这种图论题已经变得简单了... /* D */ #include <iostream> #include <string> #include <map> #incl ...

  4. 【CF】323 Div2. D. Once Again...

    挺有意思的一道题目.考虑长度为n的数组,重复n次,可以得到n*n的最长上升子序列.同理,也可以得到n*n的最长下降子序列.因此,把t分成prefix(上升子序列) + cycle(one intege ...

  5. 【CF】7 Beta Round D. Palindrome Degree

    manacher+dp.其实理解manacher就可以解了,大水题,dp就是dp[i]=dp[i>>1]+1如何满足k-palindrome条件. /* 7D */ #include &l ...

  6. 【CF】86 B. Petr#

    误以为是求满足条件的substring总数(解法是KMP分别以Sbeg和Send作为模式串求解满足条件的position,然后O(n^2)或者O(nlgn)求解).后来发现是求set(all vali ...

  7. 【CF】121 Div.1 C. Fools and Roads

    题意是给定一棵树.同时,给定如下k个查询: 给出任意两点u,v,对u到v的路径所经过的边进行加计数. k个查询后,分别输出各边的计数之和. 思路利用LCA,对cnt[u]++, cnt[v]++,并对 ...

  8. 【CF】310 Div.1 C. Case of Chocolate

    线段树的简单题目,做一个离散化,O(lgn)可以找到id.RE了一晚上,额,后来找到了原因. /* 555C */ #include <iostream> #include <str ...

  9. 【CF】110 Div.1 B. Suspects

    这题目乍眼一看还以为是2-sat.其实很水的,O(n)就解了.枚举每个人,假设其作为凶手.观察是否满足条件.然后再对满足的数目分类讨论,进行求解. /* 156B */ #include <io ...

随机推荐

  1. 20160328 javaweb Cookie 小练习

    利用cookie实现历史记录浏览: 由于是简单演示,所以直接用javabean 取代数据库了 数据存储类: package com.dzq.dao; import java.util.*; impor ...

  2. javaweb 中的乱码问题

    一.post 方式 首先我们看下面一段代码,在该HTML中我们指定的编码为“UTF-8”,如图所示. 在该代码中,我们将表单数据提交给ParamServlet 处理 servlet 会将接收到的数据打 ...

  3. ACM——进制转换

    http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1012 进制转换 时间限制(普通/Jav ...

  4. Solr4.7新建core

    Solr里面的core就像数据库里面的一个表,用来管理索引和相关配置. 一. 使用示例core 下载的solr完整包里面solr-4.7.0\example\multicore这个文件夹下面有2个示例 ...

  5. row_number() OVER (PARTITION BY COL1 ORDER BY COL2)

    select *,ROW_NUMBER() over(partition by deviceID order by RecordDate desc row_number() OVER (PARTITI ...

  6. vmstat,iostat,sar命令详解

    Procs r: 等待运行的进程数 b: 处在非中断睡眠状态的进程数 w: 被交换出去的可运行的进程数.此数由 linux 计算得出,但 linux 并不耗尽交换空间 Memory swpd: 虚拟内 ...

  7. 数据操作So easy-LINQ解析

    1.LINQ是什么? LINQ是Language Integrated Query的缩写,即“语言集成查询”的意思.LINQ的提出就是为了提供一种跨越各种数据源的统一的查询方式,它主要包含4个组件-- ...

  8. 10.26_地图应用, OSC_doc文档集合,node-webkit

    (1)地图:关于电子地图的加载.展示方式,知乎上有篇文章写的很好:http://www.zhihu.com/question/21530085对于地图的导航距离计算呢?原理是什么? (2)node-w ...

  9. UVA 11584 Paritioning by Palindromes(动态规划 回文)

    题目大意:输入一个由小写字母组成的字符串,你的任务是把它划分成尽量少的回文串.比如racecar本身就是回文串:fastcar只能分成7个单字母的回文串:aaadbccb最少可分成3个回文串:aaa. ...

  10. 配置iSCSI

    先查下yum list | grep iscsi, 存在iscsi包, 进行安装:yum install iscsi-initiator-utils.x86_64, cat /etc/iscsi/in ...