CF - 798D. Mike and distribution - 排序+数学

1.题目描述:

D. Mike and distribution
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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
5
8 7 4 8 3
4 2 5 3 7
output
3
1 4 5

2.题意概述:

A有n个数,B有n个数,要你在这n个位置中选取上限为 大小的子数组,使得它对应a下标子串的和大于 a数组总和的 1/2,且对应b下标的子串的和大于b数组总和的1/2。

3.解题思路:

开始没什么好想法,凌晨脑袋比较乱,只想到dfs去暴力,但是暴力又写x了,结果wa了一晚上,赛后再来了一发dfs,果断t了,确实1e5的数组去暴力dfs很难过。

正解应该往数学方面想,首先题只要你给出满足条件的解,且保证一定有解,其实就是一个鸽巢原理,按最大值排序后选择前 个,假设这 的数都小于sum / 2,很容易证明数组总和是小于sum的,因此反证法证明了一定可以取,下面就直接贪心地取好了。在standing上还看到一种奇妙的方法是利用随机数,这样复杂度不好说,但是确实在大量数据面前很有优势。

4.AC代码:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 100100
#define lson root << 1
#define rson root << 1 | 1
#define lent (t[root].r - t[root].l + 1)
#define lenl (t[lson].r - t[lson].l + 1)
#define lenr (t[rson].r - t[rson].l + 1)
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
int a[maxn], b[maxn], p[maxn];
int myrand(int x)
{
	return rand() % x;
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	long _begin_time = clock();
#endif
	int n;
	while (~scanf("%d", &n))
	{
		srand(time(NULL));
		ll suma = 0, sumb = 0;
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
			suma += a[i];
			p[i] = i;
		}
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &b[i]);
			sumb += b[i];
		}
		bool flag = 0;
		while (!flag)
		{
			random_shuffle(p + 1, p + n + 1, myrand);
			ll sum1 = 0, sum2 = 0;
			for (int i = 1; i <= n / 2 + 1; i++)
			{
				sum1 += a[p[i]];
				sum2 += b[p[i]];
				if (sum1 > suma / 2 && sum2 > sumb / 2)
				{
					flag = 1;
					break;
				}
			}
		}
		printf("%d\n", n / 2 + 1);
		for (int i = 1; i <= n / 2 + 1; i++)
			if (i == 1)
				printf("%d", p[i]);
			else
				printf(" %d", p[i]);
		puts("");
	}
#ifndef ONLINE_JUDGE
	long _end_time = clock();
	printf("time = %ld ms.", _end_time - _begin_time);
#endif
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值