A. Palindromic Twist
给一个字符串(小写字母) 每个字符+1,-1;变成其他字符 a只能变b z只能变y 看能否变成回文字符串
#include<bits/stdc++.h>
using namespace std;
int32_t main()
{
int T; cin>>T;
while(T--)
{
int n; cin>>n;
string ss; cin>>ss;
int t=;
for(int i=;i<n/;i++)
{
if(ss[i]-ss[n--i]==||fabs(ss[i]-ss[n--i])==)
continue;
else t=;
}
if(t==) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
}
A.cpp
B. Numbers on the Chessboard
分类讨论下就ok了
#include<bits/stdc++.h>
#define int long long
#define MAX(a,b,c) max(a,max(b,c))
#define MIN(a,b,c) min(a,min(b,c))
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef long long LL;
typedef unsigned long long ull;
typedef unsigned long long uLL;
using namespace std;
const int maxn=1e5+;
const int INF=0x3f3f3f3f;
int32_t main()
{
int n,q; cin>>n>>q;
if(n%==)
{
int ans1=;
int ans2=n*n/;
while(q--)
{
int a,b;cin>>a>>b;
if( (a+b)%== ) cout<<ans1+(a-)*n/+(b+)/<<endl;
else cout<<ans2+(a-)*n/+(b+)/<<endl;
}
}
else if(n%==)
{
int ans1=;
int ans2=n*n/+;
while(q--)
{
int a,b;cin>>a>>b;
if( (a+b)%== )
{
if(a%==) cout<<(a-)/*n+(b+)/<<endl;
else if(a%==) cout<<(a-)/*n+(n+)/+(b+)/<<endl;
}
else
{
if(a%==) cout<<ans2+(a-)/*n+(b+)/<<endl;
else if(a%==) cout<<ans2+(a-)/*n+(n-)/+(b+)/<<endl;
}
}
}
}
B.cpp
C. Minimum Value Rectangle
要使 4*(a+b)(a+b)/a*b (a<b) 即使 b/a 最小 排序后暴力也可以 直接找b/a 最小也行。
#include<bits/stdc++.h>
#define int long long
#define MAX(a,b,c) max(a,max(b,c))
#define MIN(a,b,c) min(a,min(b,c))
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef long long LL;
typedef unsigned long long ull;
typedef unsigned long long uLL;
using namespace std;
const int maxn=3e6+;
const int INF=0x3f3f3f3f;
int a[maxn];
int b[maxn];
int32_t main()
{ios::sync_with_stdio(false); cin.tie(); cout.tie();
int T;cin>>T;
while(T--)
{ int n; cin>>n;
for(int i=;i<=n;i++) cin>>a[i];
sort(a+,a++n);
int t=;
for(int i=;i<n;i++)
{
if(a[i]!=a[i+]) continue;
else { b[++t]=a[i]; i++;}
}
double ans=1e18;
int x,y;
for(int i=;i<t;i++)
{
double c=(b[i]*1.000000000000/b[i+])+b[i+]*1.000000000000/b[i];
if(c<ans)
{
ans=c;
x=b[i]; y=b[i+];
}
}
cout<<x<<" "<<x<<" "<<y<<" "<<y<<endl;
}
}
C.cpp
D. Mouse Hunt
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+;
int c[maxn];
int a[maxn];
int x[maxn];
int sst[maxn];
int ffind(int x)
{
if(x==sst[x]) return x;
return sst[x]=ffind(sst[x]);
}
int dfs(int x, int y){
if(x == y) return c[x];
return min(dfs(a[x], y), c[x]);
}
int main()
{
int n; cin>>n;
for(int i=;i<=n;i++) cin>>c[i];
for(int i=;i<=n;i++) sst[i]=i ;
for(int i=;i<=n;i++) cin>>a[i];
for(int i=;i<=n;i++)
{
if(ffind(a[i])==ffind(i))
{
x[i]=;
// continue;
}
else
{
sst[ffind(a[i])]=ffind(i);
}
/* for(int i=1;i<=n;i++)
{
cout<<i<<"=="<<sst[i]<<" ";
}cout<<endl;
for(int i=1;i<=n;i++)
{
cout<<x[i]<<"---- ";
}cout<<endl;*/
}
int ans = ;
for(int i = ; i <= n; i++)
{
//cout<<i<<endl;
if(x[i]) ans += dfs(a[i], i);
//cout<<ans<<endl;
}
printf("%d\n", ans);
return ;
}
D.cpp