bzoj2298: [HAOI2011]problem a dp

来源:互联网 发布:浙江卫视网络直播 编辑:程序博客网 时间:2024/04/30 07:52

Description
一次考试共有n个人参加,第i个人说:“有ai个人分数比我高,bi个人分数比我低。”问最少有几个人没有说真话(可能有相同的分数)
Input
第一行一个整数n,接下来n行每行两个整数,第i+1行的两个整数分别代表ai、bi
Output
一个整数,表示最少有几个人说谎
Sample Input
3
2 0
0 2
2 2
Sample Output
1
HINT
100%的数据满足: 1≤n≤100000   0≤ai、bi≤n

dp计算出最多有多少人说真话就可以了。
我们可以根据ai bi确定每个人所处在的区间,用map记下每个区间的人数
按每个的右边人数确定一类人。
很明显 这类人都说有r人比自己高 而l值不相同,所以他们最多只有一段相同的人说的是真话。
dp[i]代表前i种名次的人 最多有多少人
转移时要注意 说真话的可能是所有说法相同的人,也有可能是这个人所在的区间的最大值
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <map>#include <vector>using namespace std;#define mp(a,b) make_pair(a,b)int getint(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}int n;int f[100005];vector<int> q[100005];map<pair<int,int>,int> s;int main(){    n=getint();for(int i=1;i<=n;i++){int a=getint(),b=getint();int l=a+1,r=n-b;if(l>r)continue;s[mp(l,r)]++;if(s[make_pair(l,r)]==1) q[r].push_back(l);}for(int i=1;i<=n;i++){f[i]=f[i-1];for(int j=0;j<q[i].size();j++)        {            f[i]=max(f[i],f[q[i][j]-1]+min(s[mp(q[i][j],i)],i-q[i][j]+1));        }}printf("%d\n",n-f[n]);    return 0;}


0 0
原创粉丝点击