Sum of a subsequence 多次随机化
来源:互联网 发布:b超数据算胎儿体重公式 编辑:程序博客网 时间:2024/06/10 04:19
Problem J. Sum of a subsequence
We are given a sequence a1, a2, . . . , a2n. Your task is to find a subsequence ai1
, ai2, . . . , ain of this sequencesuch that its sum ai1 + ai2 + · · · + ain is divisible by n, or check that such subsequence doesn’t exist.
Input
The first line of the input file contains one integer n (1 ≤ n ≤ 100 000). The second line contains 2n integers ai (1 ≤ ai ≤ 1 000 000 000), separated by single spaces. You can assume that n ∈ {10, 100, 1 000, 10 000, 100 000}.
Output
If there is no subsequence that satisfies the described conditions then the first and only line of the output file should contain one word IMPOSSIBLE. Otherwise the first and only line of the output file should
contain n integers i1 < i2 < · · · < in, such that n|(ai1 + ai2 + · · · + ain
). In case of multiple correct
answers your program should output any one.
Example
Sample input
10
17 10 13 15 10 4 14 4 9 5 3 2 1 5 19
12 19 9 17 11
Sample output
We are given a sequence a1, a2, . . . , a2n. Your task is to find a subsequence ai1
, ai2, . . . , ain of this sequencesuch that its sum ai1 + ai2 + · · · + ain is divisible by n, or check that such subsequence doesn’t exist.
Input
The first line of the input file contains one integer n (1 ≤ n ≤ 100 000). The second line contains 2n integers ai (1 ≤ ai ≤ 1 000 000 000), separated by single spaces. You can assume that n ∈ {10, 100, 1 000, 10 000, 100 000}.
Output
If there is no subsequence that satisfies the described conditions then the first and only line of the output file should contain one word IMPOSSIBLE. Otherwise the first and only line of the output file should
contain n integers i1 < i2 < · · · < in, such that n|(ai1 + ai2 + · · · + ain
). In case of multiple correct
answers your program should output any one.
Example
Sample input
10
17 10 13 15 10 4 14 4 9 5 3 2 1 5 19
12 19 9 17 11
Sample output
1 3 5 6 12 13 14 16 18 19
原题链接:http://codeforces.com/gym/100096
先放个代码,具体证明晚上再填坑
update (这个坑可能要点时间才能填了,感觉完整地证明好像有点困难QAQ。。。
#include <cmath>#include <queue>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#define maxn 600030using namespace std;inline void read(int &x) { char ch; bool flag = false; for (ch = getchar(); !isdigit(ch); ch = getchar())if (ch == '-') flag = true; for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar()); x = flag ? -x : x;}inline void read(long long &x) { char ch; bool flag = false; for (ch = getchar(); !isdigit(ch); ch = getchar())if (ch == '-') flag = true; for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar()); x = flag ? -x : x;}inline void write(int x) { static const int maxlen = 100; static char s[maxlen]; if (x < 0) { putchar('-'); x = -x;} if (!x) { putchar('0'); return; } int len = 0; for (; x; x /= 10) s[len++] = x % 10 + '0'; for (int i = len - 1; i >= 0; --i) putchar(s[i]);}inline void write(long long x) { static const int maxlen = 100; static char s[maxlen]; if (x < 0) { putchar('-'); x = -x;} if (!x) { putchar('0'); return; } int len = 0; for (; x; x /= 10) s[len++] = x % 10 + '0'; for (int i = len - 1; i >= 0; --i) putchar(s[i]);}int n;struct zy{int v;int id;};zy a[420000];int sum[420000];int ans[420000];bool cmp( zy A, zy B){return A.v<B.v;}int main(){ freopen( "sum.in" , "r" , stdin); freopen( "sum.out" , "w" , stdout ); read(n); for (int i=1;i<=2*n;i++) { read(a[i].v); a[i].id=i; a[i].v=a[i].v%n; } sort( a+1 ,a+2*n+1, cmp); for (;;) { for (int i=1;i<=n;i++) a[ 2*n +i ]=a[i]; sum[0]=0; for (int i=1;i<=3*n;i++) sum[i]=( sum[i-1]+a[i].v ) % n; for (int i=1;i<=2*n;i++) if ( sum[ i-1 ]==sum[ i+n-1 ] ) { for (int j=i;j<=i+n;j++) ans[j-i+1]=a[j].id; sort(ans+1,ans+n+1); for (int j=1;j<=n;j++) printf("%d ",ans[j]); puts(""); return 0; } random_shuffle(a+1,a+2*n+1); } return 0;}
阅读全文
0 0
- Sum of a subsequence 多次随机化
- Sum of a tree
- Sum of a tree
- PAT A 1007. Maximum Subsequence Sum (25)
- PAT-A 1007. Maximum Subsequence Sum
- PAT A 1007. Maximum Subsequence Sum (25)
- PAT-A-1007. Maximum Subsequence Sum (25)
- PAT-A 1007. Maximum Subsequence Sum (25)
- pat a 1007 Maximum Subsequence Sum
- determine S is a subsequence of T
- A. Lucky Sum of Digits
- pat(A) 1007. Maximum Subsequence Sum(和最大子段)
- A - Sum of Consecutive Prime Numbers
- A - Sum of Consecutive Prime Numbers
- Minimum Path Sum of a Binary Tree
- Maximum Subsequence Sum
- 1007. Maximum Subsequence Sum
- PAT1007. Maximum Subsequence Sum
- redis的安装以及增添到window服务里面
- 使用shader实现UI按钮置灰的操作
- Objective-C: ARC和Non-ARC混编的问题
- thinkphp 3.2 钩子原理
- Git与Github的区别
- Sum of a subsequence 多次随机化
- Spring-AOP 静态正则表达式方法匹配切面
- LCA 最近公共祖先——Tarjan(离线)算法的基本思路及其算法实现
- 进制的编码计算问题
- 数组去重
- SSH ajax登录
- C++ const 与 nonconst 的简单关系
- composer安装laravel框架
- python scrapy 的概念、安装、使用和案例