Codeforces 798D Mike and distribution - 贪心

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 P should be distinct.

Example

Input

58 7 4 8 34 2 5 3 7

Output

3 1 4 5


  题目大意 给定两个长度为n的数列,选出不多于个互不相同的下标,使得每个数组对应下标的数的和的两倍超过它的和。

  显然贪心,我有很多稀奇古怪的想法,然后全都完美Wrong Answer。突然觉得自己可能一直用的都是假贪心,给这道题跪了。下面说正解吧。

  这个可以看成二维贪心,对于高维问题我们通常想到的是降维,再根据常用套路,降维通常用的两种方法:排序和枚举一维。

  因为这里是贪心,所以显然排序。

  题目要求还可以转化成,选择一些下标,在每个数组中,被选择数之和比剩下的数的和大。

  首先选择A[1],然后之后每两个分为一组,每组中哪个对应的B大就选哪个。如果n为偶数,再把最后一个选上。

  显然在B数组中是满足题目要求的(每组中都选了最大的,还多选了1个或2个),对于A数组,每个选择了A[i]一定大于等于下一组内选择的A[j],而且会多选1个或2个,所以A数组也满足。

Code

 1 /**
 2  * Codeforces
 3  * Problem#798D
 4  * Accepted
 5  * Time: 62ms
 6  * Memory: 4500k
 7  */
 8 #include <bits/stdc++.h>
 9 using namespace std;
10 typedef bool boolean;
11
12 typedef class Data {
13     public:
14         int id;
15         int x;
16         int y;
17 }Data;
18
19 int n;
20 int *A, *B;
21 Data *ds;
22
23 boolean cmp(const Data &a, const Data& b) {    return a.x > b.x;    }
24
25 inline void init() {
26     scanf("%d", &n);
27     A = new int[(n + 1)];
28     B = new int[(n + 1)];
29     ds = new Data[(n + 1)];
30     for(int i = 1; i <= n; i++)
31         scanf("%d", A + i), ds[i].x = A[i], ds[i].id = i;
32     for(int i = 1; i <= n; i++)
33         scanf("%d", B + i), ds[i].y = B[i];
34 }
35
36 vector<int> buf;
37 inline void solve() {
38     sort(ds + 1, ds + n + 1, cmp);
39     buf.push_back(ds[1].id);
40     for(int i = 2; i < n; i += 2)
41         buf.push_back((ds[i].y > ds[i + 1].y) ? (ds[i].id) : (ds[i + 1].id));
42     if((n & 1) == 0)
43         buf.push_back(ds[n].id);
44     printf("%d\n", (signed)buf.size());
45     for(int i = 0; i < (signed)buf.size(); i++)
46         printf("%d ", buf[i]);
47 }
48
49 int main() {
50     init();
51     solve();
52     return 0;
53 }
时间: 2024-12-26 04:55:55

Codeforces 798D Mike and distribution - 贪心的相关文章

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

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

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

http://codeforces.com/contest/798/problem/D http://blog.csdn.net/yasola/article/details/70477816 对于二维的贪心我们可以先让它变成其中一维有序,这样只需要重点考虑另一维,就会简单很多. 首先,对于题目要求的选择元素之和两倍大与所有元素之和,我们可以转化为选择元素之和大于剩下的.然后我们可以将下标按照a从大到小排序.然后选择第一个,之后每两个一组,选择b大的一个,如果n是偶数再选择最后一个. 至于这样写

798D - Mike and distribution

题意:给n(n<=100000)组数,每组数有(a,b),求从这n组数里面选出k(k<=(n/2)+1)组.这k组所有a的和大于剩下n-k组中a的和,并且这k组中所有b的和大于剩下n-k组中b的和. 思路:首先按a排序.对于a[i],选择a[i]之前没有选择过的或者a[i]总是能>=a[i+1],然后从a[i+2]中开始选择..这样就可以保证选出来的a的和始终大于另外一半.然后对于可以选择的a,我选择最大的b,这样也可以让选出来的b有一个比它小的对应值.这个值可以通过优先队列来维护.如果

CF Round410 D. Mike and distribution

D. Mike and distribution 构造法 798D - Mike and distribution In the beginning, it's quite easy to notice that the condition " 2·(ap1?+?...?+?apk) is greater than the sum of all elements in A " is equivalent to " ap1?+?...?+?apk is greater than

Codeforces 798D:Mike and distribution

Codeforces 798D:Mike and distributio 题目链接:http://codeforces.com/problemset/problem/798/D 题目大意:给出两个大小为$n$的数列$A,B$,现要求从这两个数列相同位置取出$K(K \leqslant n/2+1)$个数,使得$2 \times subA>sumA$且$2 \times subB>sumB$. 想法题 我们需要从数列$A$和数列$B$中取出$K$个数,使得这$K$个数的和比剩下$n-K$个数的和

Codeforces 442B Andrey and Problem(贪心)

题目链接:Codeforces 442B Andrey and Problem 题目大意:Andrey有一个问题,想要朋友们为自己出一道题,现在他有n个朋友,每个朋友想出题目的概率为pi,但是他可以同时向多个人寻求帮助,不过他只能要一道题,也就是如果他向两个人寻求帮助,如果两个人都成功出题,也是不可以的. 解题思路:贪心,从概率最大的人开始考虑,如果询问他使得概率变大,则要询问. #include <cstdio> #include <cstring> #include <a

Codeforces Round #300-Tourist&#39;s Notes(贪心)

Tourist's Notes Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level

Codeforces 432E Square Tiling(构造+贪心)

我们通常这么写 using (SqlDataReader drm = sqlComm.ExecuteReader()) { drm.Read();//以下把数据库中读出的Image流在图片框中显示出来. MemoryStream ms = new MemoryStream((byte[])drm["Logo"]); Image img = Image.FromStream(ms); this.pictureBox1.Image = img; } 我的写数据 private void b

codeforces 349B Color the Fence 贪心,思维

1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1-9每个字母分别要ai升油漆,问最多可画多大的数字. 贪心,也有点考思维. #include<bits/stdc++.h> using namespace std; #define LL long long #define INF 0x3f3f3f3f int main() { int v,a[1