Advertisement
Guest User

Untitled

a guest
Oct 14th, 2011
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.53 KB | None | 0 0
  1. //This code was writen by Alexander Dryapko (sdryapko)
  2. #include<sstream>
  3. #include<iostream>
  4. #include<stdio.h>
  5. #include<math.h>
  6. #include<stdlib.h>
  7. #include<algorithm>
  8. #include<vector>
  9. #include<map>                              
  10. #include<set>              
  11. #include<string>
  12. #include<string.h>  
  13. #define gcd(a,b) __gcd((a),(b));
  14. #define sqr(a) ((a)*(a))
  15. #define odd(a) ((a)&1)
  16. const int maxi=2000000000;              
  17. const int maxq=1000000000;
  18. const double eps=1e-10;      
  19. const double pi=3.1415926535897932;
  20. const double inf=1e+18;
  21. const int mo=1000000007;
  22. using namespace std;
  23. long long f[55][55][111];
  24. vector<long long> ansj,ansk;
  25.  
  26. struct T {
  27.     int x,nom;
  28.     long long l,r;
  29. }  a[1111];
  30. struct TT {
  31.     int i,j,k;
  32. } p[55][55][111];
  33. int qi,qk,qj,n,m,fi,fj,fk;
  34. long long ko,kk;
  35. bool my(T x,T y) {
  36.     if (x.x>y.x) return false; else return true;
  37. }
  38. int main(){                
  39.         //freopen("input.txt","r",stdin);      
  40.        // freopen("output.txt","w",stdout);
  41.         cin>>n>>m>>ko;
  42.         for (int i=1;i<=m;i++) cin>>a[i].l>>a[i].r>>a[i].x;
  43.         for (int i=1;i<=m;i++) a[i].nom=i;
  44.         sort(a+1,a+m+1,&my);
  45.         for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) for (int k=0;k<=a[j].r-a[j].l;k++) {
  46.                  if (i==1) {
  47.                         f[i][j][k]=max(f[i][j][k],a[j].l+k);
  48.                         p[i][j][k].i=i-1;
  49.                         p[i][j][k].j=0;
  50.                         p[i][j][k].k=0;
  51.                         continue;
  52.                  }
  53.                                                                                    
  54.                  if ((k+a[j].l)%ko==0)  {
  55.                         kk=(k+a[j].l)/ko;
  56.                         for (int jjj=1;jjj<j;jjj++) if (a[jjj].x<a[j].x&&kk>=a[jjj].l&&kk<=a[jjj].r) {
  57.                             if (f[i][j][k]<f[i-1][jjj][kk-a[jjj].l]+k+a[j].l) {
  58.                                 f[i][j][k]=f[i-1][jjj][kk-a[jjj].l]+k+a[j].l;
  59.                                 p[i][j][k].i=i-1;
  60.                                 p[i][j][k].j=jjj;
  61.                                 p[i][j][k].k=kk-a[jjj].l;
  62.                             }
  63.                         }
  64.                                                    
  65.                                
  66.                  }
  67.                  kk=(k+a[j].l)-ko;
  68.                  if (kk<0) continue;
  69.                  for (int jjj=1;jjj<j;jjj++) if (a[jjj].x<a[j].x&&kk>=a[jjj].l&&kk<=a[jjj].r) {
  70.                             if (f[i][j][k]<f[i-1][jjj][kk-a[jjj].l]+k+a[j].l) {
  71.                                 f[i][j][k]=f[i-1][jjj][kk-a[jjj].l]+k+a[j].l;
  72.                                 p[i][j][k].i=i-1;
  73.                                 p[i][j][k].j=jjj;
  74.                                 p[i][j][k].k=kk-a[jjj].l;
  75.                             }
  76.                  }
  77.                  
  78.         //      cout<<i<<' '<<j<<' '<<k<<' '<<f[i][j][k]<<endl;
  79.              
  80.                  
  81.         }
  82.         long long ans=0;
  83.        
  84.         for (int j=1;j<=m;j++) for (int k=0;k<=a[j].r-a[j].l;k++) {
  85.         //  cout<<j<<' '<<k<<endl;
  86.         //  cout<<f[n][j][k]<<endl;
  87.             if (f[n][j][k]>ans) {
  88.                 ans=f[n][j][k];
  89.            
  90.                 fi=n;
  91.                 fj=j;
  92.                 fk=k;
  93.             }
  94.         }
  95.         if (ans==0) {
  96.             puts("NO");
  97.             return 0;
  98.         }
  99.         while (fi) {
  100.             ansj.push_back(fj);
  101.             ansk.push_back(a[fj].l+fk);
  102.             qi=fi;
  103.             qj=fj;
  104.             qk=fk;
  105.             fi=p[qi][qj][qk].i;
  106.             fj=p[qi][qj][qk].j;
  107.             fk=p[qi][qj][qk].k;
  108.         }      
  109.         reverse(ansj.begin(),ansj.end());
  110.         reverse(ansk.begin(),ansk.end());
  111.         if (ansj.size()<n) {
  112.             puts("NO");
  113.             return 0;
  114.         }
  115.  
  116.         puts("YES");
  117.         for (int i=0;i<ansj.size();i++) cout<<a[ansj[i]].nom<<' '<<ansk[i]<<endl;
  118.         return 0;
  119. }
  120.      
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement