A. Reverse a Substring
题意
翻转某个子串使字符串字典序变小
做法
如果有逆序,一定存在相邻的两个逆序,输出即可。
代码
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 3e5+5;
char str[maxn];
int main()
{
int n;
scanf("%d",&n);
scanf("%s",str);
for(int i=0;i<n-1;i++)
{
if(str[i]>str[i+1])
{
printf("YES\n");
printf("%d %d\n",i+1,i+2);
return 0;
}
}
printf("NO\n");
return 0;
}
B. Game with Telephone Numbers
题意
电话号码是只有11
位而且开头数字为8
的一串数字,现在有一个长度为n
的数字串,AB
轮流删除一个字符,如果能得到一个电话号码,A
获胜,否则B
获胜。
做法
A
的最优策略是删除最前面的非8
字符,B
的最优策略删除最前面的字符,模拟判断输赢即可。
代码
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1e5+5;
char str[maxn];
int main()
{
int n;
scanf("%d",&n);
scanf("%s",str);
int tmp=(n-11)/2;
int cnt=0;
for(int i=0;i<2*tmp+1;i++) if(str[i]=='8') cnt++;
if(cnt>tmp) puts("YES");
else puts("NO");
return 0;
}
C. Alarm Clocks Everywhere
题意
给出n
个数,现在想找出一对p,y
,使n
个数中的每个数x
都满足x=y+kp
。
做法
首先我们差分,如果想满足所有的数字都能被凑出来,y一定可以是最小的那个数字,而且必须使选择的p
是差分数组的gcd
的因子。
代码
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 3e5+5;
ll x[maxn],p[maxn];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%lld",&x[i]);
sort(x+1,x+1+n);
for(int i=1;i<=m;i++) scanf("%lld",&p[i]);
ll gg=0;
for(int i=2;i<=n;i++) gg=__gcd(gg,x[i]-x[i-1]);
for(int i=1;i<=m;i++)
{
if(gg%p[i]==0)
{
printf("YES\n");
printf("%lld %d\n",x[1],i);
return 0;
}
}
printf("NO");
return 0;
}
D. Beautiful Array
题意
给你一个长度为n的数组和一个x,现在可以选择至多一段子区间,让这个区间同时乘以x,之后让整个数组的最大子段和最大。
做法
dp[i][0]
表示以i为终点还没进入加倍区的最大子段和。
dp[i][1]
表示以i为终点在加倍区内的最大子段和。
dp[i][2]
表示以i为终点离开加倍区的最大子段和。
转移就很显然,最后对所有dp值取max即可。
代码
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 3e5+5;
ll a[maxn];
ll dp[maxn][3];
int main()
{
int n;
ll x;
scanf("%d%lld",&n,&x);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
ll ans=0;
for(int i=1;i<=n;i++)
{
dp[i][0]=max((ll)0,dp[i-1][0]+a[i]);
dp[i][1]=max((ll)0,max(dp[i-1][0],dp[i-1][1])+a[i]*x);
dp[i][2]=max((ll)0,max(dp[i-1][1],dp[i-1][2])+a[i]);
ans=max(ans,dp[i][0]);
ans=max(ans,dp[i][1]);
ans=max(ans,dp[i][2]);
}
printf("%lld\n",ans);
return 0;
}
E. Guess the Root
题意
现在有
11
11
11个数,
a
0
,
a
1
,
a
2
,
a
3
,
a
4
,
a
5
,
a
6
,
a
7
,
a
8
,
a
9
,
a
10
a_0,a_1,a_2,a_3,a_4,a_5,a_6,a_7,a_8,a_9,a_{10}
a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,
0
≤
a
i
≤
1
0
6
+
2
0 \leq a_i \leq 10^6+2
0≤ai≤106+2
他们组成一个函数
f
x
=
a
0
+
a
1
×
x
+
a
2
×
x
2
+
.
.
.
a
k
×
x
k
f_x = a_0+a_1×x+a_2×x^2+...a_k×x^k
fx=a0+a1×x+a2×x2+...ak×xk,
现在你可以给出
50
50
50个询问,系统会返回
f
x
m
o
d
(
1
0
6
+
3
)
f_x mod (10^6+3)
fxmod(106+3).
在
50
50
50次询问之后,你要给出一个
x
x
x,使
f
x
≡
0
m
o
d
  
(
1
0
6
+
3
)
f_x \equiv 0 \mod (10^6+3)
fx≡0mod(106+3).、
做法
给出不同的11个x可以得到11个方程,现在有11个方程11个未知数,高斯消元求解即可。
求解之后得到 a 0 , a 1 , a 2 , a 3 , a 4 , a 5 , a 6 , a 7 , a 8 , a 9 , a 10 a_0,a_1,a_2,a_3,a_4,a_5,a_6,a_7,a_8,a_9,a_{10} a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,由于答案要求的范围是 [ 0 , 1 e 6 + 2 ] [0,1e6+2] [0,1e6+2]。
我们只需要枚举并检验 f x ≡ 0 m o d    ( 1 0 6 + 3 ) f_x \equiv 0 \mod (10^6+3) fx≡0mod(106+3),由于这个不消耗询问次数,所以在11次询问后就可以确定答案。
代码
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<map>
#include<bitset>
#include<stack>
#include<set>
#include<vector>
#include <time.h>
#include<string.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
const int maxn = 20;
const int Mod=1000003;
const int p = 1000003;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const double e=exp(1);
const db PI = acos(-1);
const db ERR = 1e-10;
#define Se second
#define Fi first
#define pb push_back
#define ok cout<<"OK"<<endl
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
ll a[maxn][maxn]; //增广矩阵
ll x[maxn]; //解集
ll pow_(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1) ans=ans*a%Mod;
b>>=1;
a=a*a%Mod;
}
return ans;
}
int n=11;
void gauss()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
if(i!=j)
{
long long tmp=a[i][i]*pow_(a[j][i],p-2)%p;
for(int k=1;k<=n+1;k++) a[j][k]=a[i][k]-a[j][k]*tmp%p,a[j][k]=((a[j][k]%p)+p)%p;
}
}
for(int i=1;i<=n;i++)
{
a[i][n+1]*=pow_(a[i][i],p-2),a[i][n+1]%=p;
x[i]=a[i][n+1];
}
}
int main()
{
for(int i=1;i<=n;i++)
{
printf("? %d\n",i);
fflush(stdout);
int ans;
scanf("%d",&ans);
a[i][12]=ans;
a[i][1]=1;
for(int j=2;j<=n;j++) a[i][j]=1LL*a[i][j-1]*i%Mod;
}
gauss();
int flag=0;
for(int i=0;i<Mod;i++)
{
ll tt=0;
for(int j=1;j<=11;j++)
{
tt=(tt+x[j]*pow_(i,j-1)%Mod)%Mod;
}
if(tt==0)
{
flag=1;
printf("! %d\n",i);
fflush(stdout);
break;
}
}
if(flag==0)
{
printf("! -1\n");
fflush(stdout);
}
return 0;
}