• CH0805 防线(算竞进阶习题)


    二分

    一道藏的很深的二分题。。。
    题目保证只有一个点有奇数个防具,这个是突破口。
    因为 奇数+偶数=偶数,我们假设某个点x,如果有奇数点的防具在x的左边,那么x的左边的防具总数一定是奇数,右边就是偶数
    所以我们可以用这个来二分。
    至于统计防具的公式,那就是小学学过的等差数列项数=(末项-首项)/公差+1。。

    #include <bits/stdc++.h>
    #define INF 0x3f3f3f3f
    #define full(a, b) memset(a, b, sizeof a)
    using namespace std;
    typedef long long ll;
    inline int lowbit(int x){ return x & (-x); }
    inline int read(){
        int X = 0, w = 0; char ch = 0;
        while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
        while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
        return w ? -X : X;
    }
    inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
    inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
    template<typename T>
    inline T max(T x, T y, T z){ return max(max(x, y), z); }
    template<typename T>
    inline T min(T x, T y, T z){ return min(min(x, y), z); }
    template<typename A, typename B, typename C>
    inline A fpow(A x, B p, C lyd){
        A ans = 1;
        for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
        return ans;
    }
    const int N = 1e8+5;
    int s[N], e[N], d[N];
    int main(){
    
        int _ = read();
        for(; _; _ --){
            int n = read();
            for(int i = 0; i < n; i ++)
                s[i] = read(), e[i] = read(), d[i] = read();
            int l = 1, r = INF;
            while(l < r){
                int mid = (r + l) >> 1, sum = 0;
                for(int i = 0; i < n; i ++){
                    if(s[i] <= mid){
                        if(e[i] > mid) sum += (mid - s[i]) / d[i] + 1;
                        else sum += (e[i] - s[i]) / d[i] + 1;
                    }
                }
                if(sum & 1) r = mid; else l = mid + 1;
            }
            int tot = 0;
            for(int i = 0; i < n; i ++){
                if(s[i] <= l && e[i] >= l && (l - s[i]) % d[i] == 0) tot ++;
            }
            if(tot == 0) printf("There's no weakness.
    ");
            else printf("%d %d
    ", l, tot);
        }
        return 0;
    }
    
  • 相关阅读:
    Learn Goroutine
    Redis eviction policies
    Hungarian Algorithm
    Prime and Factors
    HDU 2642 Stars
    236. Lowest Common Ancestor of a Binary Tree
    Leetcode 96. Unique Binary Search Trees
    Search in Rotated Sorted Array
    ID Generator
    概率问题
  • 原文地址:https://www.cnblogs.com/onionQAQ/p/10579684.html
Copyright © 2020-2023  润新知