Liars(2018-2019 ACM-ICPC Pacific Northwest Regional Contest (Div. 1))差分法


Problem L — limit 1 second

Liars

There are n people in a circle, numbered from 1 to n, each of whom always tells the truth or always lies. Each person i makes a claim of the form: “the number of truth-tellers in this circle is between ai and bi , inclusive.” Compute the maximum number of people who could be telling the truth. Input The first line contains a single integer n (1 ≤ n ≤ 103 ). Each of the next n lines contains two space-separated integers ai and bi (0 ≤ ai ≤ bi ≤ n). Output Print, on a single line, the maximum number of people who could be telling the truth. If the given set of statements is inconsistent, print -1 instead.

Sample Input and Output 3 1 1 2 3 2 2 2 8 0 1 1 7 4 8 3 7 1 2 4 5 3 7 1 8 -1

用差分数组对所有说真话人数的可能性进行计数,之后暴力求最大值

//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cmath>
#include<cstring>
#include <algorithm>
#include <queue>
#include<map>
#include<set>
#include<vector>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = 1e9;
const int mx = 1e7; //check the limits, dummy
typedef pair<int, int> pa;
const double PI = acos(-1);
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
#define swa(a,b) a^=b^=a^=b
#define re(i,a,b) for(int i=(a),_=(b);i<_;i++)
#define rb(i,a,b) for(int i=(a),_=(b);i>=_;i--)
#define clr(a) memset(a, inf, sizeof(a))
#define lowbit(x) ((x)&(x-1))
#define mkp make_pai
//void sc(int& x) { scanf("%d", &x); }void sc(int64_t& x) { scanf("%lld", &x); }void sc(double& x) { scanf("%lf", &x); }void sc(char& x) { scanf(" %c", &x); }void sc(char* x) { scanf("%s", x); }
int n, m,t;

int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n;
    vector<int>dif(n + 1, 0);
    for (int i = 0, l, r; i < n; i++) {
        cin >> l >> r;
        dif[l]++;
        if (r != n)dif[r + 1]--;
    }
    vector<int>cnt(n + 1, 0);
    cnt[0] = dif[0];
    int ans = -1;
    re(i, 1, n + 1) {
        cnt[i] = cnt[i - 1] + dif[i];
        if (cnt[i] == i)ans = i;
    }
    cout << ans << endl;
    return 0;
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM