题干:

Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, b2, ..., bn] of length n each which he uses to ask people some quite peculiar questions.

To test you on how good are you at spotting inequality in life, he wants you to find an "unfair" subset of the original sequence. To be more precise, he wants you to select k numbers P = [p1, p2, ..., pk] such that 1 ≤ pi ≤ n for 1 ≤ i ≤ k and elements in P are distinct. Sequence P will represent indices of elements that you'll select from both sequences. He calls such a subset P "unfair" if and only if the following conditions are satisfied: 2·(ap1 + ... + apk) is greater than the sum of all elements from sequence A, and 2·(bp1 + ... + bpk) is greater than the sum of all elements from the sequence B. Also, k should be smaller or equal to  because it will be to easy to find sequence P if he allowed you to select too many elements!

Mike guarantees you that a solution will always exist given the conditions described above, so please help him satisfy his curiosity!

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of elements in the sequences.

On the second line there are n space-separated integers a1, ..., an (1 ≤ ai ≤ 109) — elements of sequence A.

On the third line there are also n space-separated integers b1, ..., bn (1 ≤ bi ≤ 109) — elements of sequence B.

Output

On the first line output an integer k which represents the size of the found subset. k should be less or equal to .

On the next line print k integers p1, p2, ..., pk (1 ≤ pi ≤ n) — the elements of sequence P. You can print the numbers in any order you want. Elements in sequence Pshould be distinct.

Example

Input

5
8 7 4 8 3
4 2 5 3 7

Output

3
1 4 5

题目大意:

麦克有两个只包含正整数的数组 A = [a1, a2, ..., an] 和 B = [b1, b2, ..., bn] ,长度都为 n 。

他现在想要选出 k 个数 P = [p1, p2, ..., pk] 满足 1 ≤ pi ≤ n 并且数组 P 中的数字互不相等。要求P数组满足: 2·(ap1 + ... + apk) 比数组 A 的和大,并且 2·(bp1 + ... + bpk) 比数组 B的和大。当然, k 必须小于等于   ,否则 P 数组太容易选出来了。

请你给出一个合法的方案。

解题报告:

这题显然要贪心,,但是贪心还是有技巧的。。首先我们发现选择的数量当然是越多越好,所以我们肯定要选满n/2+1个数。

预处理一波,读入的时候a和b捆绑读入,,因为他俩要选就是一起选。。这很套路了不说了。

首先按照a数组排序,,然后把所有的数字分成两组,,转化问题变成:使得选出的数分别在两个数组中的和,大于剩下没有选的数的和。我们发现这只是个充分条件,,并不是充分必要条件,也就是说条件加强了 ,但是我们发现对于这道题加强这一步条件后恰好可以构造出一组解。

我们将其两两相邻的数分成一组,每次从这一组中取出一个就行了,所以我们贪心处理只要拿b值较大的那一个即可,因为a肯定是满足递减的也就是不管我们这两个拿哪一个a,都可以干掉下一组的选择的那个a。

这就很美滋滋了,,仔细观察可以发现,其实对于a数组是跨组贪心的,,也就是我们是这一组取一个值,让他大于下一组的值;对于b数组是本组贪心的,,也就是我们选择的这一个  一定是本组的最优。

