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)
- Educational Codeforces Round 170 (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)
- Educational Codeforces Round 168 (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 170 (Rated for Div. 2) A-D
- Educational Codeforces Round 170 (Rated for Div. 2) ABCD
- Educational Codeforces Round 171 (Rated for Div. 2) 題解
- Educational Codeforces Round 168 (Rated for Div. 2) A - E
- Educational Codeforces Round 143 (Rated for Div. 2) A-E
- Educational Codeforces Round 142 (Rated for Div. 2) A-D
- vp Educational Codeforces Round 168 (Rated for Div. 2) A - E
- Educational Codeforces Round 168 (Rated for Div. 2) 題解
- Educational Codeforces Round 95 (Rated for Div. 2) G. Three Occurrences
- Educational Codeforces Round 163 (Rated for Div. 2) - VP記錄
- Educational Codeforces Round 166 (Rated for Div. 2) - VP記錄
- Educational Codeforces Round 162 (Rated for Div. 2) - VP記錄
- Educational Codeforces Round 159 (Rated for Div. 2) - VP記錄
- Educational Codeforces Round 158 (Rated for Div. 2) - VP記錄
- 【CodeForces訓練記錄】Educational Codeforces Round 171 (Rated for Div. 2)
- Educational Codeforces Round 167 (Rated for Div. 2) D(dp,貪心)
- Educational Codeforces Round 163 (Rated for Div. 2) 補題記錄(A~A)
- Educational Codeforces Round 171 (Div. 2)
- Educational Codeforces Round 98 (Rated for Div. 2) E. Two Editorials 細節題
- [CF1954] Educational Codeforces Round 164 (Rated for Div. 2) 題解
- Educational Codeforces Round 165 (Rated for Div. 2) C. Minimizing the Sum題解
- Educational Codeforces Round 166 (Rated for Div. 2) (補題之抽象的自我審題)抽象
- Codeforces Round #243 (Div. 2) C. Sereja and Swaps
- Educational Codeforces Round 53 (Rated for Div. 2) D. Berland Fair 二分+樹狀陣列 O(n*logn*logn) 思路AI陣列
- Codeforces Round #253 (Div. 2) D. Andrey and Problem