HDU 2073 今年暑假不AC
“是的。”
“那你干什么呢?”
“看世界杯呀,笨蛋!”
“@#$%^&*%...”
确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视了。
作为球迷,一定想看尽量多的完整的比赛,当然,作为新时代的好青年,你一定还会看一些其它的节目,比如新闻联播(永远不要忘记关心国家大事)、非常6+7、超级女生,以及王小丫的《开心辞典》等等,假设你已经知道了所有你喜欢看的电视节目的转播时间表,你会合理安排吗?(目标是能看尽量多的完整节目)
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
<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);
}
}
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.
The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.
Each of the next n lines contains two numbers xi, wi (0 ≤ xi ≤ 109, 1 ≤ wi ≤ 109) — the coordinate and the weight of a point. All xi are different.
Print a single number — the number of vertexes in the maximum clique of the given graph.
Examples
42 33 10 26 1
3
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.
#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);
}
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 4872 | Accepted: 1740 | Special Judge |
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
Lines 2..N+1: Line i+1 describes cow i's milking interval with two space-separated integers.
Lines 2..N+1: Line i+1 describes the stall to which cow i will be assigned for her milking period.
5
1 10
2 4
3 6
5 8
4 7
Sample Output
4
1
2
3
2
4
Here's a graphical schedule for this output:
Time 1 2 3 4 5 6 7 8 9 10 Stall 1 c1>>>>>>>>>>>>>>>>>>>>>>>>>>> Stall 2 .. c2>>>>>> c4>>>>>>>>> .. .. Stall 3 .. .. c3>>>>>>>>> .. .. .. .. Stall 4 .. .. .. c5>>>>>>>>> .. .. ..Other outputs using the same number of stalls are possible.
#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();
}
}