Dashboard - Codeforces Round 892 (Div. 2) - Codeforces
A. United We Stand
题意:给出一个数组a,将数组a的元素分配到数组b和c中,bc不能为空,且c数组中的元素不能为b数组的除数
思路:把最小的数有多少都扔给b,剩下的给c,若没剩下的,“-1”。
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
int t, n;
int a[N], b[N], c[N], st[N];
int main()
{
cin >> t;
while(t --)
{
cin >> n;
for(int i = 1; i <= n; i ++)
{
cin >> a[i];
}
sort(a + 1, a + n + 1);
int u = 0, v = 0, flag = 0;
for(int i = 1; i <= n; i ++)
{
if(a[i] == a[1])
{
b[u ++] = a[i];
}
else
{
flag = 1;
c[v ++] = a[i];
}
}
if(!flag)
cout << "-1" << endl;
else
{
cout << u << " " << v << endl;
for(int i = 0; i < u; i ++)
cout << b[i] << " ";
cout << endl;
for(int i = 0; i < v; i ++)
cout << c[i] << " ";
cout << endl;
}
}
return 0;
}
B. Olya and Game with Arrays
题意:给出n组数,可以将一组中的一个数移动到另一组,一组只能操作一次,目的使每组的最小值的和最大
思路:
- 需要的是每组数的最小值,可以把当前每组数的最小值全部扔给一组数,即可做到最小值的和最大化,问题是扔给谁,显然扔给次小值最小的组,它的贡献最小
- 问题转变,找到每组数的次小值,以及所有数的最小值和该最小值对应组的最小次小值
- 所有次小值的和 - 最小次小值 + 所有数最小值 = ans
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 2e5 + 10;
int t, n, m;
int main()
{
cin >> t;
while(t --)
{
int n;
cin >> n;
vector<PII> cnt(n + 1);
LL ans = 0, mi = 0x3f3f3f3f3f;
for (int i = 1; i <= n; i ++)
{
int m;
cin >> m;
vector<LL> a(m + 1);
for (int j = 1; j <= m; j ++)
cin >> a[j];
sort(a.begin() + 1, a.end());
cnt[i].first = a[2];
cnt[i].second = a[1];
mi = min(mi, a[1]);
ans += a[2];
}
if (n == 1)
{
cout << mi << endl;
}
else
{
sort(cnt.begin() + 1, cnt.end());
ans = ans - cnt[1].first + mi;
cout << ans << endl;
}
}
return 0;
}
C. Another Permutation Problem
题意:将1到n排列组合,使当前位置元素与系数的积的和 - 最大积 ——>最大。
思路:逐级翻转数组,更新最大值。
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL t, n;
vector<int> a;
vector<int> q;
int main()
{
cin >> t;
while(t --)
{
a.clear();
cin >> n;
for(int i = 1;i <= n;i ++)
a.push_back(i);
LL ans = -1e9, sum = 0;
LL tt = -1e9;
for(int i = 0;i < n;i ++)
{
q = a;
reverse(q.begin() + i, q.end());
sum = 0, tt = -1e9;
for(LL j = 0;j < n;j ++)
{
LL u = q[j] * (j + 1);
sum += u;
tt = max(tt, u);
}
sum -= tt;
ans = max(sum, ans);
}
cout << ans << endl;
}
}