UVa 1620 懶惰的蘇珊(逆序數)


https://vjudge.net/problem/UVA-1620

題意:給出一個序列,每次可以翻轉4個連續的數,判斷是否可以變成1,2,3...n。

思路:考慮逆序數,通過計算可以得出每次翻轉4個連續的數,如果這4個數原來的逆序數為x,那么翻轉之后逆序數會變為6-x。

        1.n為偶數時,總會有序列的逆序數為偶數

        2.當n為奇數時,並且這個所給的序列的逆序數為奇數,不管怎么變換 他的逆序數不能變為 偶數。

        這兩個結論是別人博客看來的,不過我不太清楚怎么推導來着。有人懂得話希望能告訴我一下。

 1 #include<iostream> 
 2 using namespace std;
 3 
 4 const int maxn = 505;
 5 int n;
 6 int a[maxn];
 7 
 8 int main()
 9 {
10     //freopen("D:\\txt.txt", "r", stdin);
11     int t;
12     cin >> t;
13     while (t--)
14     {
15         cin >> n;
16         for (int i = 0; i < n; i++)
17             cin >> a[i];
18         int cnt = 0;
19         for (int i = 0; i < n-1; i++)
20         {
21             for (int j = i + 1; j < n; j++)
22             {
23                 if (a[i]>a[j])    cnt++;
24             }
25         }
26         if (cnt % 2 && n % 2)    cout << "impossible" << endl;
27         else cout << "possible" << endl;
28     }
29     return 0;
30 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM