Friday, May 2, 2014

Crash [Codeforces RCC 2014 Warmup (Div. 2) Problem B]

B. Crash
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
During the "Russian Code Cup" programming competition, the testing system stores all sent solutions for each participant. We know that many participants use random numbers in their programs and are often sent several solutions with the same source code to check.
Each participant is identified by some unique positive integer k, and each sent solution A is characterized by two numbers: x — the number of different solutions that are sent before the first solution identical to A, and k — the number of the participant, who is the author of the solution. Consequently, all identical solutions have the same x.
It is known that the data in the testing system are stored in the chronological order, that is, if the testing system has a solution with number x (x > 0) of the participant with number k, then the testing system has a solution with number x - 1 of the same participant stored somewhere before.
During the competition the checking system crashed, but then the data of the submissions of all participants have been restored. Now the jury wants to verify that the recovered data is in chronological order. Help the jury to do so.
Input
The first line of the input contains an integer n (1 ≤ n ≤ 105) — the number of solutions. Each of the following n lines contains two integers separated by space x and k (0 ≤ x ≤ 1051 ≤ k ≤ 105) — the number of previous unique solutions and the identifier of the participant.
Output
A single line of the output should contain «YES» if the data is in chronological order, and «NO» otherwise.

Sample test(s)
input
2
0 1
1 1
output
YES
input
4
0 1
1 2
1 1
0 2
output
NO
input
4
0 1
1 1
0 1
0 2
output
YES
→ Source
import java.util.*;

public class Warmup {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int[] a = new int[100000];
        Arrays.fill(a, -1);
        for(int i=0;i<n;++i){
            int x = scan.nextInt();
            int k = scan.nextInt()-1;
            if(a[k]<x-1){
                System.out.println("NO");
                System.exit(0);
            }else{
                a[k]=Math.max(a[k], x);
            }
        }
        System.out.println("YES");
        
        
        /*  First failed attempt - (Come back later) - I still think this isn't gonna solve it in <1000ms
        Map<Integer, Vector> m = new HashMap<Integer, Vector>();
        Vector<Integer> v = new Vector<Integer>();
        
        
        for(int i=0;i<n;++i){
            a[i][0]=scan.nextInt();
            a[i][1]=scan.nextInt();
            
            if(!m.containsKey(a[i][1])){
                v = new Vector<Integer>();
                v.add(a[i][0]);
                m.put(a[i][1], v);
            }
            else{
                m.get(a[i][1]).add(a[i][0]);
            }
        }
        
     
        if(a[0][0]!=0){
            System.out.println("NO");
            System.exit(0);
        }
        
        for(Map.Entry<Integer, Vector> entry:m.entrySet()){
            Collections.sort(entry);
            if((Integer)m.get(entry).elementAt(0)!=0){
                System.out.println("NO");
                System.exit(0);
            }
            for(int i=1;i<m.get(entry).size();++i){
                if((Integer)m.get(entry).elementAt(i)!=(Integer)m.get(entry).elementAt(i+1)-1){
                    System.out.println("NO");
                    System.exit(0);
                }
            }
        }
        System.out.println("YES");
        */
        
        
     /*  Second failed attempt (time limit exceeded) - Can do nothing! 
        for(int i=1;i<n;++i){
            if(a[i][0]==0){
                continue;
            }
            
            if(a[i][0]>0){
                int t = a[i][0]-1;
                for(int j=i-1;j>=0;--j){
                    if(a[j][1]==a[i][1]&&a[j][0]==t){
                        --t;
                    }
                    if(t==-1)
                        break;
                }
                if(t!=-1){
                        System.out.println("NO");
                        System.exit(0);
                }
            }
        }
        System.out.println("YES");*/
    }
}

No comments:

Post a Comment