CF 1372C Omkar and Baseball

題目:ios

Patrick likes to play baseball, but sometimes he will spend so many hours hitting home runs that his mind starts to get foggy! Patrick is sure that his scores across n sessions follow the identity permutation (ie. in the first game he scores 1 point, in the second game he scores 2 points and so on). However, when he checks back to his record, he sees that all the numbers are mixed up!session

Define a special exchange as the following: choose any subarray of the scores and permute elements such that no element of subarray gets to the same position as it was before the exchange. For example, performing a special exchange on [1,2,3] can yield [3,1,2] but it cannot yield [3,2,1] since the 2 is in the same position.ide

Given a permutation of n integers, please help Patrick find the minimum number of special exchanges needed to make the permutation sorted! It can be proved that under given constraints this number doesn't exceed 10^18.this

An array aa is a subarray of an array bb if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.spa

 思路:code

①所有都是正確位置   0orm

②從先後跑到第一個不是a[i] = i,從後往前跑到第一個不是a[i] = i,判斷中間的是否是都是a[i] != i,若是是就是1,不是就是2blog

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <queue>
 5 #include <string>
 6 #include <vector>
 7 #include <cmath>
 8  
 9 using namespace std;
10  
11 #define ll long long
12 #define pb push_back
13 #define fi first
14 #define se second
15  
16 const int N = 2e5 + 10;
17 int a[N];
18  
19 void solve()
20 {      
21     int T;
22     cin >> T;
23     while(T--){
24         int n;
25         cin >> n;
26         for(int i = 1; i <= n; ++i) cin >> a[i];
27         int same = 0;
28         for(int i = 1; i <= n; ++i){
29             if(a[i] == i) same++;
30         }
31         if(same == n) cout << 0 << endl;
32         else{
33             int l, r;
34             for(int i = 1; i <= n; ++i){
35                 if(a[i] == i) continue;
36                 l = i - 1;
37                 break;
38             }
39             for(int i = n; i >= 1; --i){
40                 if(a[i] == i) continue;
41                 r = i + 1;
42                 break;
43             }
44             same = 0;
45             //cout << "l = " << l << "  " << " r = " << r << endl;
46             for(int i = l + 1; i <= r - 1; ++i){
47                 if(a[i] == i) same++;
48             }
49             if(same == 0) cout << 1 << endl;
50             else cout << 2 << endl;
51         }
52     }
53 }
54  
55 int main()
56 {
57     ios::sync_with_stdio(false);
58     cin.tie(0);
59     cout.tie(0); 
60     solve();
61  
62     return 0;
63 }
相關文章
相關標籤/搜索