hdu 4293 Groups dp

Groups

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1282    Accepted Submission(s): 493


Problem 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.
 

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).
 

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
<span style="font-family: 'Courier New', Courier, monospace; white-space: pre-wrap;">2 2</span>
32 00 22 2

法一:

用ri[i][j] 表示前i个人中,最后j个人为一组,被分在这个组的人数的个数 ri[i][j] <= j

用res[i]表示前i个人中说真话最多人数

res[i] = max(res[i], ri[i][j]+res[i-j])表示 前i个人中,最后j个人一组人数+前i-j个人说真话 中取最大值


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 501
int ri[maxn][maxn];
int res[maxn];
int main(){
    int n,u,v,t;
    while(scanf("%d",&n)!=EOF){
        memset(ri,0,sizeof(ri));
        memset(res,0,sizeof(res));
        for(int i = 0;i < n; i++){
            scanf("%d%d",&u,&v);
            t = n-u-v;
            if(t<=0) continue;
            ri[u+t][t]++;
            ri[u+t][t] = min(ri[u+t][t],t);
        }
        int ans = 0;
        for(int i = 1;i <= n; i++){
            for(int j = 1;j <= i; j++){
                u = res[i-j]+ri[i][j];
                if(u > res[i]) res[i] = u;
            }
            ans = max(ans,res[i]);
        }
        printf("%d\n",ans);
    }
    return 0;
}

法二:因为ri中很多为0的元素是不需要计算的,其实有用的数据只有n个,所以只需要记录有效数据即可,

然后合并同类元素,转移的时候要 注意res[i] = max(res[i],res[i-1])即可,负责度nlogn但是数据太小了,跑不快~

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
#define maxn 501
struct Node{
    int i,j,n;
};
int comp(Node a,Node b){
    if(a.i == b.i) return a.j < b.j;
    return a.i < b.i;
}
bool operator == (Node a,Node b){
    return a.i == b.i && a.j == b.j;
}
Node queue[1007];
int res[maxn];
int main(){
    int n,u,v,t;
    Node a;
    while(scanf("%d",&n)!=EOF){
        memset(res,0,sizeof(res));
        int cnt = 0,cnt1=0;
        for(int i = 0;i < n; i++){
            scanf("%d%d",&u,&v);
            t = n-u-v;
            if(t<=0) continue;
            a.i = t+u, a.j = t, a.n = 1;
            queue[cnt++] = a;
        }
        sort(queue,queue+cnt,comp);
        for(int i = 1;i < cnt; i++){
            if(queue[cnt1] == queue[i]) queue[cnt1].n+=queue[i].n;
            else queue[++cnt1] = queue[i];
        }
        int ans = 0,t=0;
        for(int i = 0;i <= cnt1; i++){
            a = queue[i];
            while(t!=a.i)res[t+1] = max(res[t+1],res[t++]);
            res[a.i] = max(res[a.i],res[a.i-a.j]+min(a.j,a.n));
            ans = max(ans,res[a.i]);
        }
        printf("%d\n",ans);
    }
    return 0;
}






你可能感兴趣的:(dp,HDU)