#include<cstdio> #include<iostream> #include<algorithm> #include<vector> #include<tr1/unordered_map> #include<queue> #include<cstdlib> #include<list> #include<set> #include<map> #include<cmath> #define MP make_pair #define PB push_back #define s second #define f first #define PII pair<int,int> #define VPII vector <pair<int,int> > #define VI vector <int> #define abs(a) max((a),-(a)) #define LL long long #define LD long double #define ALL(x) x.begin(),x.end() #define PU putchar_unlocked #define GU getchar_unlocked #define DBG(x) cerr<<#x<<" "<<(x)<<endl; using namespace std; int a,b,e,f,n,m,mx,l,r,k; int wynik; char ch; int INF=1e9+1; const int MXN=1e6+4; int MOD=1e9+7; int c[MXN]; int d[MXN]; PII dp[MXN];//ile maxsymalnie drużyn, ile możliwości takich drużyn int byl[MXN]; void solve() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d%d",&c[i],&d[i]); } dp[0]=MP(0,1); int x=INF; for(int i=1;i<=n;i++) { a=-INF; int mi=INF,mx=0; x=min(x,i-c[i]+1); for(int j=i;j>=1;j--) { mi=min(d[j],mi); mx=max(c[j],mx); if(mi<mx)break; if(i-j+1>mi)break; // fprintf(stderr,"i: %d j: %d mi %d mx %d dl: %d\n",i,j,mi,mx,i-j+1); if(i+1<=mi+j&&i+1>=mx+j) { a=max(a,dp[j-1].f); } } mi=INF;mx=0; dp[i].f=a+1; for(int j=i;j>=1;j--) { mi=min(d[j],mi); mx=max(c[j],mx); if(mi<mx)break; if(i+1<=mi+j&&i+1>=mx+j) { if(a==dp[j-1].f)dp[i].s=(dp[i].s+dp[j-1].s)%MOD; } } // fprintf(stderr,"%d %d %d\n",i,dp[i].f,dp[i].s); } if(dp[n].f<=0) { puts("NIE"); } else printf("%d %d\n",dp[n].f,dp[n].s); } main() { solve(); }
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | #include<cstdio> #include<iostream> #include<algorithm> #include<vector> #include<tr1/unordered_map> #include<queue> #include<cstdlib> #include<list> #include<set> #include<map> #include<cmath> #define MP make_pair #define PB push_back #define s second #define f first #define PII pair<int,int> #define VPII vector <pair<int,int> > #define VI vector <int> #define abs(a) max((a),-(a)) #define LL long long #define LD long double #define ALL(x) x.begin(),x.end() #define PU putchar_unlocked #define GU getchar_unlocked #define DBG(x) cerr<<#x<<" "<<(x)<<endl; using namespace std; int a,b,e,f,n,m,mx,l,r,k; int wynik; char ch; int INF=1e9+1; const int MXN=1e6+4; int MOD=1e9+7; int c[MXN]; int d[MXN]; PII dp[MXN];//ile maxsymalnie drużyn, ile możliwości takich drużyn int byl[MXN]; void solve() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d%d",&c[i],&d[i]); } dp[0]=MP(0,1); int x=INF; for(int i=1;i<=n;i++) { a=-INF; int mi=INF,mx=0; x=min(x,i-c[i]+1); for(int j=i;j>=1;j--) { mi=min(d[j],mi); mx=max(c[j],mx); if(mi<mx)break; if(i-j+1>mi)break; // fprintf(stderr,"i: %d j: %d mi %d mx %d dl: %d\n",i,j,mi,mx,i-j+1); if(i+1<=mi+j&&i+1>=mx+j) { a=max(a,dp[j-1].f); } } mi=INF;mx=0; dp[i].f=a+1; for(int j=i;j>=1;j--) { mi=min(d[j],mi); mx=max(c[j],mx); if(mi<mx)break; if(i+1<=mi+j&&i+1>=mx+j) { if(a==dp[j-1].f)dp[i].s=(dp[i].s+dp[j-1].s)%MOD; } } // fprintf(stderr,"%d %d %d\n",i,dp[i].f,dp[i].s); } if(dp[n].f<=0) { puts("NIE"); } else printf("%d %d\n",dp[n].f,dp[n].s); } main() { solve(); } |