codeforces C. Diverse Permutation(構造)

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   for   sp   

題意:1...n 的全排列中 p1, p2, p3....pn中,找到至少有k個
|p1-p2| , |p2-p3|, ...|pn-1 - pn| 互不相同的元素!

思路: 保證相鄰的兩個數的差值的絕對值為單調遞減序列.....
如果夠k個了,最後將沒有訪問到的元素直接添加到末尾!

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring>  4 #include<algorithm> 5  6 #define N 100005  7 using namespace std; 8  9 int vis[N];10 int num[N];11 int main(){12     int n, k;13     scanf("%d%d", &n, &k);14     num[1] = 1;15     vis[1] = 1;16     int c = k, i;17     for(i=2; i<=n; ++i){18         if(num[i-1]-c >0 && !vis[num[i-1]-c]){19             vis[num[i-1]-c]=1;20             num[i] = num[i-1]-c;21         }22         else if(num[i-1]+c <= n && !vis[num[i-1]+c]){23             vis[num[i-1]+c]=1;24             num[i] = num[i-1]+c;25         }26         --c;27         if(c < 1) break;28     }29     for(int j=1; j<=n; ++j)30         if(!vis[j])   num[++i]=j;31     printf("%d", num[1]);32     for(int j=2; j<=n; ++j)33         printf(" %d", num[j]);34     printf("\n");35     return 0;36 }
View Code

 

codeforces C. Diverse Permutation(構造)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.