题目链接: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. IPC$入侵大全

    0x01  ipc$的定义 IPC$(Internet Process Connection)是共享"命名管道"的资源(大家都是这么说的),它是为了让进程间通信而开放的命名管道,可 ...

  2. libevent学习文档(三)working with event

    Events have similar lifecycles. Once you call a Libevent function to set up an event and associate i ...

  3. 手脱nSPack 2.1 - 2.5

    1.载入PEID 使用核心扫描出的结果 nSPack 2.1 - 2.5 -> North Star/Liu Xing Ping 2.载入OD,一进来就是一个大跳转,F8跟着走 >- E9 ...

  4. 效应量Effect Size

    sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&am ...

  5. Ant Design Upload 组件上传文件到云服务器 - 七牛云、腾讯云和阿里云的分别实现

    在前端项目中经常遇到上传文件的需求,ant design 作为 react 的前端框架,提供的 upload 组件为上传文件提供了很大的方便,官方提供的各种形式的上传基本上可以覆盖大多数的场景,但是对 ...

  6. LeetCode-Max Points on a Line[AC源码]

    package com.lw.leet3; import java.util.HashMap; import java.util.Iterator; import java.util.Map; imp ...

  7. 2-sat HDU 1814

    题解来自于:http://www.cnblogs.com/kuangbin/archive/2012/10/05/2712622.html 和平委员会 根据宪法,Byteland民主共和国的公众和平委 ...

  8. Python学习笔记(二十九)ThreadLocal

    import threading #创建全局ThreadLocal对象: local_school = threading.local() def process_student(): #获取当前线程 ...

  9. ⑤ 设计模式的艺术-05.原型(Prototype)模式

    场景 思考一下:克隆技术是怎么样的过程? 克隆羊多利大家还记得吗? javascript语言中的,继承怎么实现?那里面也有prototype,大家还记得吗? 原型模式 通过new产生一个对象需要非常繁 ...

  10. .NET面试题系列(一)基本概念

    什么是CLR CLR常用简写词语,CLR是公共语言运行库(Common Language Runtime)和Java虚拟机一样也是一个运行时环境,它负责资源管理(内存分配和垃圾收集等),并保证应用和底 ...