码迷,mamicode.com
首页 > 其他好文 > 详细

[HAOI2011]problem a

时间:2018-11-22 18:16:38      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:i+1   continue   inf   code   math   ace   int   stdin   NPU   

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

直接求最少不好求,我们考虑求出最多有多少个人说了真话,直接用n减去即可

\(f[i]\)表示在前i名最多有多少人说了真话,转移即为

\[f[i]=max\{f[j-1]+sum[j][i]\}\]

其中,\(sum[j][i]\)表示名次区间在\([j,i]\)中的人数

直接记二维显然不行,有效的区间其实很少,因此我们可以用pair去映射一个int

/*program from Wolfycz*/
#include<map>
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
    static char buf[1000000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
    int x=0,f=1; char ch=gc();
    for (;ch<'0'||ch>'9';ch=gc())   if (ch=='-')    f=-1;
    for (;ch>='0'&&ch<='9';ch=gc()) x=(x<<3)+(x<<1)+ch-'0';
    return x*f;
}
inline int read(){
    int x=0,f=1; char ch=getchar();
    for (;ch<'0'||ch>'9';ch=getchar())  if (ch=='-')    f=-1;
    for (;ch>='0'&&ch<='9';ch=getchar())    x=(x<<3)+(x<<1)+ch-'0';
    return x*f;
}
inline void print(int x){
    if (x<0)    putchar('-'),x=-x;
    if (x>9)    print(x/10);
    putchar(x%10+'0');
}
const int N=1e5;
map<pair<int,int>,int>mp;
vector<int>vec[N+10];
int f[N+10];
int main(){
    int n=read();
    for (int i=1;i<=n;i++){
        int l=read()+1,r=n-read();
        if (l>r)    continue;
        map<pair<int,int>,int>::iterator it=mp.find(make_pair(l,r));
        if (it==mp.end()){
            mp.insert(map<pair<int,int>,int>::value_type(make_pair(l,r),1));
            vec[r].push_back(l);
        }else   (it->second)++;
    }
    for (int i=1;i<=n;i++){
        f[i]=f[i-1];
        for (vector<int>::iterator it=vec[i].begin();it!=vec[i].end();it++)
            f[i]=max(f[i],f[(*it)-1]+min(i-(*it)+1,mp.find(make_pair((*it),i))->second));
    }
    printf("%d\n",n-f[n]);
    return 0;
}

[HAOI2011]problem a

标签:i+1   continue   inf   code   math   ace   int   stdin   NPU   

原文地址:https://www.cnblogs.com/Wolfycz/p/10002516.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!