这道题目网上有几个题解,均有问题。其实就是简单的贪心+排序,没必要做的那么复杂。
一旦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. android studio环境搭建-笔记1

    自己干了几年测试(功能性的),最近比较闲,就自己学习下android(以前也有所接触,但那是几年前的一点皮毛,都忘记了). 先搭建谷歌推出的android studio(以前用eclipse搭建总觉得 ...

  2. JavaScript之call()&apply()

    场景一:定义了一个类A,给它一个getName的方法:定义了一个类B,给它一个setName的方法:之前A只需要获取它的Name,B也只需要设置它的Name,但现在有新的需求,A和B都需要设置和获取他 ...

  3. Eclipse搭建服务器

    一.首先,依次点击Window -->preferences-->Server-->Runtime environment -->add,再选择Apache,选择TOMcat的 ...

  4. WildFly 9.0.2+mod_cluster-1.3.1 集群配置

    一.配置背景 最近使用WildFly 9.0.2作为中间件开发系统,给客户不熟的时候需要使用集群来进行负载均衡,一开始想到是使用Nginx.但是只通过Nginx使用 ip_hash 模式没有做到ses ...

  5. 三、使用Maven构建简单的java项目

    前边,我刚搭建了Maven环境,还有给大家推荐了学习资源,这个小节,我们来就来,,简单的玩玩maven. 1.所需工具: 1.Eclipse     2.apache-maven-3.3.9   3. ...

  6. 关于block块使用的6点注意事项

    对于刚学习OC新伙伴,block块一直都是一个比较纠结.比较难懂的知识点,不过,在使用一段时间后,就会感觉很酸爽.block块的原理及使用我就不再赘述,网上有很多这方面的资料.我个人使用这么长时间以来 ...

  7. ES6的编码风格

    编程风格 [转自http://es6.ruanyifeng.com/#docs/style] 块级作用域 字符串 解构赋值 对象 数组 函数 Map结构 Class 模块 ESLint的使用 本章探讨 ...

  8. EntityClient 介绍

    System.Data.EntityClient 命名空间是 实体框架的 .NET Framework 数据提供程序.EntityClient 提供程序使用存储特定的 ADO.NET 数据提供程序类和 ...

  9. 一次ora-1113 记录

    记录博客园的第一天,今天在电脑前发呆,突然感觉自己记忆越来越差,近年来随着工作力度的加强,感觉自己越来越力不从心,问题重复的出现.感觉自己应该去记录点什么了,随选择了用写博客的方式记录一下.第一天先记 ...

  10. [JS]Cookie精通之路

    [JS]Cookie精通之路 转http://blog.163.com/neu_pdh1983/blog/static/572407020077310528915/ 发布:Cary 媒体:www.Ju ...