// Author: Adam Krasuski #include <cstdio> #include <algorithm> using namespace std; struct best{ int teams; int no_of_ways; }; int c[1000009]; int d[1000009]; best bests[1000009]; #define MOD 1000000007 int main(){ int n; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d %d",&c[i],&d[i]); } best b={0,1}; bests[0]=b; for(int i=1;i<n+1;i++){ //i is an index of currently processed kid int max_c=0; int min_d=1e9; int highest_teams=0; int curr_no_of_ways=0; for(int j=c[i-1];j<=d[i-1];j++){ if(j>i){break;} max_c=max(max_c,c[i-j]); min_d=min(min_d,d[i-j]); if(j<max_c){continue;} if(j>min_d){break;} if(bests[i-j].teams>highest_teams){ highest_teams=bests[i-j].teams; curr_no_of_ways=bests[i-j].no_of_ways; } else if(bests[i-j].teams==highest_teams){ curr_no_of_ways+=bests[i-j].no_of_ways; } } best b={highest_teams+1,curr_no_of_ways}; if(curr_no_of_ways==0){ b.teams=0; } bests[i]=b; //printf("Best[%d]={%d,%d}\n",i,bests[i].teams,bests[i].no_of_ways); } if(bests[n].no_of_ways==0){ printf("NIE\n"); } else{ printf("%d %d\n",bests[n].teams,bests[n].no_of_ways); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | // Author: Adam Krasuski #include <cstdio> #include <algorithm> using namespace std; struct best{ int teams; int no_of_ways; }; int c[1000009]; int d[1000009]; best bests[1000009]; #define MOD 1000000007 int main(){ int n; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d %d",&c[i],&d[i]); } best b={0,1}; bests[0]=b; for(int i=1;i<n+1;i++){ //i is an index of currently processed kid int max_c=0; int min_d=1e9; int highest_teams=0; int curr_no_of_ways=0; for(int j=c[i-1];j<=d[i-1];j++){ if(j>i){break;} max_c=max(max_c,c[i-j]); min_d=min(min_d,d[i-j]); if(j<max_c){continue;} if(j>min_d){break;} if(bests[i-j].teams>highest_teams){ highest_teams=bests[i-j].teams; curr_no_of_ways=bests[i-j].no_of_ways; } else if(bests[i-j].teams==highest_teams){ curr_no_of_ways+=bests[i-j].no_of_ways; } } best b={highest_teams+1,curr_no_of_ways}; if(curr_no_of_ways==0){ b.teams=0; } bests[i]=b; //printf("Best[%d]={%d,%d}\n",i,bests[i].teams,bests[i].no_of_ways); } if(bests[n].no_of_ways==0){ printf("NIE\n"); } else{ printf("%d %d\n",bests[n].teams,bests[n].no_of_ways); } } |