问题A: Legion Roster
没水
问题B: Caesar's Cipher
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n,v;
char s[100010];
int main()
{
cin>>n;
while(n--)
{
cin>>v;
getchar();
cin.get(s,100000);
int m=strlen(s);
v %= 26;
for(int i=0;i<m;i++)
{
if(s[i]==' ') cout<<" ";
else{
int k=s[i]-'a';
if(k+v<26) cout<<char(k+v+'a');
else cout<<char(k+v-26+'a');
}
}
cout<<endl;
}
return 0;
}
问题C: Gods Abound
没水
问题D: Circus Sorting
没水
问题E: Pesky Publicans
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int read() {
int x=0,y=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')y=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*y;
}
const int maxn=1010;
int a[maxn];
int main(){
int T=read();
while(T--){
int n=read();
double ans=0;
for(int i=1;i<=n;i++){int x=read();ans+=x; }
double pt=1;
for(int i=1;i<=n-1;i++){
pt/=2;
}
pt=2-pt;
pt=1/pt;
ans=ans*pt;
printf("%.3lf\n",ans);
}
return 0;
}
问题G: Marching Legion
#include<iostream>
using namespace std;
int x0,y0,x1,y1;
int x,y;
const int N=110;
char s[N][N];
int flag;
int vis[N][N];
void dfs(int p,int q)
{
int dx[4]={1,-1,0,0},dy[4]={0,0,-1,1};
for(int i=0;i<4;i++)
{
int a=p+dx[i],b=q+dy[i];
if(a==x1&&b==y1)
{
flag=1;
return ;
}
if(a>=1&&a<=x&&b>=1&&b<=y&&s[a][b]=='O')
{
s[a][b]='X';
dfs(a,b);
//s[a][b]='O';
}
}
}
int main()
{
char ch;
int n;cin>>n;
while(n--)
{
flag=0;
cin>>x>>y;
for(int i=1;i<=x;i++)
{
for(int j=1;j<=y;j++)
{
cin>>ch;
s[i][j]=ch;
if(ch=='A') x1=i,y1=j;
if(ch=='R') x0=i,y0=j;
}
}
dfs(x0,y0);
if(flag) cout<<"March onward!"<<endl;
else cout<<"Stay home!"<<endl;
}
return 0;
}
问题J: Aqueduct Rider
思路:找最长
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e5+10,M=2e5+10,E=100005,S=100004;
typedef pair<int,int>PII;
int h[N],e[M],ne[M],w[M],idx;
bool vis[N];
int n,m,dist[N],p[N];
struct f {int s,e,w;} s[N];
void add(int a,int b)
{
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
priority_queue<PII,vector<PII>,less<PII> >q;
int dijkstra()
{
memset(vis,false,sizeof(vis));
while(!q.empty())
{
auto t=q.top();
q.pop();
if(vis[t.second]) continue;
vis[t.second]=true;
for(int i=h[t.second]; i!=-1; i=ne[i])
{
int j=e[i];
if(dist[j]<=t.first+s[j].w)
{
dist[j]=t.first+s[j].w;
//cout<<j<<" "<<dist[j]<<endl;
q.push({dist[j],j});
}
}
}
int ans = 0;
for(int i=1; i<=n; i++)
ans = max(ans,dist[p[i]]);
return ans;
}
int main()
{
int T;
cin>>T;
while(T--)
{
memset(h,-1,sizeof(h));
idx=0;
scanf("%d %d",&n,&m);
for(int i=1; i<=n; i++)
{
int id,u,v,w;
scanf("%d %d %d %d",&id,&u,&v,&w);
s[id] = {u,v,w};
p[i] = id;
dist[id] = w;
q.push({w,id});
}
for(int i=1; i<=m; i++)
{
int a,b;
scanf("%d %d",&a,&b);
if(s[a].e>=s[b].s) add(a,b);
else if(s[a].s<=s[b].e) add(b,a);
}
printf("%d\n",dijkstra());
}
return 0;
}
问题K: Target Practice
没水
问题L: Fortune Teller of Fraud?
没水
问题M: Cow Hopscotch
思路:
2个难点,状态转移和分治处理
1.状态转移:ans[x][y] = ∑ans[i][j] (i<x,j<y)-Σans[i][j] (i<x,j<y,a[i][j]=a[x][y]);
2.分治:由于处理左半部分,上半部分时和右半部分,下半部分没有半毛钱关系,所以分治处理;
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
const int maxn=1e3+10;
using namespace std;
int n,m,k,t,a[maxn][maxn],ans[maxn][maxn],check[maxn*maxn];
void ak(int l,int r)
{
if(l==r) return;
int mid = l+r>>1;
ak(l,mid);
memset(check,0,sizeof(check));
int all=0;
for(int j=1; j<=m; j++)
{
for(int i=mid+1; i<=r; i++)
ans[i][j] = ((ans[i][j]+all-check[a[i][j]])%mod+mod)%mod;
for(int i=l; i<=mid; i++)
all += ans[i][j], all %= mod, check[a[i][j]]+=ans[i][j],check[a[i][j]]%=mod;
}
ak(mid+1,r);
}
int main()
{
scanf("%d %d %d",&n,&m,&k);
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
scanf("%d",&a[i][j]);
ans[1][1] = 1;
ak(1,n);
printf("%d\n",ans[n][m]);
return 0;
}
问题N: Censoring
Tips:
1.只是有多个T串,那么就不能用KMP和hash来做,要用AC自动机。
2.最后任意两个单词都没有包含关系,所以处理的时候不需要沿着fail树一直找,否则会TLE。
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int M = 1e5+10;
struct node {int fa,ch[26],cnt;} p[M];
char str[M],w[M],z[M];
int n,tot,len,he;
int pos[M],q[M],l,r;
void wm()
{
int i,u,t;
l=1;
q[++r]=1;
while(l<=r)
{
u=q[l++];
for(i=0; i<26; i++)
{
if(!p[u].ch[i])
{
if(u==1) p[u].ch[i]=1;
else p[u].ch[i]=p[p[u].fa].ch[i];
continue;
}
q[++r] = p[u].ch[i];
if(u==1)
{
p[p[u].ch[i]].fa=1;
continue;
}
t = p[u].fa;
while(!p[t].ch[i]&&t) t = p[t].fa;
if(t) p[p[u].ch[i]].fa = p[t].ch[i];
else p[p[u].ch[i]].fa = 1;
}
}
}
void find()
{
int i,j,u,t;
u=1; pos[0]=1;
for(i=0; i<len; i++)
{
z[++he] = str[i];
pos[he] = p[pos[he-1]].ch[str[i]-'a'];
if(p[pos[he]].cnt) he -= p[pos[he]].cnt;
}
for(i=1; i<=he; i++) putchar(z[i]);
}
int main()
{
int k,u;
scanf("%s",str); scanf("%d",&n);
len=strlen(str); tot=1;
for(int i=1; i<=n; i++)
{
scanf("%s",w);
k=strlen(w); u=1;
for(int j=0; j<k; j++)
{
if(!p[u].ch[w[j]-'a'])
p[u].ch[w[j]-'a'] = ++tot;
u=p[u].ch[w[j]-'a'];
}
p[u].cnt=k;
}
wm();
find();
}
问题O: Fencing the Herd
题解:
#include <bits/stdc++.h>
#define ll long long
#define FOR(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int N=200200,M=200200;
const ll INF=4e18;
int n,m,cp,cl,r;
ll mx[M],mi[M];
struct vec{
ll x,y;
vec(){}
vec(ll a,ll b){x=a;y=b;}
vec operator + (vec a){return vec(x+a.x,y+a.y);}
vec operator - (vec a){return vec(x-a.x,y-a.y);}
ll operator ^ (vec a){return x*a.y-y*a.x;}
}p[N],h[N],di[M];
struct lin{vec d;int i;}L[M];
struct qry{int o;ll x,y,z;}q[M];
void upd(int i,ll x,ll y){
ll va=x*q[i].x+y*q[i].y;
mx[i]=max(mx[i],va);
mi[i]=min(mi[i],va);
}
bool cmp(vec a,vec b) {return a.x==b.x?a.y<b.y:a.x<b.x;}
bool cmp1(lin a,lin b) {return (a.d^b.d)>0;}
bool cmp2(lin a,lin b) {return (a.d^b.d)<0;}
void sol(int l,int r) {
if(l==r) return;
int md = (l+r)>>1;
sol(l,md),sol(md+1,r); cp=cl=0;
FOR(i,l,md) if(q[i].o==1) p[++cp]=vec(q[i].x,q[i].y);
FOR(i,md+1,r) if(q[i].o==2) L[++cl]=(lin){di[i],i};
if(!(cp*cl)) return;
sort(p+1,p+cp+1,cmp);
h[r=1]=p[1];
FOR(i,2,cp) {
while(1<r && ((h[r]-h[r-1])^(p[i]-h[r]))<=0) r--;
h[++r]=p[i];
}
sort(L+1,L+cl+1,cmp1);
int j=1;
//下凸包
FOR(i,1,r)
for(;(i+1>r || (L[j].d^(h[i+1]-h[i]))>=0) && j<=cl;j++)
upd(L[j].i,h[i].x,h[i].y);
h[r=1]=p[1];
FOR(i,2,cp) {
while(1<r && ((h[r]-h[r-1])^(p[i]-h[r]))>=0) r--;
h[++r]=p[i];
}
sort(L+1,L+cl+1,cmp2);
j=1;
//上凸包
FOR(i,1,r)
for(;(i+1>r || (L[j].d^(h[i+1]-h[i]))<=0) && j<=cl;j++)
upd(L[j].i,h[i].x,h[i].y);
}
int main(){
scanf("%d%d",&n,&m);
m += n;
FOR(i,1,n) q[i].o=1,scanf("%lld%lld",&q[i].x,&q[i].y);
FOR(i,n+1,m){
scanf("%d",&q[i].o);
if(q[i].o==1) scanf("%lld%lld",&q[i].x,&q[i].y);
if(q[i].o==2){
scanf("%lld%lld%lld",&q[i].x,&q[i].y,&q[i].z);
if(q[i].y<0) q[i].x*=-1,q[i].y*=-1,q[i].z*=-1;
if(q[i].y==0 && q[i].x<0) q[i].x*=-1,q[i].z*=-1;
di[i]=vec(q[i].y,-q[i].x);
mx[i]=-INF;mi[i]=INF;
}
}
sol(1,m);
FOR(i,n+1,m)if(q[i].o==2)
mx[i]-q[i].z<0 || mi[i]-q[i].z>0?puts("YES"):puts("NO");
}