Educational Codeforces Round 99 (Rated for Div. 2)D. Sequence and Swaps
思路:因為arr的大小才500所以採取硬暴力的方法,由於需要輸出一個非遞減的排列,又因為換的元素比arr[i]本身小,所以必須從前向後換,但是不確定是否前面已經排好序,所以此時列舉arr中的所有元素,還可以進行減枝,但是沒有必要
#include<bits/stdc++.h>
using namespace std;
const int N = 505;
vector<int>arr,store;
int dp[N];
int n;
bool check(){
for(int i =1;i<=n;i++){
if(arr[i]<arr[i-1])
return false;
}
return true;
}
void solve(){
int x;cin>>n>>x;
int min_=1e9;
store.resize(N);
for(int i =1;i<=n;i++)cin>>store[i];
for(int i =1;i<=n;i++){
int cnt =0;
arr = store;
if(check()){
min_=0;
break;
}
for(int j =i;j<=n;j++){
if(check())
{
min_ = min(min_,cnt);
break;
}
else{
if(arr[j]>x)
{
swap(arr[j], x);
cnt++;
}
}
}
}
if(min_==1e9)
cout<<-1<<endl;
else
cout<<min_<<endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int __=1;
cin>>__;
while (__--) {
solve();
}
}
相關文章
- Educational Codeforces Round 99 (Rated for Div. 2) D. Sequence and Swaps(貪心)
- Educational Codeforces Round 37 (Rated for Div. 2)
- Educational Codeforces Round 93 (Rated for Div. 2)
- codeforces Educational Codeforces Round 33 (Rated for Div. 2)
- Educational Codeforces Round 100 (Rated for Div. 2)
- Educational Codeforces Round 33 (Rated for Div. 2) C
- Educational Codeforces Round 34 (Rated for Div. 2) D
- Educational Codeforces Round 163 (Rated for Div. 2)
- Educational Codeforces Round 154 (Rated for Div. 2)
- codeforces Educational Codeforces Round 33 (Rated for Div. 2)B
- Educational Codeforces Round 93 (Rated for Div. 2)題解
- Educational Codeforces Round 143 (Rated for Div. 2) A-E
- Educational Codeforces Round 142 (Rated for Div. 2) A-D
- Educational Codeforces Round 163 (Rated for Div. 2) 補題記錄(A~A)
- Educational Codeforces Round 98 (Rated for Div. 2) E. Two Editorials 細節題
- [CF1954] Educational Codeforces Round 164 (Rated for Div. 2) 題解
- Educational Codeforces Round 53 (Rated for Div. 2) D. Berland Fair 二分+樹狀陣列 O(n*logn*logn) 思路AI陣列
- Codeforces Round #243 (Div. 2) C. Sereja and Swaps
- Codeforces Round #253 (Div. 2) D. Andrey and Problem
- Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 二分+前字尾預處理
- Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) B
- Codeforces Round #180 (Div. 2) D. Fish Weight 貪心
- 21 Educational Codeforces Round 136 (Rated for Div. 2)Knowledge Cards(樹狀陣列、set、+思維、數字華容道)陣列
- Codeforces Round #199 (Div. 2) A Xenia and Divisors
- Codeforces Round #688(Div 2) D. Checkpoints
- Educational Codeforces Round 32
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals)爆零記
- Codeforces Round #639 (Div. 2)
- Codeforces Round #541 (Div. 2)
- Codeforces Round #682 (Div. 2)
- Codeforces Round #678 (Div. 2)
- Codeforces Round #747 (Div. 2)
- Codeforces Round #673 (Div. 2)
- Codeforces Round #672 (Div. 2)
- Codeforces Round #448 (Div. 2) A
- Codeforces Round #217 (Div. 2)
- Codeforces Round #256 (Div. 2)
- Codeforces Round #259 (Div. 2)