题目链接:http://codeforces.com/problemset/problem/798/D

题目大意:从长度为n的序列A和序列B中分别选出k个下表相同的数要求,设这两个序列中k个数和分别为ta,tb,两个序列总和分别为asum,bsum。要求ta*2>asum&&tb*2>bsum,k<=n/2+1.

解题思路:首先,肯定要选n/2+1的吧。我们把序列A当成第一层,序列B当成第二层。第一层:按从大到小严格排好。在第二层上:每两个取大的那一个,那最后的和肯定>=总和的一半,而且再加上第一个,所以所选数的和和>总和的一半。

 #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=1e6+; struct node {
int a;
int b;
int num;
}t[N]; bool cmp(node x,node y){
return x.a>y.a;//第一层按从大到小排好
} int main(){
ios::sync_with_stdio(false);
int n;
cin>>n;
for(int i=;i<=n;i++){
cin>>t[i].a;
}
for(int i=;i<=n;i++){
cin>>t[i].b;
t[i].num=i;
}
sort(t+,t++n,cmp);
cout<<n/+<<endl;
cout<<t[].num<<" ";
for(int i=;i<=n;i+=){
cout<<(t[i].b>t[i+].b?t[i].num:t[i+].num)<<" ";//两两取较大的那个
}
cout<<endl;
return ;
}

还有一种利用random_shuffle随机排列来写的,就是每次排好都选前k个,总会选到符合条件的答案,玄学。。。。:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<time.h>
using namespace std;
const int N=1e6+;
typedef long long LL;
struct node {
int a;
int b;
int num;
}t[N]; int main(){
ios::sync_with_stdio(false);
int n;
cin>>n;
LL asum=,bsum=;
for(int i=;i<=n;i++){
cin>>t[i].a;
asum+=t[i].a;
}
for(int i=;i<=n;i++){
cin>>t[i].b;
t[i].num=i;
bsum+=t[i].b;
}
cout<<n/+<<endl;
srand(time(NULL));
while () {
random_shuffle(t+, t+n+);
LL ta=,tb=;
for(int i=;i<=n/+;i++){
ta+=t[i].a;
tb+=t[i].b;
}
if (*ta>asum&&*tb>bsum) {
for(int i=;i<=n/+;i++){
cout<<t[i].num<<" ";
}
cout<<endl;
return ;
}
}
return ;
}

Codeforces 798D - Mike and distribution(二维贪心、(玄学)随机排列)的更多相关文章

  1. 【算法系列学习】codeforces D. Mike and distribution 二维贪心

    http://codeforces.com/contest/798/problem/D http://blog.csdn.net/yasola/article/details/70477816 对于二 ...

  2. Codeforces 798D Mike and distribution - 贪心

    Mike has always been thinking about the harshness of social inequality. He's so obsessed with it tha ...

  3. Codeforces 798D Mike and distribution(贪心或随机化)

    题目链接 Mike and distribution 题目意思很简单,给出$a_{i}$和$b_{i}$,我们需要在这$n$个数中挑选最多$n/2+1$个,使得挑选出来的 $p_{1}$,$p_{2} ...

  4. CodeForces - 798D Mike and distribution 想法题,数学证明

    题意:给你两个数列a,b,你要输出k个下标,使得这些下标对应的a的和大于整个a数列的和的1/2.同时这些下标对应的b //题解:首先将条件换一种说法,就是要取floor(n/2)+1个数使得这些数大于 ...

  5. Codeforces 798D Mike and distribution (构造)

    题目链接 http://codeforces.com/contest/798/problem/D 题解 前几天的模拟赛,居然出这种智商题..被打爆了QAQ 这个的话,考虑只有一个序列怎么做,把所有的排 ...

  6. Codeforces 798D Mike and distribution

    题目链接 题目大意 给定两个序列a,b,要求找到不多于个下标,使得对于a,b这些下标所对应数的2倍大于所有数之和. N<=100000,所有输入大于0,保证有解. 因为明确的暗示,所以一定找个. ...

  7. Codeforces 869E. The Untended Antiquity (二维Fenwick,Hash)

    Codeforces 869E. The Untended Antiquity 题意: 在一张mxn的格子纸上,进行q次操作: 1,指定一个矩形将它用栅栏围起来. 2,撤掉一个已有的栅栏. 3,询问指 ...

  8. CodeForces 242E - XOR on Segment 二维线段树?

    今天练习赛的题....又是线段树的变换..拿到题我就敲了个点更新区间查询的..果断超时...然后想到了可以将每个数与合表示成不进位的二进制数..这样就可以区间进行更新了..比赛的时候写搓了..刚重写了 ...

  9. Codeforces 1080C 题解(思维+二维前缀和)

    题面 传送门 题目大意: 有一个黑白的棋盘,现在将棋盘上的一个子矩形全部染成黑色,另一个子矩形全部染成白色 求染完色后黑,白格子的总数 分析 我们可以发现,对于一个(1,1)到(x,y)的矩形,若xy ...

随机推荐

  1. Docker学习笔记一:如何在线安装

    一.Docker简介: Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源.Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后 ...

  2. nginx php fastcgi Connection reset by peer的原因及解决办法

    Connection reset by peer 这个错误是在nginx的错误日志中发现的,为了更全面的掌握nginx运行的异常,强烈建议在nginx的全局配置中增加 error_log   logs ...

  3. Linux基础--------监控系统、进程管理、软件包管理-------free、dd、kill、 rpm、yum、源码安装python

    作业一:1) 开启Linux系统前添加一块大小为15G的SCSI硬盘 2) 开启系统,右击桌面,打开终端 3) 为新加的硬盘分区,一个主分区大小为5G,剩余空间给扩展分区,在扩展分区上划分1个逻辑分区 ...

  4. restful风格请求及都是 / 的请求及参数也在请求的/中

    前台请求的样式: http://localhost:8080/item/88909 其中参数就是最后的 商品id号  88909 后台Controller中取出参数的方法: @Controller p ...

  5. django2.0 uwsgi nginx

    [TOC]# 1.安装pip```sudo apt-get updatesudo apt-get install python-pip```# 2.使用pip 安装virtualenv 和 virtu ...

  6. Ubuntu配置vncserver

    https://help.aliyun.com/knowledge_detail/59330.html 首先,安装桌面环境和vnc4server: sudo apt-get install gnome ...

  7. hadoop 安装教程

    本教程由厦门大学数据库实验室 / 给力星出品,转载请注明.本教程适合于原生 Hadoop 2,包括 Hadoop 2.6.0, Hadoop 2.7.1 等版本,主要参考了官方安装教程,步骤详细,辅以 ...

  8. 利用Zynq Soc创建一个嵌入式工程

    英文题目:Using the Zynq SoC Processing System,参考自ADI的ug1165文档. 利用Zynq Soc创建一个嵌入式工程,该工程总体上包括五个步骤: 步骤一.新建空 ...

  9. 用js实现千位分隔符

    function mm(num) { return num && num .toString() .replace(/(\d)(?=(\d{3})+\.)/g, function($0 ...

  10. Codeforces 221 B. Little Elephant and Numbers

    B. Little Elephant and Numbers time limit per test 2 seconds memory limit per test 256 megabytes inp ...