CF 1033 C. Permutation Game

來源:互聯網
上載者:User

標籤:分析   target   nlog   +=   long   name   cst   ORC   --   

C. Permutation Game

http://codeforces.com/contest/1033/problem/C

題意:

  一個排列,每個位置i走到的位置j滿足:a[j]>a[i],(j-i)是a[i]的倍數。問從每個位置開始,是否有必勝策略。

分析:

  博弈論+拓撲。

  由於是一個排列,那麼可以枚舉每個數,判斷這個位置的a是否大於它,如果可以連邊。這樣的複雜度是$nlogn$的。

  然後對於一些無路可走的點,這是必敗態,根據必勝和必敗的定義:必勝態的後繼狀態中存在至少一個必敗態,必敗態的後繼狀態全是必勝態。

  然後建反圖,在DAG上拓撲。

代碼:

 1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<cmath> 5 #include<iostream> 6 #include<cctype> 7 #include<set> 8 #include<vector> 9 #include<queue>10 #include<map>11 #define fi(s) freopen(s,"r",stdin);12 #define fo(s) freopen(s,"w",stdout);13 using namespace std;14 typedef long long LL;15 16 inline int read() {17     int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch==‘-‘)f=-1;18     for(;isdigit(ch);ch=getchar())x=x*10+ch-‘0‘;return x*f;19 }20 21 22 const int N = 1000005;23 int a[N], deg[N], q[N], f[N];24 vector<int>T[N];25 26 int main() {27     int n = read();28     for (int i=1; i<=n; ++i) a[i] = read();29     30     for (int i=1; i<=n; ++i) {31         for (int j=i+a[i]; j<=n; j+=a[i]) 32             if (a[j] > a[i]) T[j].push_back(i), deg[i] ++;33         for (int j=i-a[i]; j>=1; j-=a[i]) 34             if (a[j] > a[i]) T[j].push_back(i), deg[i] ++;35     }36 37     int L = 1, R = 0;38     memset(f, -1, sizeof(f));39     for (int i=1; i<=n; ++i) 40         if (!deg[i]) q[++R] = i, f[i] = 0;41     42     while (L <= R) {43         int u = q[L ++];44         for (int sz=T[u].size(),i=0; i<sz; ++i) {45             int v = T[u][i];46             if (f[v] == -1) {47                 if (f[u] == 0) f[v] = 1;48                 else f[v] = 0;49             }50             else {51                 if (f[u] == 0) f[v] = 1;52             }53             deg[v] --;54             if (!deg[v]) q[++R] = v;55         }56     }57     for (int i=1; i<=n; ++i) { 58         if (f[i]) putchar(‘A‘);59         else putchar(‘B‘);60     }61     return 0;62 }

 

CF 1033 C. Permutation Game

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.