zoukankan      html  css  js  c++  java
  • Codeforces Round #725(Div.3)

    A. Stone Game

    #include <bits/stdc++.h>
    using namespace std;
     
    const int maxn = 1e5 + 10;
    int T, N;
    int a[maxn];
     
    int main() {
        scanf("%d", &T);
        while(T --) {
            scanf("%d", &N);
            int l = 0, r = 0;
            for(int i = 0; i < N; i ++) {
                scanf("%d", &a[i]);
     
                if(a[i] == 1) l = i;
                else if(a[i] == N) r = i;
            }
     
            if(l > r) swap(l, r);
     
            int ans = 0;
            if(l == r) {
                ans = 1;
            } else {
                ans = min(l + 1 + N - r, min(r + 1, N - l));
            }
            printf("%d
    ", ans);
        }
        return 0;
    }
    View Code

    题意大概为给一排数字 每次只能从一端拿走一个 求最少操作次数拿走最大和最小 直接算一下只取左 只取右 两边拿的 min

    B. Friends and Candies

    #include <bits/stdc++.h>
    using namespace std;
     
    const int maxn = 2e5 + 10;
    int T, N;
    int a[maxn];
     
    int main() {
        scanf("%d", &T);
        while(T --) {
            int sum = 0;
            scanf("%d", &N);
            for(int i = 1; i <= N; i ++) {
                scanf("%d", &a[i]);
                sum += a[i];
            }
     
            int ans = 0, ave = 0;
            if(sum % N) ans = -1;
            else {
                ave = sum / N;
                for(int i = 1; i <= N; i ++) {
                    if(a[i] > ave) ans ++;
                }
            }
     
            printf("%d
    ", ans);
     
        }
        return 0;
    }
    View Code

    N 个人每个人手上有若干糖果 操作最少人数去分他们的糖使最后每个人手上糖数量一样 求大于平均数的人数即为答案

    C. Number of Pairs

    #include <bits/stdc++.h>
    using namespace std;
     
    const int maxn = 2e5 + 10;
    int T, N, L, R;
    int a[maxn];
     
    int main() {
        scanf("%d", &T);
        while(T --) {
            scanf("%d%d%d", &N, &L, &R);
            for(int i = 0; i < N; i ++) scanf("%d", &a[i]);
            sort(a, a + N);
     
            long long  ans = 0;
           for(int i = 0; i < N; i ++) {
                ans += lower_bound(a, a + i, L - a[i]) - upper_bound(a, a + i, R-a[i]);
           }
     
           printf("%lld
    ", abs(ans));
     
        }
        return 0;
    }
    View Code

    N 个数字 L 和 R 的范围 L <= a[i] + a[j] <= R 称 i j 为一对 问有多少对 排序 + 二分

    D. Another Problem About Dividing Numbers

    #include <bits/stdc++.h>
    using namespace std;
     
    int T;
    int a, b, k;
     
    int step(int x) {
        int ans = 0;
        while(x % 2 == 0) {
            ans ++;
            x /= 2;
        }
        for(int i = 3; i * i <= x; i ++) {
            while(x % i == 0) {
                ans ++;
                x /= i;
            }
        }
        if(x != 1) ans ++;
     
        return ans;
     
    }
     
    int main() {
     
        scanf("%d", &T);
        while(T --) {
            scanf("%d%d%d", &a, &b, &k);
     
            if(k == 1) {
                if(a == b || (a % b && b % a)) printf("NO
    ");
                else printf("YES
    ");
            } else if(k == 0) {
                if(a == b) printf("YES
    ");
                else printf("NO
    ");
            } else {
                if(k > step(a) + step(b)) printf("NO
    ");
                else printf("YES
    ");
            }
     
        }
     
        return 0;
    }
    View Code

    给两个数字 a b 和操作次数 k 是否可以通过刚好 k 次对 a 或者 b 的整除操作使得 a = b 即看 a b 两个数字的分解成多少素数的因子的积 把 a 和 b 分解成的因子个数相加和 k 比较大小

    F. Interesting Function

    #include <bits/stdc++.h>
    using namespace std;
     
    int T;
     
    long long getStep(long long x) {
        long long cnt = 0;
        while(x) {
            cnt += x;
            x /= 10;
        }
     
        return cnt;
     
    }
     
    int main() {
        scanf("%d", &T);
        while(T --) {
            long long L, R;
            scanf("%lld%lld", &L, &R);
            printf("%lld
    ", getStep(R) - getStep(L));
        }
        return 0;
    }
    View Code

    从 1 到 x 数字在变化的过程中按位 把每一位做的贡献加起来 个位做的贡献是 x 十位是 x / 10 ... 以此类推 所以要 L  到 R 的数字位数变化即 1 ~ R 减去 1 ~ L

    G. Gift Set

    #include <bits/stdc++.h>
    using namespace std;
    
    int T, x, y, a, b;
    
    bool check(int num) {
        int xx = x - num * a, yy = y - num * a;
        int temp = b - a;
        if(temp == 0) return true;
        return ((xx / temp) + (yy / temp)) >= num;
    }
    
    int main() {
        scanf("%d", &T);
        while(T --) {
            scanf("%d%d%d%d", &x, &y, &a, &b);
            if(a > b) swap(a, b);
            int maxx = min(x / a, y / a);
            int l = 1, r = maxx, pos = -1;
            while(l <= r) {
                int mid = (l + r) / 2;
                if(check(mid)) l = mid + 1, pos = mid;
                else r = mid - 1;
            }
            if(pos == -1) printf("0
    ");
            else printf("%d
    ", pos);
        }
        return 0;
    }
    View Code

    二分

     

  • 相关阅读:
    C语言编程练习4:镂空三角形
    C语言编程练习3:小明的18岁生日
    C语言编程练习2:放大的X
    C语言编程练习1:打印数字图形
    Hexo+Github搭建个人博客
    报表
    唐人街探案
    窗体
    ACCESS SQL
    交叉表
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/14917806.html
Copyright © 2011-2022 走看看