codeforce #578(DIV.2)

codeforce #578(DIV.2)

A.hotel register

Input ‘L’,register in the first room from the left side.
The same with ‘R’.
Input number 1-10(note that it’s not 0-9),log out the corresponding room.

key:

Define a[10] to represent the availability of the 10 rooms.
Define s[20000] globally to record the operations.
Go through s.
1.s[i]=='left'
2. s[i]=='right'
3. isdigit(s[i])

code
#include<stdio.h>
#include<ctype.h>
int a[10];
char s[200000]; //use global variables in case of the overflow of stacks
int main(void)
{
	int n;
	int i,j;

	scanf("%d",&n);
	scanf("%s",s);
	for(i=0;i<n;i++)
	{
		if(s[i]=='L')
		{
			for(j=0;j<10;j++)
			{
				if(a[j]==0)
				{
					a[j]=1;
				    break;     //ERROR!Forget to break;
				}
			}
		}
		else if(s[i]=='R')
		{
			for(j=9;j>=0;j--)
			{
				if(a[j]==0)
				{
					a[j]=1;
				    break;
				}
			}
		}
		else if(isdigit(s[i]))
		{
			a[s[i]-'0']=0;
		}
     }
	for(i=0;i<10;i++)
	{
		printf("%d",a[i]);
	}
	}

B.travel on blocks

Mike walk on the top of stacks of blocks of same size with a huge bag.
use h[i] to represent the height of each column.
Mike can go to the next column if and only if |h[i]-h[i+1]|<=k
There are 3 operations with no limits of the times they are done.
1.put blocks in the bag.The pack has infinite capacity.
2.fetch blocks from the bag and put them on the current column.
3.go to the next column if |h[i]-h[i+1]|<=k and i<n, i=1,2...n
Judge if Mike can make it through h1-h10.

key

To ensure Mike can cover the longest distance, we should collect as much blocks as possible.So we collect blocks repeatedly until h[i]=h[i+1]-k. In this case we need to first compare first h[i] and h[i+1]-k,thenh[i+1]-k and 0

code
#include<stdio.h>
#include<string.h>
#include<math.h>
int h[200];
int main(void)
{
	int i,j;
	int t;
	int m,n,k;
	int d;
	int pack;
	scanf("%d",&t);
	for(i=0;i<t;i++)
	{

		scanf("%d%d%d",&n,&m,&k);
		pack=m;
		for(j=1;j<=n;j++)
		{
			scanf("%d",&h[j]);
		}
		for(j=1;j<n;j++)
		{	
		       if(h[j]>h[j+1]-k)
				{
				d=(h[j]<(h[j]-(h[j+1]-k)))?h[j]:(h[j]-(h[j+1]-k));
			   	pack+=d;
				h[j]-=d;
				}
				else if(h[j]<=h[j+1]-k)
				{
				d=h[j+1]-k-h[j];
			   	pack-=d;
				h[j]+=d;
				}
				if(pack<0)
				{
					printf("NO\n");
					break;
				}
		}
		if(pack>=0)printf("YES\n");
		
	}
}

C.travel in clock map

A circle has a inner and outer ring represent by 1 and 2. They are divided into n and m parts respectively.Each part is seperated by a wall. sx,ex stands for the serial number 1/2 representing the loop while sy,ey stand of the xth part.Given sx,sy,ex,ey,decide if (sx,sy)and (ex,ey) are connected.

key

If one inner wall and outer wall are in the same line, then the 2 distinctions the line divides are disconnected.
So we first calculate gcd(n,m), then devide n and m by gcd to figure out the number of parts of inner and outer ring.

code
#include<stdio.h>
int gcd(int x, int y)
{	
	int z = y;
	while(x%y!=0)      //ERROR 1:forget gcd
	{
		z = x%y;
		x = y;
		y = z;	
	}
	return z;
}

int main(void)
{
	long long int n,m;   //ERROR2:ignore the scope of the parameters so          fail to use lld 
	int q;
	int pi,po;
	int ti,to;
	int g;
	int i,j;
	int sx,sy,ex,ey; 
	scanf("%lld%lld%d",&n,&m,&q);
	g=gcd(n,m);
	pi=n/g;
	po=m/g;
	for(i=0;i<q;i++)
	{
		scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
		if(sx==1)
		{
			ti=(sy-1)/pi; //ERROR 3:ignore that sy,ey start from 1 which will cause mistakes when we mark the part.So we need to descrement it

		}
		else if(sx==2) //ERROR4:idnore that sx can equal sy 
		{
			ti=(sy-1)/po;
		}
		if(ex==1)
		{
			to=(ey-1)/pi;

		}
		else if(ex==2)
		{
			to=(ey-1)/po;
		}
		
		if(ti==to)puts("YES");
		else puts("NO");
	}
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值