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.
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 ≤ 105; 1 ≤ k ≤ 105) — the number of previous unique solutions and the identifier of the participant.
A single line of the output should contain «YES» if the data is in chronological order, and «NO» otherwise.
2 0 1 1 1
YES
4 0 1 1 2 1 1 0 2
NO
4 0 1 1 1 0 1 0 2
YES
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
int a,b,d;
}s[100110];
bool cmp(node a,node b)
{
if(a.b!=b.b)return a.b<b.b;
else return a.d<b.d;
}
int main()
{
int n,i;
scanf("%d",&n);
int flag=1;
for(i=0;i<n;i++)
{
scanf("%d%d",&s[i].a,&s[i].b);
s[i].d=i;
}
sort(s,s+n,cmp);
int jiance=-1,jijie;
for(i=0;i<n;i++)
{
if(jiance!=s[i].b){
jijie=0;
jiance=s[i].b;
if(s[i].a!=0){
flag=0;
}
}
else {
if(s[i].a==jijie+1){
jijie++;
continue;
}
else if(s[i].a<=jijie)
continue;
else flag=0;
}
}
if(flag)puts("YES");
else puts("NO");
}