USACO 2017 February Gold

时间:2021-03-21 07:37:09

那天打cf前无聊练手

T1.Why Did the Cow Cross the Road

题目大意:N*N的矩阵,从左上角走到右下角,走一步消耗T,每走3步消耗当前所在位置上的权值,求最小消耗

思路:好像很傻逼但我不会做,写BFS写着写着写成SPFA,管他呢,过了就行(大致就是每个点拆成三个,就能知道什么时候要加上额外的权值)

#include<cstdio>
#include<algorithm>
using namespace std;
char B[<<],*S=B,C;int X;
inline int read()
{
while((C=*S++)<''||C>'');
for(X=C-'';(C=*S++)>=''&&C<='';)X=X*+C-'';
return X;
}
#define MN 100
#define MQ 10000000
const int o[][]={{,},{,-},{,},{-,}};
int a[MN+][MN+],qx[MQ],qy[MQ],qt[MQ],qn,f[MN+][MN+][];
int main()
{
freopen("visitfj.in","r",stdin);
freopen("visitfj.out","w",stdout);
fread(B,,<<,stdin);
int n,t,i,j,x,y,p,w;
n=read();t=read();
for(i=;i<=n;++i)for(j=;j<=n;++j)a[i][j]=read();
for(f[][][]=qx[]=qy[]=,i=;i<=qn;++i)for(j=;j<;++j)
{
x=qx[i]+o[j][];y=qy[i]+o[j][];w=f[qx[i]][qy[i]][qt[i]]+t;
if((p=qt[i]+)>)p=,w+=a[x][y];
if(!x||x>n||!y||y>n)continue;
if(f[x][y][p]&&w>=f[x][y][p])continue;
f[x][y][p]=w;qx[++qn]=x;qy[qn]=y;qt[qn]=p;
}
printf("%d",min(f[n][n][],min(f[n][n][],f[n][n][]))-);
}

T2.Why Did the Cow Cross the Road II

参见我写的铂金组题解T2

http://www.cnblogs.com/ditoly/p/6404238.html

T3.Why Did the Cow Cross the Road III

题目大意:给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数。

思路:树状数组维护,从左到右扫一遍,第一次出现就在对应位置加1,第二次出现统计答案并把第一次出现的那个位置减1。

#include<cstdio>
#include<iostream>
using namespace std;
char B[<<],*S=B,C;int X;
inline int read()
{
while((C=*S++)<''||C>'');
for(X=C-'';(C=*S++)>=''&&C<='';)X=(X<<)+(X<<)+C-'';
return X;
}
#define MN 100000
#define lb(x) (x&-x)
int s[MN+],a[MN+];
void inc(int x,int z){for(;x<=MN;x+=lb(x))s[x]+=z;}
int sum(int x){int r=;for(;x;x-=lb(x))r+=s[x];return r;}
int main()
{
freopen("circlecross.in","r",stdin);
freopen("circlecross.out","w",stdout);
fread(B,,<<,stdin);
int n=read()<<,i,x;long long ans=;
for(i=;i<=n;++i)
if(a[x=read()])ans+=sum(i)-sum(a[x]),inc(a[x],-);
else inc(a[x]=i,);
cout<<ans;
}