当前位置:网站首页>ZOJ - 4104 sequence in the pocket

ZOJ - 4104 sequence in the pocket

2022-06-24 16:00:00 51CTO

Original link : ​https://zoj.pintia.cn/problem-sets/91827364500/problems/91827370499​

ZOJ——4104 Sequence in the Pocket( Thinking problems )_ios
The test sample :

Sample Input
2
4
1 3 2 4
5
2 3 3 5 5
Sample Output
2
0

Sample explanation :

For the first sample test case , Move the third element to the front ( So the sequence becomes {2,1,3,4}), Then move the second element to the front ( So the sequence becomes {1, 2, 3,4}). Now? , The sequence is not decreasing .
For the second sample test case , Since the sequence has been sorted , Therefore, no operation is required .

The question : Here's a sequence of integers , You can move an element in the sequence to the front of the sequence any number of times , Ask how many times you have to go through at least to make this sequence not decreasing .

Their thinking : The topic is really simple , Our goal is to make the sequence not decreasing , So what if you encounter a decreasing ? Should we arrange it in order so that the sequence does not decrease , But not everyone has to operate ? That must not be , Our final sequence state is our ordered sequence , Then we use this ordered sequence to compare with the original sequence from back to front ( The comparison from back to front is because our operation is to put in front of the sequence ), Just judge which ones are not operated , So the other thing is to make the sequence non decreasing by moving to the front of the sequence .OK, Let's look at the code .

AC Code :

      
      
/*

*
*/
#include<bits/stdc++.h> //POJ I won't support it

#define rep(i,a,n) for (int i=a;i<=n;i++) //i Is a cyclic variable ,a For the initial value ,n Is the limit value , Increasing
#define per(i,a,n) for (int i=a;i>=n;i--) //i Is a cyclic variable , a For the initial value ,n Is the limit value , Decline .
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f; // infinity
const int maxn = 1e5; // Maximum .
typedef long long ll;
typedef long double ld;
typedef pair < ll, ll > pll;
typedef pair < int, int > pii;
//******************************* Split line , The above is a custom code template ***************************************//

int t, n, a[ maxn], b[ maxn];
int main(){
//freopen("in.txt", "r", stdin);// When submitting, you should comment out
IOS;
while( cin >> t){
while( t --){
cin >> n;
rep( i, 0, n - 1){
cin >> a[ i];
b[ i] = a[ i];
}
sort( b, b + n); // Sort
int pos = n - 1, sum = 0; //sum Count the number of times we have to move to the front
per( i, n - 1, 0){
if( a[ i] == b[ pos]) pos --; // There is no need to move forward
else sum ++; // Operation forward
}
cout << sum << endl;
}
}
return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.


原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241533226282.html