noip历届签到题题解

noip历届签到题题解
noip历届签到题题解

石头剪刀布//2014

https://www.360docs.net/doc/3e8241408.html,/problem/3716/

联合权值

https://www.360docs.net/doc/3e8241408.html,/problem/3728/

无线网络发射器

https://www.360docs.net/doc/3e8241408.html,/problem/3578/

寻找道路

https://www.360docs.net/doc/3e8241408.html,/problem/3731/

石头剪刀布

打表,找出对应关系。

#include

#include

using namespace std;

const int m[5][5]={

0,0,1,1,0,

1,0,0,1,0,

0,1,0,0,1,

0,0,1,0,1,

1,1,0,0,0,

};

int a[205],b[205];

int main()

{

int n,na,nb;

scanf("%d %d %d",&n,&na,&nb);

for(int i=0;i

scanf("%d",&a[i]);

for(int i=0;i

scanf("%d",&b[i]);

int sa=0,sb=0;

for(int i=0;i

{

sa=sa+m[a[i%na]][b[i%nb]];

sb=sb+m[b[i%nb]][a[i%na]];

}

printf("%d %d",sa ,sb);

return(0);

}

#include

#include

#include

#include

using namespace std;

long long n,p,k,w[200010];

bool vis[200010];

vector edge[200010];

void dfs(int x,int last1,int last2)

{

v is[x]=1;

l ong long t=0,t2=0,t3=0,t4=0;

f or (int i=0;i

{

int y=edge[x][i];

if (!vis[y])

{

vis[y]=1;

if (last1!=0)

{

k=(k+w[y]*w[last1])%10007;

p=max(p,w[y]*w[last1]);

}

t2+=t*w[y]; t+=w[y];

if (w[y]>=t3)

{

t4=t3; t3=w[y];

}

if (w[y]

dfs(y,x,last1);

}

}

p=max(t3*t4,p); k=(k+t2)%10007;

}

int main()

{

c in>>n;

f or (int i=1;i

{

cin>>u>>v;

edge[u].push_back(v);

edge[v].push_back(u);

}

f or (int i=1;i<=n;i++) cin>>w[i];

d fs(1,0,0);

cout<

r eturn 0;

}

有关图的问题,到时候再解决。

无线网络发射器

#include

#include

using namespace std;

int map[200][200];

int d,n,x,y,a,fa;

int main()

{

l ong long ans=0;

s canf("%d%d",&d,&n);

f or(int i=0;i

{

scanf("%d%d%d",&x,&y,&a);

map[x][y]=a;

}

f or(int i=0;i<=128;i++)

for(int j=0;j<=128;j++)

{

long long t=0;

for(int u=i-d;u<=i+d;u++)

for(int v=j-d;v<=j+d;v++)

if(u>=0&&u<=128&&v>=0&&v<=128)

t=t+map[u][v];

if(ans

{

ans=t;

fa=1;

}

else

if(t==ans)

fa=fa+1;

}

p rintf("%d %lld",fa,ans);

return(0);

暴力搜索,不解释。

寻找道路

先反向,搜出所有与终点相连接的点

然后再删除所有已加边,正向再加一遍边

搜一遍,只要有点的出边的出点为false(不与终点连通),它就为false(刚开始只要它与终点连通就true)

然后再SPFAtrue的点

就ok了

DFS想直接搜出来符合条件的貌似不行......

#include

#include

#include

#include

using namespace std;

const int INF=999999;

int n,m,s,t,g[20000],tot,dis[20000],q[20000],a[200100],b[200100];

bool vis[20000],used[20000],inq[20000];

struct edge

{

int t;

int next;

}e[500000];

void addedge(int a,int b)

{

tot+=1;

e[tot].t=b;

e[tot].next=g[a];

g[a]=tot;

}

void dfss(int x)

{

if(used[x])return;

used[x]=true;

for(int i=g[x];i;i=e[i].next) {

if(!used[e[i].t])dfss(e[i].t); }

}

void SPFA()

{

memset(dis,INF,sizeof(dis)); int head=0,tail=1;

dis[s]=0;

q[tail]=s;

inq[s]=true;

while(head!=tail)

{

head+=1;

head=((head-1)%20000)+1;

int x=q[head];

inq[x]=false;

for(int i=g[x];i;i=e[i].next)

{

if(dis[e[i].t]>dis[x]+1&&vis[e[i].t])//SPFA只走vis=true的点{

dis[e[i].t]=dis[x]+1;

if(!inq[e[i].t])

{

tail+=1;

tail=((tail-1)%20000)+1;

q[tail]=e[i].t;

inq[e[i].t]=true;

}

}

}

}

}

int main()

{

scanf("%d%d",&n,&m);

for(int i=1;i<=m;i++)

{

scanf("%d%d",&a[i],&b[i]);

addedge(b[i],a[i]);

}

scanf("%d%d",&s,&t);

dfss(t);//先反向搜一遍,与终点不连通的不会被走到

if(!used[s])

printf("-1");

else

{

memset(e,0,sizeof(e));

memset(g,0,sizeof(g));

tot=0;

for(int i=1;i<=m;i++)

addedge(a[i],b[i]);//再正向+边,判断,若其有一个边的终点不与终点连通,即不符合,FALSE

for(int i=1;i<=n;i++)

{

if(used[i])vis[i]=true;

for(int j=g[i];j;j=e[j].next)

if(!used[e[j].t])vis[i]=false;

}

SPFA();//SPFA只走vis=true的点printf("%d",dis[t]);

}

return 0;

}

转圈游戏//2013

https://www.360docs.net/doc/3e8241408.html,/problem/3285/

火柴排队

https://www.360docs.net/doc/3e8241408.html,/problem/3286/

积木大赛

https://www.360docs.net/doc/3e8241408.html,/problem/3288/

花匠

https://www.360docs.net/doc/3e8241408.html,/problem/3289/

游戏

快速幂。

火柴排队

#include

#include

#include

using namespace std;

int d[1000001];

long long ans=0;

void stablesort(int *a,int l,int mid,int r)

{

int i=l,j=mid+1,k=l;

while (i<=mid && j<=r)

if (a[i]<=a[j]) d[k++]=a[i++];

else { d[k++]=a[j++]; ans+=mid+1-i;ans=ans%99999997;} while (i<=mid)

d[k++]=a[i++];

while (j<=r)

d[k++]=a[j++];

for (int q=l;q<=r;q++) a[q]=d[q];

}

void merge(int *a,int l,int r)

{

{

int mid=(l+r)/2;

merge(a,l,mid);

merge(a,mid+1,r);

stablesort(a,l,mid,r);

}

}

int n,c[1000001];

struct data{

int xx,yy;}a[1000001],b[1000001];

inline int cmp(const void *a,const void *b)

{

if ( (*(data *)a) .xx < (*(data *)b) . xx )

return 1;

else return -1;

}

int main()

{

scanf("%d",&n);

for (int q=0;q

qsort(b,n,sizeof(data),cmp);

for (int q=0;q

c[b[q].yy]=a[q].yy;

merge(c,0,n-1);

printf("%d",ans);

return 0;

}

积木大赛

#include

#include

#include

using namespace std;

int n , first , next , ans;

int main()

{

while( scanf( "%d" , &n ) != EOF )(不用理会这个){

scanf( "%d" , &first );

ans = first;

for( i = 2 ; i <= n ; i++ )

{

scanf("%d",&next);

if( first <= next ){

ans += next - first;

first = next;

}

else

first = next;

}

printf( "%d\n" , ans );

}

return 0;

}

相关主题
相关文档
最新文档