Codeforces Round #556 (Div. 2) - C. Prefix Sum Primes(思维)
Posted npugen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #556 (Div. 2) - C. Prefix Sum Primes(思维)相关的知识,希望对你有一定的参考价值。
Problem Codeforces Round #556 (Div. 2) - D. Three Religions
Time Limit: 1000 mSec
Problem Description
Input
Output
Sample Input
5
1 2 1 2 1
Sample Output
1 1 1 2 2
题解:这个题有做慢了,这种题做慢了和没做出来区别不大。。。
读题的时候脑子里还意识到素数除了2都是奇数,读完之后就脑子里就只剩欧拉筛了,贪心地构造使得前缀和是连续的素数,那实现就很简单了,将素数序列的差分序列求出来,不断凑出差分序列的每个数即可,但是之后想想,除了2 和 3,每个的间隔不都是偶数么,肯定是连续的2呀,费劲算差分序列干什么,直接先放2再放1不就行了(特殊处理一下2 和 3 即可),写着写着还误以为要输出下标,临时改了改,等到测样例的时候发现是输出1、2,暴风哭泣。
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define REP(i, n) for (int i = 1; i <= (n); i++) 6 #define sqr(x) ((x) * (x)) 7 8 const int maxn = 400000 + 10000; 9 const int maxm = 200000 + 100; 10 const int maxs = 10000 + 10; 11 12 typedef long long LL; 13 typedef pair<int, int> pii; 14 typedef pair<double, double> pdd; 15 16 const LL unit = 1LL; 17 const int INF = 0x3f3f3f3f; 18 const double eps = 1e-14; 19 const double inf = 1e15; 20 const double pi = acos(-1.0); 21 const int SIZE = 100 + 5; 22 const LL MOD = 1000000007; 23 24 LL n; 25 LL a[maxn]; 26 int cnt[5]; 27 LL cnt1, cnt2; 28 LL tot, prime[maxn]; 29 bool is_prime[maxn]; 30 31 void Euler() 32 { 33 memset(is_prime, true, sizeof(is_prime)); 34 is_prime[0] = is_prime[1] = false; 35 for (LL i = 2; i < maxn; i++) 36 { 37 if (is_prime[i]) 38 { 39 prime[tot++] = i; 40 } 41 for (LL j = 0; j < tot && i * prime[j] < maxn; j++) 42 { 43 is_prime[prime[j] * i] = false; 44 if (i % prime[j] == 0) 45 { 46 break; 47 } 48 } 49 } 50 } 51 52 vector<int> ans; 53 queue<int> que[3]; 54 55 int main() 56 { 57 ios::sync_with_stdio(false); 58 cin.tie(0); 59 //freopen("input.txt", "r", stdin); 60 //freopen("output.txt", "w", stdout); 61 Euler(); 62 cin >> n; 63 int x; 64 LL sum = 0; 65 for (int i = 1; i <= n; i++) 66 { 67 cin >> x; 68 que[x].push(i); 69 cnt[x]++; 70 sum += x; 71 } 72 cnt1 = cnt[1], cnt2 = cnt[2]; 73 LL pre = 0; 74 for (int i = 0; i < tot && prime[i] <= sum; i++) 75 { 76 LL tmp = prime[i] - pre; 77 LL x = tmp / 2; 78 x = min(x, cnt2); 79 if (tmp - x * 2 <= cnt1) 80 { 81 pre = prime[i]; 82 for (int j = 0; j < x; j++) 83 { 84 ans.push_back(2); 85 } 86 for (int j = 0; j < tmp - x * 2; j++) 87 { 88 ans.push_back(1); 89 } 90 cnt2 -= x; 91 cnt1 -= (tmp - x * 2); 92 } 93 } 94 for(int i = 0; i < ans.size(); i++) 95 { 96 cout << ans[i] << " "; 97 } 98 while(cnt1--) 99 { 100 cout << 1 << " "; 101 } 102 while(cnt2--) 103 { 104 cout << 2 << " "; 105 } 106 return 0; 107 }
以上是关于Codeforces Round #556 (Div. 2) - C. Prefix Sum Primes(思维)的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #556 (Div. 2) - D. Three Religions(动态规划)
Codeforces Round #556 (Div. 2) - C. Prefix Sum Primes(思维)
Codeforces Round #556 (Div. 2) C. Prefix Sum Primes
Codeforces Round #436 E. Fire(背包dp+输出路径)