A
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e6+10;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--)
{
int n,x=0;
cin>>n;
for(int i=1;;i++)
{
if(i%2)x-=2*i-1;
else x+=2*i-1;
if(abs(x)>n)
{
if(i%2)cout<<"Sakurako"<<'\n';
else cout<<"Kosuke"<<'\n';
break;
}
}
}
}
B
计算每条对角线的最小值和0取min依次累加即可
# include <bits/stdc++.h>
using namespace std;
#define int long long
int n;
const int N=510;
int a[N][N];
signed main()
{
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
int sum=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>a[i][j];
}
}
for(int i=1;i<=n;i++)
{
int mn=1e18;
int x=1,y=i;
while(x<=n&&y<=n)
{
//cout<<x<<" "<<y<<endl;
mn=min(mn,a[x][y]);
x++;
y++;
}
if(mn<0)
sum+=abs(mn);
}
for(int i=2;i<=n;i++)
{
int mn=1e18;
int x=i,y=1;
while(x<=n&&y<=n)
{ //cout<<x<<" "<<y<<endl;
mn=min(mn,a[x][y]);
x++;
y++;
}
if(mn<0)
sum+=abs(mn);
}
cout<<sum<<endl;
}
}
C
计算操作的两种情况每次去取最优
# include <bits/stdc++.h>
using namespace std;
#define int long long
void solve()
{
int n;
cin >> n;
vector<int>a(n+1);
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
for (int i = 2; i <= n / 2; i++)
{
int pre = (a[i] == a[i - 1]) + (a[n - i + 1] == a[n - i + 2]);
int nex = (a[i] == a[n - i + 2]) + (a[n - i + 1] == a[i - 1]);
if (nex < pre)
swap(a[i], a[n - i + 1]);
}
int ans = 0;
for (int i = 2; i <= n; i++)
{
if (a[i] == a[i - 1])
ans++;
}
cout << ans << "\n";
return ;
}
signed main()
{
int T;
cin>>T;
while(T--)
{
solve();
}
}
D 当两个前缀和相等时区间和为零,贪心考虑区间越小越好 用map维护当前出现过的所有前缀和
# include <bits/stdc++.h>
using namespace std;
#define int long long
int n;
const int N=1e5+10;
int a[N];
map<int,int>ma;
signed main()
{
int T;
cin>>T;
while(T--)
{
ma.clear();
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
ma[0]=1;
int sum=0,res=0;
for(int i=1;i<=n;i++)
{
sum+=a[i];
res+=ma[sum];
if(ma[sum])
ma.clear();
ma[sum]=1;
}
cout<<res<<endl;
}
}
E置换环问题
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const int N=1000005;
int a[N];
bool vis[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
vis[i]=0;
}
int ans=0;
for(int i=1;i<=n;i++)
{
if(vis[i])continue;
int now=i,cnt=0;
while(!vis[now])
{
vis[now]=1;
now=a[now];
cnt++;
}
ans+=cnt-1>>1;
}
cout<<ans<<'\n';
}
return 0;
}