A题
签到模拟即可
B题
单独考虑每一个a[i],如果i要是答案需要指针移动多少次,然后算完,排个序,指针移动最少的就是答案。
#include <bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define ls p<<1
#define rs p<<1|1
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define ull unsigned long long
#define db double
#define endl '\n'
#define debug(a) cout<<#a<<"="<<a<<endl;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 0x3f3f3f3f
#define x first
#define y second
#define pb push_back
using namespace std;
const int N=2e5+10,mod=1e9+7;
void solve()
{
int n;cin>>n;
vector<pii>a(n+1);
int ans=1e9;
rep(i,1,n)
{
int val;cin>>val;
int pos=val%n+1;
if(n==0) pos=1;
int k=0;
//a[i]减为0时指针的位置在i之后
if(pos>i) k=n-pos+i;
else if(pos<i) k=i-pos;
a[i]={val+k,i};
}
sort(a.begin()+1,a.begin()+1+n);
cout<<a[1].y<<endl;
return;
}
signed main()
{
IOS
// freopen("1.in", "r", stdin);
// int _;
// cin>>_;
// while(_--)
solve();
return 0;
}
C题
赛时没过被卡map了开成vector光速过了
我的思路就是从前往后\(O(n)\)扫一遍模拟一下交换,这样交换的次数应该是最少的
用\(3*n-cnt\)去判断如果为偶数说明剩下的数可以通过偶数次交换不改变结果的情况下消耗掉。
这时就直接输出1反之则输出2
#include <bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define ls p<<1
#define rs p<<1|1
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define ull unsigned long long
#define db double
#define endl '\n'
#define debug(a) cout<<#a<<"="<<a<<endl;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 0x3f3f3f3f
#define x first
#define y second
#define pb push_back
using namespace std;
const int N=2e5+10,mod=1e9+7;
void solve()
{
int n;cin>>n;
// map<int,int>mp;
vector<int>a(n+1);
vector<int>mp(n+1);
rep(i,1,n)
{
cin>>a[i];
mp[a[i]]=i;
}
int cnt=0;
auto change=[&]()
{
rep(i,1,n)
{
if(a[i]!=i)
{
//tar是i当前所在的位置,pos
int tar=mp[i];
int tmp=a[i];
swap(a[i],a[tar]);
mp[i]=i;
mp[tmp]=tar;
cnt++;
// cout<<mp[i]<<' '<<mp[a[i]]
}
}
};
change();
if((3*n-cnt)%2==0) cout<<1<<endl;
else cout<<2<<endl;
return;
}
signed main()
{
IOS
// freopen("1.in", "r", stdin);
// int _;
// cin>>_;
// while(_--)
solve();
return 0;
}
标签:周赛,141,int,rep,pos,long,mp,define,Acwing
From: https://www.cnblogs.com/cxy8/p/18005327