CodeVS 线段覆盖1~5

Posted on 2016-09-29 16:01  yyjxx2010xyu  阅读(102)  评论(0编辑  收藏  举报
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int Maxn=2000100;
 4 struct Info{int l,r;}P[Maxn];
 5 int n,Cnt,F[Maxn];
 6 map<int,int> M;
 7 inline bool Cmp(Info A,Info B) {return A.r<B.r;}
 8 inline int Max(int x,int y) {return x>y?x:y;}
 9 inline void Swap(int &x,int &y) {int t=x;x=y;y=t;}
10 int main()
11 {
12 
13     scanf("%d",&n); Cnt=0;
14     for (int i=1;i<=n;i++) 
15     {
16         scanf("%d%d",&P[i].l,&P[i].r);
17         if (P[i].l>P[i].r) Swap(P[i].l,P[i].r);
18         M[P[i].l]=1,M[P[i].r]=1;
19     }
20     for (map<int,int>::iterator it=M.begin();it!=M.end();it++) it->second=++Cnt;
21     for (int i=1;i<=n;i++) P[i].l=M[P[i].l],P[i].r=M[P[i].r];
22     sort(P+1,P+n+1,Cmp); int j=1;
23     for (int i=1;i<=Cnt;i++)
24     {
25         while (j<=n && P[j].r==i)
26         {
27             F[i]=Max(F[i],F[P[j].l]+1);
28             j++;
29         }
30         F[i]=Max(F[i],F[i-1]);
31     }
32     printf("%d\n",F[Cnt]);
33     return 0;
34 }
线段覆盖1 
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int Maxn=2000100;
 4 struct Info{int l,r;}P[Maxn];
 5 int n,Cnt,F[Maxn];
 6 map<int,int> M;
 7 inline bool Cmp(Info A,Info B) {return A.r<B.r;}
 8 inline int Max(int x,int y) {return x>y?x:y;}
 9 inline void Swap(int &x,int &y) {int t=x;x=y;y=t;}
10 int main()
11 {
12 
13     scanf("%d",&n); Cnt=0;
14     for (int i=1;i<=n;i++) 
15     {
16         scanf("%d%d",&P[i].l,&P[i].r);
17         if (P[i].l>P[i].r) Swap(P[i].l,P[i].r);
18         M[P[i].l]=1,M[P[i].r]=1;
19     }
20     for (map<int,int>::iterator it=M.begin();it!=M.end();it++) it->second=++Cnt;
21     for (int i=1;i<=n;i++) P[i].l=M[P[i].l],P[i].r=M[P[i].r];
22     sort(P+1,P+n+1,Cmp); int j=1;
23     for (int i=1;i<=Cnt;i++)
24     {
25         while (j<=n && P[j].r==i)
26         {
27             F[i]=Max(F[i],F[P[j].l]+1);
28             j++;
29         }
30         F[i]=Max(F[i],F[i-1]);
31     }
32     printf("%d\n",F[Cnt]);
33     return 0;
34 }
线段覆盖2
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int Maxn=20100;
 4 struct Info{int l,r,w;}P[Maxn];
 5 int n,Cnt,F[Maxn];
 6 map<int,int> M;
 7 inline bool Cmp(Info A,Info B) {return A.r<B.r;}
 8 inline int Max(int x,int y) {return x>y?x:y;}
 9 inline void Swap(int &x,int &y) {int t=x;x=y;y=t;}
10 int main()
11 {
12 
13     scanf("%d",&n); Cnt=0;
14     for (int i=1;i<=n;i++) 
15     {
16         scanf("%d%d%d",&P[i].l,&P[i].r,&P[i].w);
17         if (P[i].l>P[i].r) Swap(P[i].l,P[i].r);
18         M[P[i].l]=1,M[P[i].r]=1;
19     }
20     for (map<int,int>::iterator it=M.begin();it!=M.end();it++) it->second=++Cnt;
21     for (int i=1;i<=n;i++) P[i].l=M[P[i].l],P[i].r=M[P[i].r];
22     sort(P+1,P+n+1,Cmp); int j=1;
23     for (int i=1;i<=Cnt;i++)
24     {
25         while (j<=n && P[j].r==i)
26         {
27             F[i]=Max(F[i],F[P[j].l]+P[j].w);
28             j++;
29         }
30         F[i]=Max(F[i],F[i-1]);
31     }
32     printf("%d\n",F[Cnt]);
33     return 0;
34 }
线段覆盖3
 1 #include <bits/stdc++.h>
 2 #define LL long long
 3 using namespace std;
 4 const int Maxn=1000010;
 5 const int Inf=0x3f3f3f3f;
 6 struct Info{int l,r,w;}P[Maxn];
 7 int n,Cnt,a[Maxn<<1];
 8 LL F[1500010];
 9 inline bool Cmp(Info A,Info B) {return A.r<B.r;}
10 inline int Min(int x,int y) {return x>y?y:x;}
11 inline LL Max(LL x,LL y) {return x>y?x:y;}
12 inline void Swap(int &x,int &y) {int t=x;x=y;y=t;}
13 inline void Get_Int(int &x)
14 {
15     x=0; register char ch=getchar(); int f=1;
16     while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
17     while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} x*=f;
18 }
19 int main()
20 {
21 
22     Get_Int(n); 
23     for (int i=1;i<=n;i++) 
24     {
25         Get_Int(P[i].l),Get_Int(P[i].r),Get_Int(P[i].w);
26         if (P[i].l>P[i].r) Swap(P[i].l,P[i].r);
27         a[i*2-1]=P[i].l,a[i*2]=P[i].r;
28     }
29     sort(a+1,a+n*2+1); Cnt=1;
30     for (int i=2;i<=2*n;i++) if (a[i]!=a[Cnt]) a[++Cnt]=a[i];
31     for (int i=1;i<=n;i++) P[i].l=lower_bound(a+1,a+Cnt+1,P[i].l)-a,P[i].r=lower_bound(a+1,a+Cnt+1,P[i].r)-a;
32     
33     
34     sort(P+1,P+n+1,Cmp); int j=1;
35     for (int i=1;i<=Cnt;i++)
36     {
37         while (j<=n && P[j].r==i)
38         {
39             F[i]=Max(F[i],F[P[j].l]+(LL)P[j].w);
40             j++;
41         }
42         F[i]=Max(F[i],F[i-1]);
43     }
44     printf("%lld\n",F[Cnt]);
45     return 0;
46 }
线段覆盖4
 1 #include <bits/stdc++.h>
 2 #define LL long long
 3 using namespace std;
 4 const int Maxn=1000010;
 5 const int Inf=0x3f3f3f3f;
 6 struct Info{int l,r,w;}P[Maxn];
 7 int n,Cnt,a[Maxn<<1];
 8 LL F[Maxn<<1];
 9 inline bool Cmp(Info A,Info B) {return A.r<B.r;}
