题解 P8475 「GLR-R3」雨水
题解 P8475 「GLR-R3」雨水
简要题意
感觉原题描述比较复杂QAQ
本题是给一个长度为
基本思路:
先举个例子:
那么,我们该怎么移动序列呢?首先,要使字典序尽可能小,我们就要把最小的放在第一位,可以证明,如果使用其他的方法,最小的那个数没有排在第一位,那么处理后的序列一定不是字典序最小的子序列。因为是两两交换,假设
当查找时
再举个例子:
- 注意:本题要求答案对
2^{64} 取模,unsigned long long的自然溢出就等于对2^{64} 的取模。\text{std:} #include<bits/stdc++.h> typedef unsigned long long ull; const int MAXN = 1e7; // Set a right value according to your solution. int n, kind; int a[MAXN + 1]; ull sum; namespace Generator { unsigned long long k1, k2; int thres; inline unsigned long long xorShift128Plus() { unsigned long long k3 = k1, k4 = k2; k1 = k4, k3 ^= (k3 << 23), k2 = k3 ^ k4 ^ (k3 >> 17) ^ (k4 >> 26); return k2 + k4; } inline void generate() { for (int i = 1; i <= n; ++i) { a[i] = xorShift128Plus() % thres; } } } int main() { scanf("%d", &n); scanf("%llu %llu %d", &Generator::k1, &Generator::k2, &Generator::thres); Generator::generate(); for (int i = 1; i <= n; ++i) { int minn = 0x7fffffff, sce; for (int j = n; j > i; --j) if (a[j] < minn) minn = a[j], sce = j; if (minn < 0x7fffffff && a[i] > a[sce]) std::swap(a[i], a[sce]), i = sce; } for (int i = 1; i <= n; ++i) { sum = sum + (ull)i * a[i]; } printf("%llu", sum); return 0; }
时间复杂度分析:本代码看似两重循环,但每次 i 从 j + 1 继续所以每个元素只被遍历
有问题欢迎评论区留言哦!