选择不相交区间 小结

来源:互联网 发布:樱桃软件 编辑:程序博客网 时间:2024/05/19 00:14

HDU 2073 今年暑假不AC

Problem Description
“今年暑假不AC?”
“是的。”
“那你干什么呢?”
“看世界杯呀,笨蛋!”
“@#$%^&*%...”

确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视了。
作为球迷,一定想看尽量多的完整的比赛,当然,作为新时代的好青年,你一定还会看一些其它的节目,比如新闻联播(永远不要忘记关心国家大事)、非常6+7、超级女生,以及王小丫的《开心辞典》等等,假设你已经知道了所有你喜欢看的电视节目的转播时间表,你会合理安排吗?(目标是能看尽量多的完整节目)

   Input
  输入数据包含多个测试实例,每个测试实例的第一行只有一个整数n(n<=100),表示你喜欢看的节目的总数,然后是n行数据,每行包   括两个数据Ti_s,Ti_e (1<=i<=n),分别表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。n=0表示输入    结束,不做处理。

  Output
   对于每个测试实例,输出能完整看到的电视节目的个数,每个测试实例的输出占一行。

 Sample Input

12

1 3

3 4

0 7

3 8

15 19

15 20

10 15

8 18

6 12

5 10

4 14

2 9

0

Sample Output
5

分析
    求最多不重叠区间的个数。
    对区间的右端点从小到大排序,然后从左到右遍历一遍,这样为了尽多得选区间

<span style="font-size:18px;">#include<cstdio>#include<algorithm>#include<cstring>#include<string>#include<cmath>using namespace std;const int inf=1<<28;struct sb{    int a,b;};bool  cmp(sb a,sb b){    return a.b<b.b;}int main(){    int n;    while(scanf("%d",&n),n)    {        sb z[100+10];        for(int i=0;i<n;i++)            scanf("%d %d",&z[i].a,&z[i].b);        sort(z,z+n,cmp);        int t=0,sum=0;        for(int i=0;i<n;i++)            if(z[i].a>=t)        {            t=z[i].b;            sum++;        }//        printf("%d\n",sum);    }}

CF   D. Clique Problem

time limit per test
2 seconds   
memory limit per test
256 megabytes
input
standard input      
output
standard output

The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.

Consider n distinct points on a line. Let the i-th point have the coordinate xi and weight wi. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |xi - xj| ≥ wi + wj.

Find the size of the maximum clique in such graph.

Input

The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.

Each of the next n lines contains two numbers xiwi (0 ≤ xi ≤ 109, 1 ≤ wi ≤ 109) — the coordinate and the weight of a point. All xi are different.

Output

Print a single number — the number of vertexes in the maximum clique of the given graph.

Examples

input
4
2 3
3 1
0 2
6 1
output
3
Note

If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!

The picture for the sample test.


题意:有N个点在数轴上,每个点有一个坐标(x)和以个权值(w),求最大的集合的大小,且其中任意两点必须满足|xi - xj| ≥ wi + wj.,所以也就是求最多不重叠区间的个数。

#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>#include<cmath>#include<vector>using namespace std;struct sb{    int L,R;} zu[21*10000];bool cmp(sb a,sb b){    return a.R<b.R;}int main(){    int n;    scanf("%d",&n);    for(int i=1; i<=n; i++)    {        int x,w;        scanf("%d %d",&x,&w);        zu[i].L=x-w;        zu[i].R=x+w;    }    sort(zu+1,zu+1+n,cmp);    int temp=zu[1].R;    int num=1;    for(int i=2; i<=n; i++)        if(temp<=zu[i].L)        {            temp=zu[i].R;            num++;        }    printf("%d\n",num);}

 Stall Reservations

Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4872 Accepted: 1740 Special Judge
Description

Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a reservation system to determine which stall each cow can be assigned for her milking time. Of course, no cow will share such a private moment with other cows. 


Help FJ by determining:
  • The minimum number of stalls required in the barn so that each cow can have her private milking period
  • An assignment of cows to these stalls over time
Many answers are correct for each test dataset; a program will grade your answer.

Input

Line 1: A single integer, N 

Lines 2..N+1: Line i+1 describes cow i's milking interval with two space-separated integers.
Output

Line 1: The minimum number of stalls the barn must have. 

Lines 2..N+1: Line i+1 describes the stall to which cow i will be assigned for her milking period.

Sample Input
51 102 43 65 84 7
Sample Output
412324

Hint
Explanation of the sample: 

Here's a graphical schedule for this output: 

Time     1  2  3  4  5  6  7  8  9 10Stall 1 c1>>>>>>>>>>>>>>>>>>>>>>>>>>>Stall 2 .. c2>>>>>> c4>>>>>>>>> .. ..Stall 3 .. .. c3>>>>>>>>> .. .. .. ..Stall 4 .. .. .. c5>>>>>>>>> .. .. ..
Other outputs using the same number of stalls are possible.

题意:给你一个整数N表示牛的数量,然后给出N个区间的(每头牛挤奶的时间),两头牛不能同时在一个摊位挤奶,问最少需要多少个摊位
分析:先按区间左端点从小排序,然后用优先队列(按右端点从小维护),一旦发现可以共用一个摊位的牛,立即更新。

#include<cstdio>#include<cstring>#include<cstring>#include<iostream>#include<algorithm>#include<cmath>#include<vector>#include<queue>using namespace std;struct sb{    int L,R,num;} zu[5*10000+10];int zz[5*10000+10];bool operator < (const sb &a,const sb &b){    return  a.R>b.R;///右端点从小维护}bool cmp(sb a,sb b){    return a.L<b.L;///左端点从小排序int main(){    int n;    while(scanf("%d",&n)==1)    {        priority_queue<sb>Q;        memset(zz,0,sizeof(zz));        for(int i=1; i<=n; i++)        {            int x,w;            scanf("%d %d",&x,&w);            zu[i].L=x;            zu[i].R=w;            zu[i].num=i;///记录位置        }//        sort(zu+1,zu+1+n,cmp);//        Q.push(zu[1]);        int ans=1;        zz[zu[1].num]=ans;//        for(int i=2; i<=n; i++)            if(!Q.empty()&&Q.top().R<zu[i].L)///可以共用的            {                zz[zu[i].num]=zz[Q.top().num];///记录摊位号                Q.pop();       ///更新摊位上最后一个牛                Q.push(zu[i]);            }            else            {                ans++;                zz[zu[i].num]=ans;///没牛能和它共用                Q.push(zu[i]);            }//        printf("%d\n",ans);        for(int i=1; i<=n; i++)            printf("%d\n",zz[i]);        while(!Q.empty())            Q.pop();    }}


0 0
原创粉丝点击