1. 程式人生 > >CodeForces 758 C Unfair Poll

CodeForces 758 C Unfair Poll

txt 技術 n) 題解 long closed nfa HR type

Unfair Poll

題意:一共有n排同學每排同學有m個人, 老師問問題有一個順序, 先從第一排開始問,問完第一排的所有同學之後,再問第2排的,對於所有排的訪問順序為 1,2,3……n-1,n,n-1,n-2,……,2,1,2,然後每次訪問到新的一排先要問完這一排的所有人才會往下一(目標)排走。

題解:先聲明我們開一個數組來記錄這一排被詢問的總次數,先將k /= m, 這個代表的是完全訪問的次數,即一整排m位同學都問完有幾次,如果 完全訪問的次數< n, 我們就將前幾排全訪問次數的人都加上m,並且將剩下的訪問次數都塞到一下排就好了,如果完全訪問次數大於n了之後,發現除了第一次轉移的次數位n-1排, 我們再對 k/=(n-1) 處理出來, 然後判斷奇偶性來決定最後一次是從上到下還是從下到上,對於所有的排都加上次數,最後再把剛開始的訪問次數塞到目標排就好了。

n==1的話,對於上述解法要特殊討論因為除數或者模數不能為0。

代碼:

技術分享圖片
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
 4 #define LL long long
 5 #define ULL unsigned LL
 6 #define fi first
 7 #define se second
 8 #define pb push_back
 9 #define lson l,m,rt<<1
10
#define rson m+1,r,rt<<1|1 11 #define max3(a,b,c) max(a,max(b,c)) 12 #define min3(a,b,c) min(a,min(b,c)) 13 typedef pair<int,int> pll; 14 const LL INF = 0x3f3f3f3f3f3f3f3f; 15 const LL mod = (int)1e9+7; 16 const int N = 2e5 + 100; 17 LL n, m, x, y; 18 LL k, ans1, ans2 = INF, ans3; 19 LL cnt[N];
20 int main(){ 21 scanf("%I64d%I64d%I64d%I64d%I64d", &n, &m, &k, &x, &y); 22 LL c1, c2; 23 c1 = k/m; 24 c2 = k%m; 25 swap(c1, k); 26 if(k < n){ 27 for(int i = 1; i <= k; i++) cnt[i] = m; 28 cnt[k+1] = c2; 29 } 30 else { 31 for(int i = 1; i <= n; i++) cnt[i] = m; 32 k -= n; 33 if(n != 1){ 34 LL t = n-1; 35 LL z = k / t; 36 LL lf = k % t; 37 LL t1 = z-z/2; 38 for(int i = 1; i < n; i++) cnt[i] += t1*m; 39 for(int i = 2; i <= n; i++) cnt[i] += z/2 * m; 40 if(z&1ll){ 41 int g = 1; 42 for(int i = 2; i <= n, lf > 0; i++, lf--) cnt[i] += m, g = i; 43 cnt[++g] += c2; 44 } 45 else{ 46 int g = n; 47 for(int i = n-1; i >= 1, lf > 0; i--, lf--) cnt[i] += m, g = i; 48 cnt[--g] += c2; 49 } 50 } 51 else cnt[1] = c1; 52 } 53 for(int i = 1; i <= n; i++){ 54 if(cnt[i] == 0) {ans2 = 0; continue;} 55 if(i == x) { 56 LL lf = cnt[i]%m; 57 ans3 = cnt[i]/m; 58 if(lf >= y) ans3++; 59 }; 60 LL t1 = cnt[i]/m; 61 if(cnt[i]%m) t1++; 62 ans1 = max(ans1, t1); 63 LL t2 = cnt[i]/m; 64 ans2 = min(ans2, t2); 65 } 66 67 printf("%I64d %I64d %I64d", ans1, ans2, ans3); 68 return 0; 69 }
758 C

CodeForces 758 C Unfair Poll