Codeforces Round 960 (Div. 2)解题报告(B.C)
B. Array Craft
题目:
解题思路:根据题目我们显然可以知道x>y,若要使x前缀和最大则x之后的值到x的前缀和必定小于0,故可以-1,1,-1,1交替出现,y同理,让y之前的数-1,1交替出现,
然后只需x,y在[y,x]区间内分别为前,后缀和最大就行了,只需要在剩下位置都为1就可以满足了
代码:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
void solve() {
int n, x, y;
cin >> n >> x >> y;
vector<int> a(n, 1);
x--; y--;
int t = -1;
for (int i = y-1; i >= 0; i--) {
a[i] = t;
t = -t;
}
t = -1;
for (int i = x+1; i < n; i++) {
a[i] = t;
t = -t;
}
for (int i = 0; i < n; i++) {
cout << a[i];
if (i < n-1) cout << ' ';
else cout << '\n';
}
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
C. Mad MAD Sum
题目:
解题思路:通过打表发现经过两次变换后之后的数组的值就已经基本确定,剩下的变换都是数组的值不断右移罢了
例如:
11522533(初始数组)
01112555
00111155
00011115
00001111
00000111
00000011
00000001
00000000
代码:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
void solve() {
int n;
cin >> n;
ll a[n], sum = 0;
map<ll,ll> m;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
if (a[i] > 0) {
sum += a[i];
}
}
int max = 0;
for (int i = 0; i < n; i++) {
if (m.end() != m.find(a[i])) m[a[i]]++;
else m.insert(pair<ll,ll>(a[i], 1));
if ((m[a[i]] >= 2) && a[i] > max) {
max = a[i];
}
a[i] = max;
}
for (int i = 0; i < n; i++) {
if (a[i] > 0) {
sum += a[i];
}
}
m.clear();
max = 0;
for (int i = 0; i < n; i++) {
if (m.end() != m.find(a[i])) m[a[i]]++;
else m.insert(pair<ll,ll>(a[i], 1));
if ((m[a[i]] >= 2) && a[i] > max) {
max = a[i];
}
a[i] = max;
}
for (int i = 0; i < n; i++) {
if (a[i] > 0) {
sum += a[i];
}
}
for (int i = 0; i < n; i++) {
if (a[i] > 0) {
sum += a[i] * (n-1-i);
}
}
cout << sum << '\n';
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
题解代码(cpp):
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 200005;
int n;
int a[N];
bool c[N];
void doit()
{
for (int i = 1; i <= n; ++i)
c[i] = false;
int maxu = 0;
for (int i = 1; i <= n; ++i)
{
if (c[a[i]])
maxu = max(maxu, a[i]);
c[a[i]] = true;
a[i] = maxu;
}
}
void solv()
{
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> a[i];
ll ans = 0;
for (int i = 1; i <= n; ++i)
ans += a[i];
doit();
for (int i = 1; i <= n; ++i)
ans += a[i];
doit();
for (int i = 1; i <= n; ++i)
ans += (n - i + 1) * 1LL * a[i];
cout << ans << "\n";
}
int main()
{
#ifdef SOMETHING
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif // SOMETHING
ios_base::sync_with_stdio(false), cin.tie(0);
int tt = 1;
cin >> tt;
while (tt--)
{
solv();
}
return 0;
}