vp时间思路
A.一开始以为是无限操作,codeforce better"翻译真不错", exactly once!,一次操作求数组之和最大,贪心找到-1,-1就可以,如果没有就去找-1,1然后之和就不变了,如果没有-1,1,那就是1,1把ans-4;
1.wa6;2.ac
B.翻译貌似还有问题,是所有都满足就是坏的,因此操作一个不满足条件即可,一开始想a[b[1]]和a[b[2]],应该得枚举找最小的操作次数.!如果n,m二层循环枚举会超时.
仔细分析一下需要满足的条件
x=a[b[i]],y=a[b[i+1]];
1.y-x>d;
2.y>x;
操作不满足以上一个条件即可
1.那就是y和x交换,ans=y-x;
2.找到y+d+1的元素(index),ans=abs(y-index);
两者比较
没写出来!
题解
A.
// Problem: A. Flip Flop Sum
// Contest: Codeforces - Codeforces Round 848 (Div. 2)
// URL: https://codeforces.com/contest/1778/problem/A
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//へ /|
// /\7 ∠_/
// / │ / /
// │ Z _,< / /`ヽ
// │ ヽ / 〉
// Y ` / /
// イ● 、 ● ⊂⊃〈 /
// () へ | \〈
// >ー 、_ ィ │ //
// / へ / ノ<| \\
// ヽ_ノ (_/ │//
// 7 |/
// >―r ̄ ̄`ー―_
#include <iostream>
#include <cstring>
#include <iomanip>
#include <ctime>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#define eps 1e-5
#define INF 1e9
using namespace std;
typedef long long ll;
const int N = 2e6 + 9;
int a[N];
void solve() {
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
int ans=0;
int mn=2;
for(int i=1;i<=n;i++){
ans+=a[i];
if(i>1){
mn=min(mn,a[i]+a[i-1]);
}
}
cout<<ans-2*mn<<'\n';
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int q;
cin >> q;
while (q--) {
solve();
}
return 0;
}
B.理解错,要仔细读题,不是数组的值而且是用数组索引满足不等式而且要全部都满足才是坏数组.
x=pos[a[i-1]],y=pos[a[i]];
第二个方法就是x+d+1-y;
第一步先预处理
不满足一个不等式的最小操作.
// Problem: B. The Forbidden Permutation
// Contest: Codeforces - Codeforces Round 848 (Div. 2)
// URL: https://codeforces.com/contest/1778/problem/B
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//へ /|
// /\7 ∠_/
// / │ / /
// │ Z _,< / /`ヽ
// │ ヽ / 〉
// Y ` / /
// イ● 、 ● ⊂⊃〈 /
// () へ | \〈
// >ー 、_ ィ │ //
// / へ / ノ<| \\
// ヽ_ノ (_/ │//
// 7 |/
// >―r ̄ ̄`ー―_
#include <iostream>
#include <cstring>
#include <iomanip>
#include <ctime>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#define eps 1e-5
#define INF 1e9
using namespace std;
typedef long long ll;
const int N = 2e6 + 9;
int p[N],a[N],index[N];
void solve() {
int n,m,d;
cin>>n>>m>>d;
for(int i=1;i<=n;i++){
cin>>p[i];
index[p[i]]=i;
}
for(int i=1;i<=m;i++){
cin>>a[i];
}
int ans=INF;
for(int i=2;i<=m;i++){
int x=index[a[i-1]],y=index[a[i]];
if(x>y){
ans=0;
break;
}
ans=min(ans,y-x);
if(d<=n-2){
ans=min(ans,x+d+1-y);
}
}
if(ans<0){
ans=0;
}
cout<<ans<<'\n';
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int q;
cin >> q;
while (q--) {
solve();
}
return 0;
}