zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    iOS小技巧总结,绝对有你想要的
    Myeclipse for Mac快捷键
    iOS开发如何学习前端
    iOS应用支持IPV6,就那点事儿
    App Store审核被拒的23个理由
    43个优秀的Swift开源项目
    ExtJs组件之间的相互访问,访问机制
    hibernate or连接查询内容/criteria动态或连接查询/disjunction/其他查询条件
    hibernate如何解除关系———只删除多方保留一方
    java如何操作视图
  • 原文地址:https://www.cnblogs.com/xxxsans/p/12800681.html
Copyright © 2011-2022 走看看