所以对于这种贪心策略,我们只需要再找一个大于第一组的选择的a就行了(因为不然没有人压过他),对于这种特例我们这样处理:排好序后第一个结构体一定是我们选择的,然后从第二个元素开始往后分组,,这样就有一个好处那就是可以保证后面的贪心一定是正确的,并且我们也有一个元素可以压过第一组的a。。所以贪心结束。写代码就完事了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
struct Node {int a,b;int pos;
} node[MAX];
bool cmp (Node a,Node b) {if(a.a != b.a) return a.a > b.a;else return a.b > b.b;
}
int ans[MAX];
int main()
{int n;cin>>n;for(int i = 1; i<=n; i++) scanf("%d",&node[i].a),node[i].pos = i;for(int i = 1; i<=n; i++) scanf("%d",&node[i].b);sort(node+1,node+n+1,cmp);int tot = 0;ans[++tot] = node[1].pos;for(int i = 2; i<=n; i+=2) {if(node[i].b >= node[i+1].b) ans[++tot] = node[i].pos;else ans[++tot] = node[i+1].pos;}//if(n%2==0) ans[++tot] = n;printf("%d\n",tot);for(int i = 1; i<=tot; i++) printf("%d%c",ans[i],i == tot ? '\n' : ' ');return 0 ;}

总结:

这题思维很巧妙啊。。分成两组,,将问题转化成一个充分条件的问题去进行求解,因为这样方便我们构造贪心。还是要从条件入手啊,,告诉你选n/2个数了,分析一下,,也只能分成两组去考虑 这么做了。

20191005陈题重做

再来介绍一种黑科技:

random_shuffle(R+1,R+n+1);可以在O(n)的时间内获得一个随机排列。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int n,up;
struct Node {int a,b,id;
} R[MAX];
ll suma,sumb;
bool ok() {ll ta = 0,tb = 0;for(int i = 1; i<=up; i++) {ta += R[i].a;tb += R[i].b; }if(ta*2>suma && tb*2>sumb) return 1;else return 0;
}
int main()
{cin>>n;for(int i = 1; i<=n; i++) cin>>R[i].a,R[i].id=i,suma += R[i].a;for(int i = 1; i<=n; i++) cin>>R[i].b,sumb += R[i].b;up = n/2+1;printf("%d\n",up);while(1) {random_shuffle(R+1,R+n+1);if(ok()) break;}for(int i = 1; i<=up; i++) printf("%d ",R[i].id);return 0 ;
}

【CodeForces - 798D】Mike and distribution (思维构造,贪心,黑科技)相关推荐

  1. CodeForces - 798D Mike and distribution(构造+思维/玄学随机数)

    题目链接:点击查看 题目大意:给出两个长度为n的数列,现在要求选出n/2+1个位置,使得两个序列中这些位置的和分别大于各自序列之和的一半 题目分析:题意换句话说,是需要让我们从数组中选出一半,要大于另 ...

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

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

  3. 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 ...

  4. 51nod 1574 || Codeforces 584 E. Anton and Ira 思维+构造+贪心

    传送门:E. Anton and Ira 题意:给定两个1-n的全排列p和s,假设交换pi和pj的代价是abs(i-j),问怎样将p交换成s才能使代价最小. 思路:先将s映射成1-n的顺序排列,再将p ...

  5. CodeForces - 1504C Balance the Bits(思维+构造)

    题目链接:https://vjudge.net/problem/CodeForces-1504C 题目大意:给出一个长度为 nnn 的 010101 串,现在要求构造出两个长度为 nnn 的合法括号序 ...

  6. CodeForces - 1348C Phoenix and Distribution(思维)

    题目分析:点击查看 题目大意:给出一个字符串 s 以及 k 个集合,要求将字符串 s 中的字符分配到 k 个集合中,使得: k 个集合都是非空集合 k 个集合中字典序最大的字符串最小 输出这个字典序最 ...

  7. CodeForces - 618B Guess the Permutation(思维+构造)

    题目链接:点击查看 题目大意:先给出一个长度为n的序列ai,这个序列是1~n全排列中的其中一种,再给出一个n*n的矩阵,maze[i][j]=val代表min(ai,aj)=val,要求我们构造出原始 ...

  8. Educational Codeforces Round 53C(二分,思维|构造)

    #include<bits/stdc++.h> using namespace std; const int N=1e6+6; int x[N],y[N]; int sx,sy,n; ch ...

  9. CodeForces - 1450C2 Errich-Tac-Toe (Hard Version)(思维+构造)

    题目链接:点击查看 题目大意:给出一个大小为 n * m 的棋盘,规定不能有三个连续的 ' X ' 或三个连续的 ' O ' ,现在可以通过一次操作将 ' X ' 改成 ' O ' 或者将 ' O ' ...

  10. CodeForces - 1304D Shortest and Longest LIS(构造+贪心)

    题目链接:点击查看 题目大意:题目给出 n - 1 个大小关系,分别代表一个长度为 n 的数列各个位置的相对大小,现在需要我们用两个符合相对大小关系的 1 ~ n 的一个排列,且该排列的最长不下降子序 ...

最新文章

  1. 计算机网络概述---传输层 UDP和TCP
  2. VR原理讲解及开发入门
  3. Delphi FastReport动态加载图片
  4. 小米面试题:单词搜索
  5. 前端框架Bootstrap 教程
  6. 7月15号day7总结
  7. Mysql能删了重装吗_mysql卸载重新安装
  8. ReportViewer教程(9)-给报表增加页打印日期编号
  9. LeetCode-210 Course Schedule II
  10. 非法关机linux分辨率丢失,非法关机造成文件系统损坏,怎么办?请教:附图片...
  11. base64编码 vba_[VBA]Base64编码和Base64解码
  12. IDC销售系统前台模板知了云模板
  13. 几个常用的C语言编程工具,极力推荐!
  14. HTMl悬浮播放器XPlayer,Xplayer播放器
  15. 如何让单片机I/O口上电复位时为低电平
  16. 使用内网开发微信公众号
  17. G6-定制不同节点的参数 --组合图
  18. 前端异步解决方案大全(2021版)
  19. video标签(获取视频时间总长度,视频当前时间,播放暂停方法,视频封面,)
  20. Win7及以上笔记本设置共享WiFi热点

热门文章

  1. 542. 01 Matrix
  2. POJ-1845 数论
  3. linux系统命令光标移动,Linux 命令行 光标移动技巧及利用grep和find查找文件内容...
  4. 没有可用软件包 jenkins。_Jenkins分布式构建与并行构建
  5. mysql low_case_MySQL8.0的坑之lower_case_table_names
  6. 华为鸿蒙系统智能手机_余承东再度确认:鸿蒙系统将适配到华为手机上
  7. 图片自动翻转css代码,用css实现图片翻转(示例代码)
  8. python cli_click python cli 开发包
  9. 在linux中 要删除abc目录,操作系统原理与应用(linux)A卷
  10. 用mingw链接msvc生成的库时,无定义chkstk问题的解决