#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
int party[100][2],vis[100],p,s,e;
int main()
{
int t=1,i,j,k;
while(cin>>p,p)
{
for(i=0;i<p;++i)
{
cin>>s>>e;
party[i][0]=8+(s-8)*2; //把一刻钟拆成两半
party[i][1]=8+(e-8)*2;
}
memset(vis,0,sizeof(vis));
int res=0;
for(i=8;i<40;++i)
{
int end=100;
for(j=0;j<p;++j)
if(vis[j]==0&&party[j][0]<=i&&party[j][1]>i&&party[j][1]<end) //在所有未参加的party中优先选择覆盖 i 这一时刻且最早结束
{
end=party[j][1];
k=j;
}
if(end<100)
{
vis[k]=1;
res++;
}
}
printf("On day %d Emma can attend as many as %d parties.\n",t++,res);
}
return 0;
}
错误的策略
#include<iostream> //WA
#include<stdio.h>
#include<algorithm>
#include<cstring>
using namespace std;
struct Node
{
int st,ed;
bool operator<(const Node& other)const
{
if(st==other.st)
return ed<other.ed;
else
return st<other.st;
}
}node[200];
int vis[50];
int main()
{
int t=1,p,s,e;
while(cin>>p,p)
{
for(int i=0;i<p;++i)
{
cin>>s>>e;
node[i].st=8+(s-8)*2;
node[i].ed=8+(e-8)*2;
}
sort(node,node+p);
memset(vis,0,sizeof(vis));
int res=0;
for(int i=0;i<p;++i)
{
for(int j=node[i].st;j<node[i].ed;++j)
if(vis[j]==0)
{
vis[j]=1;
res++;
break;
}
}
printf("On day %d Emma can attend as many as %d parties.\n",t++,res);
}
return 0;
}
/*
选择策略有问题,比如输入:
5
8 11
8 11
8 11
8 11
9 10
答案应该是5而不是4
*/