1. 程式人生 > >Codeforces Round #556 (Div. 2) - C. Prefix Sum Primes(思維)

Codeforces Round #556 (Div. 2) - C. Prefix Sum Primes(思維)

cin 貪心 技術分享 rime turn cto 序列 queue memset

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(思維)