小心失心疯

· · 题解

你这出的什么狗屁题目?背景故事又臭又长,扯了一堆毫无卵用的考古学废话,最后就为了包装一个弱智完形填空?THUPC 要是真用这种题,怕不是要被参赛者喷成筛子!

首先,背景里塞满恶俗烂梗(“114514”“哈姆星”),自以为幽默,实则尴尬得脚趾抠地。所谓的“学术分析”通篇胡扯,除了能忽悠出题人自己,还有哪个正常考生会关心你这些废话?

其次,题目本身毫无算法含量,输入输出简单到令人发指——这也好意思叫程序设计竞赛题?随便找个小学语文卷子挖个空都比你这有技术含量。输出就一个字母,选手写个cout<<"D"就能AC,你是在侮辱所有人的智商吗?

再看选项设计,强行玩错别字梗(“遭成”代“造成”),低劣且无聊。背景里故弄玄虚编了一堆词义推测,结果选项里连个像样的干扰项都没有,但凡识字的人都能蒙对,考个屁的古地球语?

最后,整道题弥漫着一股自嗨气息:出题人沉浸在自我编造的烂俗剧情里,却根本没考虑竞赛题应有的严谨性和考察价值。这种题目放在初赛,除了浪费纸面和选手时间,还有任何存在的意义吗?

总结:赶紧把这破题扔进垃圾桶,别再出来丢人现眼了!

#include <bits/stdc++.h>
#include <bits/extc++.h>
#define gcd std::__gcd
#define popcount __builtin_popcountll
#define seed std::chrono::steady_clock::now().time_since_epoch().count
using namespace std;
struct __h {
    static uint64_t s(uint64_t x)
    {return x+=0x9e3779b97f4a7c15,x=(x^(x>>30))*0xbf58476d1ce4e5b9,x=(x^(x>>27))*0x94d049bb133111eb,x^(x>>31);}
    size_t operator()(uint64_t x) const {static const uint64_t f=seed();return s(x+f);}
};
template<class K,class V>using umap=__gnu_pbds::gp_hash_table<K,V,__h>;
template<class T,class C=less<T>>using heap=__gnu_pbds::priority_queue<T,C>;
template<class K>using uset=__gnu_pbds::gp_hash_table<K,__gnu_pbds::null_type,__h>;
template<class T,class C=less<T>>using bst=__gnu_pbds::tree<T,__gnu_pbds::null_type,C,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>;
using u32=unsigned int;
using u64=unsigned long long;
#ifdef __SIZEOF_INT128__
using u128=unsigned __int128;
using int128=__int128;
#else
using u128=unsigned long long;
using int128=long long;
#endif
template<class T>constexpr T lowbit(T x){return x&-x;}
template<class T>constexpr T lg(T x){return x?__lg(x):0;}
template<class T>constexpr T ctz(T x){return x?__builtin_ctzll(x):0;}
template<class T1,class T2>ostream&operator<<(ostream&out,const pair<T1,T2>&p) 
{return out<<"("<<p.first<<", "<<p.second<<")";}
template<class T1,class T2>istream&operator>>(istream&in,pair<T1,T2>&p)
{return in>>p.first>>p.second;}
template<class T>ostream&operator<<(ostream&out,const vector<T>&v) 
{out<<"[";for(int i=0,n=v.size();i<n;++i<n?(out<<", "):out)out<<v[i];return out<<"]";}
mt19937 rand32(seed());
mt19937_64 rand64(seed());
#undef assert
#ifdef SPN_LOCAL
#define assert(x) (x)?void():(cerr<<"Assertion Failed On Line "<<__LINE__<<endl,exit(0))
void _o(auto&&...x){int f=1;((cerr<<(f?"":", ")<<x,f=0),...);}
#define mdebug(...) (cerr<<"On Line "<<__LINE__<<": ["<<#__VA_ARGS__<<"] = {",_o(__VA_ARGS__),cerr<<"}\n")
#define debug(x) (cerr<<"On Line "<<__LINE__<<": "<<#x<<" = "<<(x)<<endl)
#define FILE(x) void()
#define FLOCAL(x) freopen(x".in","r",stdin),freopen(x".out","w",stdout)
#else
#define assert(x) void()
#define mdebug(...) void()
#define debug(x) void()
#define FILE(x) freopen(x".in","r",stdin),freopen(x".out","w",stdout)
#define FLOCAL(x) void()
#endif //SPN_LOCAL

