Description
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 2
DP:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
#define maxn 508
int dp[maxn];
int map[maxn][maxn];
struct QJ
{
int l,r;
}qj[maxn];
bool cmp(QJ a,QJ b)
{
if(a.r < b.r) return 1;
if(a.r > b.r) return 0;
if(a.r == b.r)
{
return a.l < b.l;
}
}
inline int min(int a,int b)
{
return a>b?b:a;
}
inline int max(int a,int b)
{
return a>b?a:b;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(dp,0,sizeof(dp));
memset(map,0,sizeof(map));
int t = 1;
for(int i=1;i<=n;i++)
{
int fir,rear;
scanf("%d%d",&fir,&rear);
if(!map[fir+1][n-rear])
{
qj[t].l = fir + 1;
qj[t++].r = n - rear;
}
map[fir+1][n-rear] = min(map[fir+1][n-rear]+1,n-rear-fir);
}
sort(qj+1,qj+t,cmp);
for(int i=1;i<t;i++)
{
for(int j=0;j<qj[i].l;j++)
{
dp[qj[i].r] = max(dp[qj[i].r],dp[j]+map[qj[i].l][qj[i].r]);
}
}
int ans = 0 ;
for(int i=1;i<=n;i++)
{
ans = max(ans,dp[i]);
}
printf("%d\n",ans);
}
return 0;
}
最长路:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define maxn 1008 //点数,在此题为区间数。
#define edge 300008 //边数
int A[maxn],B[maxn];//用来存每个区间的左端点和每个区间的右端点
int map[maxn][maxn];
int dis[maxn];
inline int max(int a,int b)
{
return a>b?a:b;
}
inline int min(int a,int b)
{
return a>b?b:a;
}
struct Adj
{
int v,w,next;
Adj(){}
Adj(int vv,int ww,int nxt)
{
v = vv;
w = ww;
next = nxt;
}
}adj[edge];
int cnt;
int first[maxn];
void addEdge(int u,int v,int w)
{
adj[cnt] = Adj(v,w,first[u]);
first[u] = cnt++;
}//建立的必须是单向边
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(first,-1,sizeof(first));
memset(dis,0,sizeof(dis));
memset(map,0,sizeof(map));
cnt = 0;
int t = 1;
for(int i=1;i<=n;i++)
{
int fir,rear;
scanf("%d%d",&fir,&rear);
if(fir + rear + 1 > n) continue;
if(!map[fir+1][n-rear])
{
A[t] = fir + 1;
B[t++] = n - rear;
}
map[fir+1][n-rear]=min(map[fir+1][n-rear]+1,n-rear-fir);
}
for(int i=1;i<t;i++)
{
for(int j=1;j<t;j++)
{
if(i == j) continue;//如果是同个点,(点代表的是区间)
if(B[i] < A[j])
{
addEdge(i,j,map[A[j]][B[j]]);
}
}
}
for(int i=1;i<t;i++)
{
addEdge(0,i,map[A[i]][B[i]]);
}
queue <int> q;
q.push(0);
while(!q.empty())
{
int p = q.front();
q.pop();
for(int i=first[p];i!=-1;i=adj[i].next)
{
int v = adj[i].v;
if(dis[p] + adj[i].w > dis[v])
{
dis[v] = dis[p] + adj[i].w;
q.push(v);
}
}
}
int ans = 0;/
for(int i=1;i<t;i++)
{
ans = max(ans,dis[i]);
}
printf("%d\n",ans);
}
return 0;
}