10 inline int Min(int x,int y) {return x>y?y:x;}
11 inline LL Max(LL x,LL y) {return x>y?x:y;}
12 inline void Swap(int &x,int &y) {int t=x;x=y;y=t;}
13 inline void Get_Int(int &x)
14 {
15     x=0; register char ch=getchar(); int f=1;
16     while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
17     while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} x*=f;
18 }
19 int main()
20 {
21 
22     Get_Int(n); 
23     for (int i=1;i<=n;i++) 
24     {
25         Get_Int(P[i].l),Get_Int(P[i].r),Get_Int(P[i].w);
26         if (P[i].l>P[i].r) Swap(P[i].l,P[i].r);
27         a[i*2-1]=P[i].l,a[i*2]=P[i].r;
28     }
29     sort(a+1,a+n*2+1); Cnt=1;
30     for (int i=2;i<=2*n;i++) if (a[i]!=a[Cnt]) a[++Cnt]=a[i];
31     for (int i=1;i<=n;i++) P[i].l=lower_bound(a+1,a+Cnt+1,P[i].l)-a,P[i].r=lower_bound(a+1,a+Cnt+1,P[i].r)-a;
32     
33     
34     sort(P+1,P+n+1,Cmp); int j=1;
35     for (int i=1;i<=Cnt;i++)
36     {
37         while (j<=n && P[j].r==i)
38         {
39             F[i]=Max(F[i],F[P[j].l]+(LL)P[j].w);
40             j++;
41         }
42         F[i]=Max(F[i],F[i-1]);
43     }
44     printf("%lld\n",F[Cnt]);
45     return 0;
46 }
线段覆盖5

 不会贪心只会DP,Code都类似只不过中间用STL离散化被卡常了..