zoukankan      html  css  js  c++  java
  • Atcoder B

    http://agc010.contest.atcoder.jp/tasks/agc010_b

    预处理出每两个相邻的数的差值,那么首先知道是一共取了sum / ((1 + n) * n / 2)次,因为每一次固定要取这么多,所以这个就是操作次数。

    然后观察到,每一次操作,都是把dis[]数组的n - 1个减小1,有一个要加上n - 1、最终要变成0

    那么问题就是转化成,给定一个数,有两种操作,

    第一种是减去1,第二种是加上某一个数,问其在k步后,能否变成0

    那么设第一种操作用了x次,第二种操作就是k - x次,那么带入去,能得出一条方程。

    dis[i] - x + (k - x) * (n - 1) = 0  (错误了)

    不知为何这样错了,很假。

    然后题解是设第一种用了k - x次,第二种用了x次。

    那么就是dis[i] - (k - x) + x * (n - 1) = 0(能过)

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <assert.h>
    #define IOS ios::sync_with_stdio(false)
    using namespace std;
    #define inf (0x3f3f3f3f)
    typedef long long int LL;
    
    
    #include <iostream>
    #include <sstream>
    #include <vector>
    #include <set>
    #include <map>
    #include <queue>
    #include <string>
    #include <bitset>
    const int maxn = 2e5 + 20;
    int a[maxn];
    vector<int>da;
    void work() {
        int n;
        LL sum = 0;
        cin >> n;
        for (int i = 1; i <= n; ++i) {
            cin >> a[i];
            sum += a[i];
        }
        LL add = 1LL * n * (n + 1) / 2;
        if (sum % add != 0) {
            cout << "NO" << endl;
            return;
        }
        for (int i = 2; i <= n; ++i) {
            da.push_back(a[i] - a[i - 1]);
        }
        da.push_back(a[1] - a[n]);
        LL k = sum / add;
        for (int i = 0; i < da.size(); ++i) {
            LL t = k - da[i];
    //        LL t = da[i] + k * (n - 1);
            if (t % n != 0 || t < 0) {
                cout << "NO" << endl;
                return;
            }
        }
        cout << "YES" << endl;
    }
    
    int main() {
    #ifdef local
        freopen("data.txt", "r", stdin);
    //    freopen("data.txt", "w", stdout);
    #endif
        work();
        return 0;
    }
    View Code
  • 相关阅读:
    Codeforces round 493 Convert to Ones
    石子合并系列问题【区间dp,环形,四边不等式优化】
    UVa 10635
    选课【树形dp】
    JSOI2016病毒感染
    加分二叉树【树形dp】
    人为什么活着__稻盛和夫的哲学
    213. House Robber II
    安装 error: Microsoft Visual C++ 14.0 is required 解决方案
    ImportError:no mudle named 'cv2'
  • 原文地址:https://www.cnblogs.com/liuweimingcprogram/p/6371931.html
Copyright © 2011-2022 走看看