CodeForces 902A Visiting a Friend

Posted by koolaid on Sun, 17 Nov 2019 22:41:38 +0100

Title Link: CodeForces 902A Visiting a Friend




The first time I think about it, as long as 0~m has been visited, the second group of samples have not been used, WA

The second time, as long as 0~m has been visited and the left end of each interval is between the previous interval, wrong answer on test 3

I didn't understand QAQ

Make a list of everything

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
using namespace std;

struct point
{
    int st;		//Left end point 
    int en;		//Right endpoint 
}p[110];

int main()
{
    int n,m;
	scanf("%d%d",&n,&m);
	
    for(int i=1;i<=n;i++)
        scanf("%d%d",&p[i].st,&p[i].en);
    
    if(p[1].st!=0)	//First, if you don't start from 0, No
    {
    	printf("No\n");
    	return 0;
	}
	else
	{
		if(p[1].en>=m)	//The first one starts from 0 and is greater than or equal to m, directly Yes
		{
			printf("Yes\n");
			return 0;
		}
	}
	
	bool flag=false;

    for(int i=2;i<=n;i++)
    {
    	if(p[i].st>p[i-1].en)	//If No. 5 and No. 7 in the figure above occur, it is not possible to directly determine whether they are correct. Exit the cycle and give it to flag for judgment
            break;
    	
		if(p[i].en<p[i-1].en)	//If No. 1 in the figure above occurs, update the right endpoint; 2, 3, 4 do not need to be updated
            p[i].en = p[i-1].en;
        
        if(p[i].en>=m)
        {
            flag = true;
            break;
        }
    }
    
    if(flag)
        printf("Yes\n");
    else
        printf("No\n");
    return 0;
}

After understanding the above ideas, let's see the solution again: initialize x as 0, representing the farthest distance that can be transmitted, and then if x is > = the distance of the last transmission point, it means that the pig can continuously use the transmission and update the maximum transmission distance of X. If x ends up > = m, you can visit successfully.

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,a,b,x;
    while(cin>>n>>m)
    {
        x=0;
        for(int i=0; i<n; i++)
        {
            cin>>a>>b;
            if(x>=a)   ///The limit of each transmission should be > = the last transmission location
                x=max(x,b);///Update Max distance
        }
        printf("%s\n",x>=m?"YES":"NO"); ///If the last transmission distance is > = m, it is OK
    }
    return 0;
}