Mike and distribution
题意:
给定 $n$ 个物品,每个物品有两个属性$a_i$, $b_i$,求一个长度为$[\frac{n}{2}]+1$的子序列 $p$ 使得
$2 * \sum_{i = 1}^{|p|}{a_{p_i}} > \sum_{i=1}^n {a_i}$
$2 * \sum_{i = 1}^{|p|}{b_{p_i}} > \sum_{i=1}^n {b_i}$
解法:
非常巧妙的构造方法。
注意到题目的要求可以转化为非 $p_i$ 的集合的元素之和小于 $p_i$内元素之和。
考虑一种构造方法:
首先按照$a_i$大小将物品从大到小进行排序。
首先选上第一个物品,接下来对于后面的物品分成 $[\frac{n-1}{2}]$ 个两对物品来看,
对于每一对物品选取$b_i$较大的物品加入$p$集合。
如果余下了一个物品,则加入$p$集合。
正确性:
首先对于$b$,显然有原题要求条件成立。
对于$a$,可以发现 $a_1$ 大于后面所有 $a_{i+1} - a_i$ 的和,这样,也成立。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> #define N 100010 using namespace std; int n;
int a[N], p[N], b[N], c[N]; bool cmp(int x, int y)
{
return a[x] > a[y];
} int main()
{
scanf("%d", &n);
for(int i = ;i <= n;i++) scanf("%d", &a[i]);
for(int i = ;i <= n;i++) scanf("%d", &b[i]);
for(int i = ;i <= n;i++) c[i] = i;
sort(c + , c + n + , cmp);
int tot = ;
p[tot = ] = c[];
for(int i = ;i <= n;i += )
{
if(i<n && b[ c[i] ] < b[ c[i+] ]) p[++tot] = c[i+];
else p[++tot] = c[i];
}
sort(p + , p + tot + );
cout << tot << endl;
for(int i = ;i <= tot;i++) cout << p[i] << ' ';
cout << endl;
return ;
}
Mike and distribution的更多相关文章
- codeforces 798 D. Mike and distribution
D. Mike and distribution time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- #410div2D. Mike and distribution
D. Mike and distribution time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- D. Mike and distribution 首先学习了一个玄学的东西
http://codeforces.com/contest/798/problem/D D. Mike and distribution time limit per test 2 seconds m ...
- Codeforces 798D Mike and distribution(贪心或随机化)
题目链接 Mike and distribution 题目意思很简单,给出$a_{i}$和$b_{i}$,我们需要在这$n$个数中挑选最多$n/2+1$个,使得挑选出来的 $p_{1}$,$p_{2} ...
- CF410div2 D. Mike and distribution
/* CF410div2 D. Mike and distribution http://codeforces.com/contest/798/problem/D 构造 题意:给出两个数列a,b,求选 ...
- CF798D Mike and distribution
CF798D Mike and distribution 洛谷评测传送门 题目描述 Mike has always been thinking about the harshness of socia ...
- Codeforces 798D Mike and distribution - 贪心
Mike has always been thinking about the harshness of social inequality. He's so obsessed with it tha ...
- 【算法系列学习】codeforces D. Mike and distribution 二维贪心
http://codeforces.com/contest/798/problem/D http://blog.csdn.net/yasola/article/details/70477816 对于二 ...
- Mike and distribution CodeForces - 798D (贪心+思维)
题目链接 TAG: 这是我近期做过最棒的一道贪心思维题,不容易想到,想到就出乎意料. 题意:给定两个含有N个正整数的数组a和b,让你输出一个数字k ,要求k不大于n/2+1,并且输出k个整数,范围为1 ...
- Codeforces 798D Mike and distribution
题目链接 题目大意 给定两个序列a,b,要求找到不多于个下标,使得对于a,b这些下标所对应数的2倍大于所有数之和. N<=100000,所有输入大于0,保证有解. 因为明确的暗示,所以一定找个. ...
随机推荐
- 关于ASP.NET MVC中Response.Redirect和RedirectToAction的BUG (跳转后继续执行后面代码而不结束进程)以及处理方法
关于ASP.NET MVC中Response.Redirect和RedirectToAction的BUG (跳转后继续执行后面代码而不结束进程)以及处理方法 在传统的ASP.NET中,使用Resp ...
- 不使用flash实现复制文字(图片)到剪贴板
<div>这里是待复制的文字或图片</div> var range = document.createRange(); var referenceNode = document ...
- MVC上传多张图片
改变上传文件的按钮样式: <div id="post-upload-image"> <div id="divfile_-1"> < ...
- PythonCookBook笔记——数据编码和处理
数据编码和处理 主要涉及用Python处理不同方式编码的数据,如CSV.JSON.XML和二进制包装记录. 读写CSV数据 使用csv库. import csv with open('stocks.c ...
- spring 过滤器简介
spring 过滤器简介 过滤器放在容器结构的什么位置 过滤器放在web资源之前,可以在请求抵达它所应用的web资源(可以是一个Servlet.一个Jsp页面,甚至是一个HTML页面)之前截获进入的请 ...
- This means that only a small number of nodes must be read from disk to retrieve an item.
http://cis.stvincent.edu/html/tutorials/swd/btree/btree.html Introduction A B-tree is a specialized ...
- DuiLib笔记之设置文本字体
设置文本字体要用到Font 它的常用属性如下 id 用于标识Font,类型:INT name 用于指定字体名称,类型:STRING size 用于指定字体大小,类型:INT bold 用于指定是否加粗 ...
- No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=x86_64, VALID_ARCHS=i386).错误解决方法
targets ->build setting 下的 Build Active Architecture Only 设置 NO 就可以.
- 解决Error:Unable to find method 'org.gradle.api.internal.project.ProjectInternal.
错误描述今天在Github上面下载了一份代码,然后导入到Android Studio中直接报错误 错误描述如下: Error: Unable to find method ‘org.gradle.ap ...
- uboot之logo显示【转】
本文转载自:http://blog.csdn.net/tuwenqi2013/article/details/60583735 版权声明:本文为博主原创文章,博主欢迎各位转载. 一.logo的调用流程 ...