Tranvick

Untitled

Mar 9th, 2012
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | None | 0 0
  1. #pragma comment(linker, "/STACK:65777216")
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cstdlib>
  5. #include <ctime>
  6. #include <cstdio>
  7. #include <algorithm>
  8. #include <cmath>
  9. #include <vector>
  10. #include <set>
  11. #include <stack>
  12. #include <map>
  13. #include <queue>
  14. #include <string>
  15. #include <memory.h>
  16. #include <iterator>
  17. #define y1 trololoy1
  18. #define y0 trololoy0
  19. #define mem(A,X) memset(A,X,sizeof(A))
  20. #define memo(A) memset(A,0,sizeof(A))
  21. #define forn(I,B) for (int I=1;I<=(B);I++)
  22. #define forg(H,V) for (int H=first[V];h;h=next[H])
  23. #define rep(I,B) for (int I=0;I<(B);I++)
  24. #define labs(X) (((X)>0)?(X):(-(X)))
  25. #define ropen(X) freopen(X,"r",stdin)
  26. #define wopen(X) freopen(X,"w",stdout)
  27. #define rwopen(X) freopen(X".in","r",stdin);freopen(X".out","w",stdout)
  28. #define pb push_back
  29. #define mp make_pair
  30. #define all(X) (X).begin(),(X).end()
  31. #define sqr(X) ((X)*(X))
  32.  
  33. using namespace std;
  34.  
  35. typedef pair <int,int> pii;
  36. typedef double ld;
  37. typedef long long ll;
  38. typedef pair <ll,ll> pll;
  39. typedef vector<int> vi;
  40. const int N=5555;
  41. const int INF=111111111;
  42. const double eps=1e-9;
  43. const double pi=3.14159265358979;
  44.  
  45. struct seg{
  46.     int bg,en,id;
  47. } a[N],b[N],w[N];
  48.  
  49. struct qs{
  50.     int bg,en,id,ans;
  51. } t[N];
  52.  
  53. inline bool cmp1(seg a, seg b){
  54.     return a.en-a.bg<b.en-b.bg;
  55. }
  56.  
  57. inline bool cmp2(seg a,seg b){
  58.     return a.en<b.en;
  59. }
  60.  
  61. int k,n,f[N],g[N],ptr,p[N],q[N],l,r,mx[N];
  62. bool used[N];
  63.  
  64. inline int bin_search(int x,int pos){
  65.     int l=0,r=pos;
  66.     if (t[0].en>x) return -1;
  67.     while (l<r){
  68.         int m=(l+r)/2;
  69.         if (t[m].en<=x) l=m+1;
  70.         else r=m;
  71.     }
  72.     if (t[l].en>x || l>pos) --l;
  73.     return l;
  74. }
  75.  
  76. inline void modify(int pos,int value){
  77.     for(;pos<n;pos|=pos+1) if (mx[pos]<0 || g[mx[pos]]<g[value]) mx[pos]=value;
  78. }
  79.  
  80. inline pii get_max(int pos){
  81.     int best=-1;
  82.     for(;pos>=0;pos=(pos&(pos+1))-1) if (mx[pos]>=0 && (best==-1 || g[mx[pos]]>g[best])) best=mx[pos];
  83.     return mp(g[best],best);
  84. }
  85.  
  86. inline void calc_g(){
  87.     memo(g);
  88.     if (ptr==0) return;
  89.     g[0]=t[0].ans;
  90.     p[0]=-1;
  91.     mem(mx,128);
  92.     modify(0,0);
  93.     forn(i,ptr-1){
  94.         g[i]=t[i].ans;
  95.         p[i]=-1;
  96.         int bs=bin_search(t[i].bg,i-1);
  97.         if (bs!=-1){
  98.             pii tt=get_max(bs);
  99.             if (tt.first+t[i].ans>g[i]){
  100.                 g[i]=tt.first+t[i].ans;
  101.                 p[i]=tt.second;
  102.             }
  103.         }
  104.         modify(i,i);
  105.     }
  106. }
  107.  
  108. void rec_ans(){
  109.     w[n]=(seg){1,k,n};
  110.     q[++r]=n;
  111.     while (l!=r){
  112.         int v=q[++l];
  113.         ptr=0;
  114.         rep(j,n) if (b[j].bg>=w[v].bg && b[j].en<=w[v].en && b[j].id!=w[v].id){
  115.             t[ptr].bg=b[j].bg;
  116.             t[ptr].en=b[j].en;
  117.             t[ptr].id=b[j].id;
  118.             t[ptr].ans=f[b[j].id];
  119.             ++ptr;
  120.         }
  121.         if (ptr==0) continue;
  122.         calc_g();
  123.         for(v=max_element(g,g+ptr)-g;v!=-1;v=p[v]){
  124.             used[t[v].id]=1;
  125.             q[++r]=t[v].id;
  126.         }
  127.     }
  128. }
  129.  
  130. int main(){
  131.     ropen("input.txt");
  132.     wopen("output.txt");
  133.     scanf("%d%d",&k,&n);
  134.     rep(i,n){
  135.         scanf("%d%d",&a[i].bg,&a[i].en);
  136.         if (a[i].bg>a[i].en) swap(a[i].bg,a[i].en);
  137.         a[i].id=i;
  138.         w[i]=b[i]=a[i];
  139.     }
  140.     sort(a,a+n,cmp1);
  141.     sort(b,b+n,cmp2);
  142.     rep(i,n){
  143.         ptr=0;
  144.         rep(j,n) if (b[j].bg>=a[i].bg && b[j].en<=a[i].en && b[j].id!=a[i].id){
  145.             t[ptr].bg=b[j].bg;
  146.             t[ptr].en=b[j].en;
  147.             t[ptr].id=b[j].id;
  148.             t[ptr].ans=f[b[j].id];
  149.             ++ptr;
  150.         }
  151.         calc_g();
  152.         f[a[i].id]=*max_element(g,g+ptr)+1;
  153.     }
  154.     ptr=0;
  155.     rep(j,n){
  156.         t[ptr].bg=b[j].bg;
  157.         t[ptr].en=b[j].en;
  158.         t[ptr].id=b[j].id;
  159.         t[ptr].ans=f[b[j].id];
  160.         ++ptr;
  161.     }
  162.     calc_g();
  163.     printf("%d\n",n-*max_element(g,g+n));
  164.     rec_ans();
  165.     rep(i,n) if (!used[i]) printf("%d ",i+1);
  166.     return 0;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment