一句话题解系列。
A. Okabe and Future Gadget Laboratory
暴力枚举验证。
#include<cstdio>
#include<algorithm>
using namespace std;
int a[55][55],n;
int main()
{
int ok;
scanf("%d",&n);
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
scanf("%d",&a[i][j]);
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
if (a[i][j]!=1)
{
ok=0;
for (int x=1;x<=n&&!ok;x++)
for (int y=1;y<=n&&!ok;y++)
if (a[i][x]+a[y][j]==a[i][j])
ok=1;
if (!ok)
{
printf("No\n");
return 0;
}
}
printf("Yes\n");
}
B. Okabe and Banana Trees
最优解一定可以取在整点上,枚举
x
,
#include<cstdio>
#include<algorithm>
using namespace std;
#define LL long long
int main()
{
LL ans=0,x,y;
int m,b;
scanf("%d%d",&m,&b);
for (int i=0;i<=m*b;i++)
{
x=i;
y=b-(i+m-1)/m;
ans=max(ans,(y+1)*(x+y)*(x+1)/2);
}
printf("%I64d\n",ans);
}
C. Okabe and Boxes
每次修改一定最优地把栈排序,但是直接排序是
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int cmp(int x,int y)
{
return x>y;
}
int sta[300010],ok[300010],n,top;
char s[110];
int main()
{
int ans=0,x;
scanf("%d",&n);
for (int i=1,j=1;i<=2*n;i++)
{
scanf("%s",s);
if (s[0]=='a')
{
scanf("%d",&x);
sta[++top]=x;
}
else
{
if (!ok[top]&&sta[top]!=j)
{
ans++;
ok[top]=1;
}
if (ok[top])
{
ok[top]=0;
ok[top-1]=1;
}
top--;
j++;
}
}
printf("%d\n",ans);
}
D. Okabe and City
每个点可以花费
1
的代价走到和它行或列之差不超过
#include<cstdio>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
const int maxn=1000010,mod=1000000,oo=0x3f3f3f3f;
int n,m,k,r[maxn],c[maxn],dis[maxn],que[maxn],in[maxn];
map<pair<int,int>,int> mp;
vector<pair<int,int> > to[maxn];
void inc(int &x)
{
x=(x==mod?0:x+1);
}
int main()
{
int hd=0,tl=1,ans=oo,u,v;
scanf("%d%d%d",&n,&m,&k);
for (int i=1;i<=k;i++)
{
scanf("%d%d",&r[i],&c[i]);
to[k+r[i]].push_back(make_pair(i,0));
to[k+n+c[i]].push_back(make_pair(i,0));
to[i].push_back(make_pair(k+r[i],1));
to[i].push_back(make_pair(k+n+c[i],1));
if (r[i]>1)
{
to[i].push_back(make_pair(k+r[i]-1,1));
pair<int,int> pr=make_pair(r[i]-1,c[i]);
if (mp.count(pr))
{
to[i].push_back(make_pair(mp[pr],0));
to[mp[pr]].push_back(make_pair(i,0));
}
}
if (r[i]>2) to[i].push_back(make_pair(k+r[i]-2,1));
if (r[i]<n)
{
to[i].push_back(make_pair(k+r[i]+1,1));
pair<int,int> pr=make_pair(r[i]+1,c[i]);
if (mp.count(pr))
{
to[i].push_back(make_pair(mp[pr],0));
to[mp[pr]].push_back(make_pair(i,0));
}
}
if (r[i]<n-1) to[i].push_back(make_pair(k+r[i]+2,1));
if (c[i]>1)
{
to[i].push_back(make_pair(k+n+c[i]-1,1));
pair<int,int> pr=make_pair(r[i],c[i]-1);
if (mp.count(pr))
{
to[i].push_back(make_pair(mp[pr],0));
to[mp[pr]].push_back(make_pair(i,0));
}
}
if (c[i]>2) to[i].push_back(make_pair(k+n+c[i]-2,1));
if (c[i]<m)
{
to[i].push_back(make_pair(k+n+c[i]+1,1));
pair<int,int> pr=make_pair(r[i],c[i]+1);
if (mp.count(pr))
{
to[i].push_back(make_pair(mp[pr],0));
to[mp[pr]].push_back(make_pair(i,0));
}
}
if (c[i]<m-1) to[i].push_back(make_pair(k+n+c[i]+2,1));
mp[make_pair(r[i],c[i])]=i;
}
for (int i=1;i<=k+n+m;i++) dis[i]=oo;
for (int i=1;i<=k;i++)
if (r[i]==1&&c[i]==1)
{
que[0]=i;
in[i]=1;
dis[i]=0;
break;
}
while (hd!=tl)
{
u=que[hd];
inc(hd);
in[u]=0;
for (vector<pair<int,int> >::iterator it=to[u].begin();it!=to[u].end();++it)
if (dis[u]+(*it).second<dis[v=(*it).first])
{
dis[v]=dis[u]+(*it).second;
if (!in[v])
{
in[v]=1;
que[tl]=v;
inc(tl);
}
}
}
for (int i=1;i<=k;i++)
if (r[i]==n&&c[i]==m)
{
printf("%d\n",dis[i]);
return 0;
}
else if (r[i]>=n-1||c[i]>=m-1)
ans=min(ans,dis[i]+1);
if (ans==oo) printf("-1\n");
else printf("%d\n",ans);
}
E. Okabe and El Psy Kongroo
矩阵快速幂转移。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL long long
int p=1000000007;
int c[110],m,n;
LL k,a[110],b[110],len[110];
int inc(int x,int y)
{
x+=y;
return x>=p?x-p:x;
}
struct mat
{
int a[16][16];
void init()
{
for (int i=0;i<=m;i++)
for (int j=0;j<=m;j++)
a[i][j]=(i==j);
}
void build(int k)
{
for (int i=0;i<=m;i++)
for (int j=0;j<=m;j++)
a[i][j]=(j<=k&&abs(i-j)<=1);
}
mat operator * (const mat &x) const
{
mat ret;
for (int i=0;i<=m;i++)
for (int j=0;j<=m;j++)
{
ret.a[i][j]=0;
for (int k=0;k<=m;k++)
ret.a[i][j]=inc(ret.a[i][j],(LL)a[i][k]*x.a[k][j]%p);
}
return ret;
}
}ans,tem;
mat pow(mat b,LL k)
{
mat ret;
ret.init();
for (;k;k>>=1,b=b*b)
if (k&1) ret=ret*b;
return ret;
}
int main()
{
scanf("%d%I64d",&n,&k);
for (int i=1;i<=n;i++) scanf("%I64d%I64d%d",&a[i],&b[i],&c[i]),m=max(m,c[i]);
b[n]=k;
for (int i=1;i<=n;i++) len[i]=b[i]-a[i]-1;
for (int i=1;i<n;i++)
if (c[i]<c[i+1]) len[i]++;
else len[i+1]++;
len[n]++;
ans.init();
for (int i=1;i<=n;i++)
{
tem.build(c[i]);
ans=ans*pow(tem,len[i]);
}
printf("%d\n",ans.a[0][0]);
}