题目:
Groups
After the regional contest, all the ACMers are walking alone a very long avenue to the dining hall in groups. Groups can vary in size for kinds of reasons, which means, several players could walk together, forming a group.
As the leader of the volunteers, you want to know where each player is. So you call every player on the road, and get the reply like “Well, there are A i players in front of our group, as well as B i players are following us.” from the i th player.
You may assume that only N players walk in their way, and you get N information, one from each player.
When you collected all the information, you found that you’re provided with wrong information. You would like to figure out, in the best situation, the number of people who provide correct information. By saying “the best situation” we mean as many people as possible are providing correct information.
As the leader of the volunteers, you want to know where each player is. So you call every player on the road, and get the reply like “Well, there are A i players in front of our group, as well as B i players are following us.” from the i th player.
You may assume that only N players walk in their way, and you get N information, one from each player.
When you collected all the information, you found that you’re provided with wrong information. You would like to figure out, in the best situation, the number of people who provide correct information. By saying “the best situation” we mean as many people as possible are providing correct information.
Input
There’re several test cases.
In each test case, the first line contains a single integer N (1 <= N <= 500) denoting the number of players along the avenue. The following N lines specify the players. Each of them contains two integers A i and B i (0 <= A i,B i < N) separated by single spaces.
Please process until EOF (End Of File).
In each test case, the first line contains a single integer N (1 <= N <= 500) denoting the number of players along the avenue. The following N lines specify the players. Each of them contains two integers A i and B i (0 <= A i,B i < N) separated by single spaces.
Please process until EOF (End Of File).
Output
For each test case your program should output a single integer M, the maximum number of players providing correct information.
Sample Input
3 2 0 0 2 2 2 3 2 0 0 2 2 2
Sample Output
2 2HintThe third player must be making a mistake, since only 3 plays exist.
Source
思路: 关键还是建模,建图呀~~~ 将每个数对应到一个区间, 将区间映射成一个点,互不相交的区间可以连边。就拿样例来说吧, 2 0属于区间 [3,3],记录为点1,0 2属于区间[1,1] ,记录为点2,2 2不能映射到一个合法区间 所以忽略。添加一个起点0,那现在用点0连接所有构造出的点,边的权值就是 :构造出的点所对应的区间里出现了几个人,同样添加一个终点,所有构造的点向终点连一条边。权值为0。 所有的不想交的区间可以连边。 那现在就是求起点到终点的最长路了,一遍spfa直接水过,具体见代码吧~~~
#include<stdio.h>
#include<string.h>
#include <string>
#include <cmath>
#include <iostream>
#include <map>
#include<vector>
#include<queue>
#include<algorithm>
#define fr(i,s,n) for(int i=s;i<n;i++)
#define pf printf
#define sf scanf
#define sfv1(a) scanf("%d",&a)
#define sfv2(n,m) scanf("%d%d",&n,&m)
#define sfv3(u,v,w) scanf("%d%d%d",&u,&v,&w)
#define sfstr(c) scanf("%s",c)
#define pfv1(a) printf("%d\n",a)
#define fi freopen("in.txt","r",stdin)
#define fo freopen("out.txt","w",stdout)
#define cl(a) memset(a,0,sizeof(a))
#define me(a,x) memset(a,x,sizeof(a))
#define inf 2147483647
using namespace std;
typedef long long ll;
int n;
int mp[510][510];
int tot;
struct P{
int x,y;
int val;
}p[250020];
const int N=250020;
struct E{
int u,v,nxt;
}edge[2000010];
int tote,head[N];
void init(){
tote=0;
me(head,-1);
}
inline void addedge(int u,int v){
edge[tote].u=u;edge[tote].v=v;edge[tote].nxt=head[u];head[u]=tote++;
};
int dis[250020];
int inq[250020];
queue<int> q;
int spfa(){
int v;
int cur;
memset(dis,0,sizeof(int)*(tot+1));
cl(inq);
while(!q.empty()) q.pop();
q.push(0);
while(!q.empty()){
cur = q.front();
q.pop();
inq[cur]=0;
for (int i = head[cur]; i != -1; i=edge[i].nxt) {
v=edge[i].v;
if (dis[v]<dis[cur]+p[v].val){
dis[v]=dis[cur]+p[v].val;
if (!inq[v]){
q.push(v);
inq[v]=1;
}
}
}
}
return dis[tot];
}
int main(){
int a,b;
int nn;
while(sfv1(n)!=EOF){
init();
tot=1;
cl(mp);
nn=n;
while(nn--){
sfv2(a,b);
if (a+b>=n) continue;
if (!mp[a][b]) {
p[tot].x=a+1;
p[tot].y=n-b;
p[tot].val=1;
mp[a][b]=tot++;
}else{
int aim=mp[a][b];
if (p[aim].val<=p[aim].y-p[aim].x) {
p[aim].val++;
}
}
}
fr(i,1,tot-1){
fr(j,i+1,tot){
if (p[i].y<p[j].x){
addedge(i,j);
}else if(p[j].y<p[i].x){
addedge(j,i);
}
}
}
fr(i,1,tot){
addedge(0,i);
addedge(i,tot);
}
p[tot].val=0;
int ans=spfa();
pfv1(ans);
}
return 0;
}