/*
  有解的充要条件为 a[i] | gcd(x, y)
  对于每个位置的覆盖次数为 p[i], q[i]
  总有 p[i] * x + q[i] * y = a[i]
  exGCD 易得通解形式
  根据链上铲雪,总代价 1/2 * sum |p[i]|+|q[i]|
  不妨设 x>y,f(i)=|x0+ib|+|y0-ia| 单谷
  谷底为 y0/a
  先算出谷底,调整法满足 sum p[i] = 0

  这咋办,对函数分三段考虑
  1. 左端每次跨步,代价为 a-b
  2. 跨过谷点,这是不平凡的
  3. 右端每次跨过,代价为 a+b
  存到一个vector里

  将所有代价排序即可
 */

//#define int long long
const int N = 1e5 + 5;

int n, a, b, g, h[N], d[N];
void exgcd(int128 a, int128 b, int128& x, int128& y) 
{b ? (exgcd(b, a % b, y, x), y -= a / b * x) : (x = 1, y = 0);}
tuple<int128, int128, int128> info[N];
int128 _abs(int128 x) {return x >= 0 ? x : -x;}  // 不手写你会失心疯的

void _main() {
    cin >> n >> a >> b, g = gcd(a, b);
    for (int i = 1; i <= n; i++) {
        cin >> h[i];
        if (h[i] % g) return cout << -1, void(); 
        h[i] /= g;
    }
    for (int i = 1; i <= n + 1; i++) d[i] = h[i] - h[i - 1];
    n++, a /= g, b /= g;
    if (a < b) swap(a, b);
    int128 x0, y0; exgcd(a, b, x0, y0);

    int128 cost = 0, cur = 0;
    auto work1 = [&]() -> void {
        for (int i = 1; i <= n; i++) {
            // x * a + y * b = -d[i]
            int128 v = -d[i], x = (x0 * v % b + b) % b, y = (v - a * x) / b;
            auto F = [&](int i) -> int128 {return _abs(x + i * b) + _abs(y - i * a);};
            int128 p = y / a;
            if (F(y / a - 1) < F(p)) p = y / a - 1;
            if (F(y / a + 1) < F(p)) p = y / a + 1;
            cost += F(p), cur += x + p * b;
            info[i] = {x, y, p};
        }
    };
    work1();
    int offset = -(cur / b), dir = offset < 0 ? -1 : 1;
    if (offset == 0) return cout << static_cast<u64>(cost / 2), void();

    vector<pair<int128, int128>> vec;
    auto work2 = [&]() -> void {
        for (int i = 1; i <= n; i++) {
            auto [x, y, p] = info[i];
            auto F = [&](int i) -> int128 {return _abs(x + i * b) + _abs(y - i * a);};
            auto D = [&](int i) -> int128 {return F(i + dir) - F(i);};
            int p1 = floor(-1.0 * x / b), p2 = floor(1.0 * y / a);
            if (dir == 1) {
                int cur = p;
                vector<int> tmp = {p1, p2};
                sort(tmp.begin(), tmp.end());
                for (int pos : tmp) {
                    if (pos < cur) continue;
                    if (pos > cur) vec.emplace_back(D(cur), pos - cur);  // ->
                    vec.emplace_back(D(p), 1), cur = pos + 1;   // 跨过 p -> p+1
                }
                vec.emplace_back(D(cur), _abs(offset));  // 整体
            } else {
                int cur = p;
                vector<int> tmp = {p1 + 1, p2 + 1};
                sort(tmp.begin(), tmp.end(), greater<int>());
                for (int pos : tmp) {
                    if (pos > cur) continue;
                    if (pos < cur) vec.emplace_back(D(cur), cur - pos);  // <-
                    vec.emplace_back(D(p), 1), cur = pos - 1;   // 跨过 p-1 <- p
                }
                vec.emplace_back(D(cur), _abs(offset));  // 整体
            }
        }
    };
    work2();

    sort(vec.begin(), vec.end());
    int128 res = cost, need = _abs(offset);
    for (auto [val, cnt] : vec) {
        int128 w = min(cnt, need);
        res += w * val, need -= w;
        if (need <= 0) break;
    } cout << static_cast<u64>(res / 2);
} signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr), cerr.tie(nullptr);
    cout << 'D'; return 0;

    int t = 1; for (/*cin >> t*/; t--; ) _main();
    return 0;
}

// g++ snow.cpp -o snow.exe -O2 -static -std=c++14 -DSPN_LOCAL -Wall -Wextra