CodeForces - 798D Mike and distribution(构造+思维/玄学随机数)
题目链接:点击查看
题目大意:给出两个长度为n的数列,现在要求选出n/2+1个位置,使得两个序列中这些位置的和分别大于各自序列之和的一半
题目分析:题意换句话说,是需要让我们从数组中选出一半,要大于另一半,这样我们可以以数组a为基准先从大到小排序,然后依次对于每两个相邻的b比较大小,选择较大的那个输出,因为此时数组a已经是降序排列的了,只需要让数组b满足条件就好了
比较让我惊艳到的是随机数的玄学做法,直接用STL里的random_shuffle函数将原序列随机排列,直到前n/2+1个数满足条件输出即可
代码:
排序:
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cmath>
#include<sstream>
#include<unordered_map>
using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=1e5+100;struct Node
{int a,b,id;bool operator<(const Node& t)const{return a>t.a;}
}q[N];int main()
{
// freopen("input.txt","r",stdin);
// ios::sync_with_stdio(false);int n;scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d",&q[i].a);q[i].id=i;}for(int i=1;i<=n;i++)scanf("%d",&q[i].b);sort(q+1,q+1+n);printf("%d\n",n/2+1);printf("%d ",q[1].id);for(int i=2;i<=n;i+=2){if(q[i].b>q[i+1].b)printf("%d ",q[i].id);elseprintf("%d ",q[i+1].id);}return 0;
}
随机数:
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cmath>
#include<sstream>
#include<unordered_map>
using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=1e5+100;struct Node
{int a,b,id;
}q[N];int main()
{
// freopen("input.txt","r",stdin);
// ios::sync_with_stdio(false);int n;scanf("%d",&n);LL suma=0,sumb=0;for(int i=1;i<=n;i++){scanf("%d",&q[i].a);suma+=q[i].a;q[i].id=i;}for(int i=1;i<=n;i++){scanf("%d",&q[i].b);sumb+=q[i].b;}while(1){random_shuffle(q+1,q+1+n);LL tempa=0,tempb=0;for(int i=1;i<=n/2+1;i++){tempa+=q[i].a;tempb+=q[i].b;}if(tempa*2>suma&&tempb*2>sumb){printf("%d\n",n/2+1);for(int i=1;i<=n/2+1;i++)printf("%d ",q[i].id);return 0;}}return 0;
}
CodeForces - 798D Mike and distribution(构造+思维/玄学随机数)相关推荐
- Codeforces 798D Mike and distribution (构造)
题目链接 http://codeforces.com/contest/798/problem/D 题解 前几天的模拟赛,居然出这种智商题..被打爆了QAQ 这个的话,考虑只有一个序列怎么做,把所有的排 ...
- 【CodeForces - 798D】Mike and distribution (思维构造,贪心,黑科技)
题干: Mike has always been thinking about the harshness of social inequality. He's so obsessed with it ...
- CodeForces - 1265D Beautiful Sequence(贪心+构造+思维)
题目链接:点击查看 题目大意:给出a个0,b个1,c个2,d个3,要求构造一种序列,使得数列两两之间绝对值之差等于1,若不能构造输出NO 题目分析:首先我们需要稍微讨论一下特殊情况,那就是对于两端的数 ...
- Mike and distribution(思维)
Mike has always been thinking about the harshness of social inequality. He's so obsessed with it tha ...
- CF Round410 D. Mike and distribution
D. Mike and distribution 构造法 798D - Mike and distribution In the beginning, it's quite easy to notic ...
- CodeForces 798D 思维,贪心
CodeForces 798D 题意:长度为 n的两个数组 a[]和 b[],要找出 k ( k<=n/2+1 )个下标,使得在两个数组中这 k个数的和乘上 2 要大于所有数的和. tags: ...
- CodeForces - 798B Mike and strings
B. Mike and strings time limit per test2 seconds memory limit per test256 megabytes inputstandard in ...
- Codeforces Round #410 (Div. 2) D. Mike and distribution 思维+数学
链接: http://codeforces.com/contest/798/problem/D 题意: 给你两个长度为n的数列a和b,让你选n/2+1个下标,使得2*∑ai>suma,2*∑bi ...
- codeforces 148 C. Terse princess(思维,构造)
C. Terse princess(思维,构造) 题目链接:codeforces 148C 题意: 有个公主找王子,如果这个人比之前所有王子的财富都高,公主就会Oh, ...
最新文章
- android 控件id为0,Android Studio错误:(3,0)未找到ID为“com.android.application”的插件...
- Linux System Programming -- Appendix
- C++ Primer 5th笔记(chap 19 特殊工具与技术)运行时类型识別RTTI
- 在Linux 5/6上使用UDEV SCSI规则配置ASM DISK
- com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
- Ajax发送formdata数据,SpringMVC后台处理
- Swift WKWebView读取本地html
- mysql主从配置错误_mysql主从配置失败,主从通讯失败
- golang后端php前端,Golang如何接收前端的参数
- 导航菜单UI设计中的作用,优秀案例临摹起来!
- Angular Material design设计
- table表格某一td内容太多导致样式混乱的解决方案
- Eigen3的库使用
- 「1024 程序员节」各大公司和程序员们都是怎么过的?你都做了哪些计划或安排?
- 关于linux下内存使用的一些疑惑
- mysql校验规则居民身份证号_身份证号规则及校验
- python中随机函数import random作用_python学习之随机函数random
- 【已解决】如何让压缩率达到最大?使用lrzip工具进行文件压缩(好用)
- OPENGL 简单图形绘制
- SpringBoot笔记(四)其他框架介绍