Codeforces Round #410 (Div. 2) D. Mike and distribution(贪心)
2017-04-24 15:34
381 查看
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 neach 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
output
分析:按第一组数排序后,先把第一个选上,然后后边按顺序两两配对,每对中选同编号中第二组数较大的那一个数的编号,很容易发现这样选下来两组数都是符合条件的。
#include <bits/stdc++.h>
#define N 100005
using namespace std;
struct thing
{
int v,n;
friend bool operator < (thing a,thing b)
{
return a.v > b.v;
}
}a
;
int n,cnt,b
,ans
;
int main()
{
scanf("%d",&n);
for(int i = 1;i <= n;i++)
{
scanf("%d",&a[i].v);
a[i].n = i;
}
sort(a+1,a+1+n);
for(int i = 1;i <= n;i++) scanf("%d",&b[i]);
ans[++cnt] = a[1].n;
for(int i = 2;i <= n;i+=2)
{
if(i == n) ans[++cnt] = a[i].n;
else
if(b[a[i].n] > b[a[i+1].n]) ans[++cnt] = a[i].n;
else ans[++cnt] = a[i+1].n;
}
cout<<cnt<<endl;
for(int i = 1;i <= cnt;i++) cout<<ans[i]<<" ";
}
length neach 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
5 8 7 4 8 3 4 2 5 3 7
output
31 4 5
分析:按第一组数排序后,先把第一个选上,然后后边按顺序两两配对,每对中选同编号中第二组数较大的那一个数的编号,很容易发现这样选下来两组数都是符合条件的。
#include <bits/stdc++.h>
#define N 100005
using namespace std;
struct thing
{
int v,n;
friend bool operator < (thing a,thing b)
{
return a.v > b.v;
}
}a
;
int n,cnt,b
,ans
;
int main()
{
scanf("%d",&n);
for(int i = 1;i <= n;i++)
{
scanf("%d",&a[i].v);
a[i].n = i;
}
sort(a+1,a+1+n);
for(int i = 1;i <= n;i++) scanf("%d",&b[i]);
ans[++cnt] = a[1].n;
for(int i = 2;i <= n;i+=2)
{
if(i == n) ans[++cnt] = a[i].n;
else
if(b[a[i].n] > b[a[i+1].n]) ans[++cnt] = a[i].n;
else ans[++cnt] = a[i+1].n;
}
cout<<cnt<<endl;
for(int i = 1;i <= cnt;i++) cout<<ans[i]<<" ";
}
相关文章推荐
- Codeforces Round #410 Div2 D Mike and distribution
- Mike and gcd problem Codeforces Round #410 (Div. 2)
- Codeforces Round #410 (Div. 2) Mike and gcd problem 思维
- Codeforces Round #410 (Div. 2)B. Mike and strings(暴力)
- Codeforces Round #305 (Div. 2) A. Mike and Fax 暴力回文串
- Codeforces Round #228 (Div. 1) A. Fox and Box Accumulation 贪心
- Codeforces Round #361 (Div. 2)E. Mike and Geometry Problem
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
- Codeforces Round #169 (Div. 2) D. Little Girl and Maximum XOR(贪心,中等)
- Codeforces Round #292 (Div. 2) C. Drazil and Factorial(贪心YY)
- 【Codeforces Round 326 (Div 2)C】【贪心】Duff and Weight Lifting 每次取数二的幂数最小取数次数
- Codeforces Round #305 (Div. 2) C. Mike and Frog +B. Mike and Fun
- Codeforces Round #324 (Div. 2) E. Anton and Ira(贪心)
- 【Codeforces Round 331 (Div 2)C】【脑洞 SET贪心写法】Wilbur and Points 锯齿图形差值编号,右上角的最大
- Codeforces Round #305 (Div. 2)B. Mike and Fun
- 【Codeforces Round 330 (Div 2)E】【贪心 暴力】Edo and Magnets 给定矩形最多去除m个,最小面积矩形使得覆盖所有小矩形重心
- 【Codeforces Round 331 (Div 2)B】【水题 贪心】Wilbur and Array 修改对p位置后的所有位置生效 最少修改次数使得数列变成b
- Codeforces Round #410 (Div.2) D.Mike and distrubition-构造
- 【Codeforces Round 324 (Div 2)E】【贪心 构造】Anton and Ira 全排列交换 最小距离成本
- Codeforces Round #305 (Div. 2) D. Mike and Feet 单调栈