Negative Prefixes (CodeForces - 1418B )
题意
给定一组数,其中有些数被锁定,对没有被锁定的数进行任意排列,被锁定的数保持在数组中原来的位置,然后求出重排后数组的所有前缀和,当某一前缀s[i]>0时,规定k=i-1,问该k值最小可以为多少
思路
若想前缀和大于0的s[i]尽早出现,就需要前面的数尽可能的大,所以除去锁定的部分,只需要将未锁定的部分进行从大到小排序即可
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int a[N], b[N], s[N], st[N];
bool cmp(int x, int y) {
return x > y;
}
void solve() {
int n, idx = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) {
scanf("%d", &st[i]);
if (!st[i])
b[++idx] = a[i];
}
sort(b + 1, b + idx + 1, cmp);
int j = 0;
for (int i = 1; i <= n; i++) {
if (st[i])
printf("%d ", a[i]);
else
printf("%d ", b[++j]);
}
puts("");
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
solve();
}
return 0;
}
Bargain CodeForces - 1422C
题意
给出一个非常大的数(<=10^100000),每次选择删除该数字串中的一个子段,然后将剩余部分拼接成一个新的数字串,加上这个新数字串的值,现在将所有的子串都删除一遍,然后依次加上形成的新数字串的值,将最终答案对1e9+7取模
思路
参考dalao博客
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e5 + 5, MOD = 1e9 + 7;
LL f[N];
int main() {
string s;
cin >> s;
LL n = s.size();
reverse(s.begin(), s.end());
f[0] = 1, f[1] = 10;
for (int i = 2; i < N; i++)
f[i] = (f[i - 1] * 10) % MOD;
LL t1, t2, temp = 0, ans = 0;
for (int i = 0; i < n; i++) {
t1 = (n - i) * (n - i - 1) / 2 % MOD * f[i] * (s[i] - '0') % MOD;
t2 = (s[i] - '0') * temp % MOD;
temp = ((i + 1) * f[i] % MOD + temp) % MOD;
ans += (t1 + t2) % MOD;
ans %= MOD;
}
printf("%lld\n", ans);
return 0;
}