Dashboard - Codeforces Round 891 (Div. 3) - Codeforces
A. Array Coloring
思路:记录奇数的数量,奇数数量为偶数即可
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
int t, q[N];
int main()
{
cin >> t;
while(t --)
{
int n;
cin >> n;
int a = 0, b = 0;
for(int i = 0; i < n; i ++)
{
cin >> q[i];
if(q[i] % 2)
a ++;
else
b ++;
}
if(b == n)
cout << "YES" << endl;
else
{
if(a % 2 == 0)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
return 0;
}
B. Maximum Rounding
思路:找到最高位>=5的位置,其之后均变为零,从最低位开始,注意进位,当最高位>=5时,进1位
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
int t;
int main()
{
cin >> t;
while(t --)
{
string s;
cin >> s;
LL n = s.size();
LL cnt = 0, tt = -10000, flag = 0;
for(int i = n - 1; i >= 0; i --)
{
if(flag)
{
s[i] += 1;
flag = 0;
}
if(s[i] >= '5')//只要当前位>=5,便归零进一,并记录当前位置,改位置之后均为0
{
flag = 1;
tt = i;
s[i] = '0';
}
}
if(tt >= 0)//标记位,即最后>=5的位置,之后均归零
{
for(int i = tt; i < n; i ++)
s[i] = '0';
}
if(flag)//若最高位仍可进一
{
cout << 1 << s << endl;
}
else
cout << s << endl;
}
return 0;
}
C. Assembly via Minimums
题意:b数组是由a数组中所有元素两两取最小值组成,现在给出随机排列的b数组,求可能的a数组
思路:
- a数组中最小的数在b中一定出现了n-1次;
- 记录b数组最大的数,在a数组的最后输出;
- 以此类推,a数组中第i小的元素在b数组中出现了(n - i)次。
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 4e6 + 10;
int t;
LL q[N], a[N];
int main()
{
cin >> t;
while(t --)
{
LL n;
cin >> n;
LL m = (n * (n - 1)) / 2;
for(LL i = 0; i < m; i ++)
{
cin >> q[i];
}
sort(q, q + m);
if(q[0] == q[m - 1])
{
while(n --)
{
cout << q[0] << " ";
}
cout << endl;
}
else
{
LL ans = -0x3f3f3f;
for(int i = 0; i < m; i += n)//a数组中最小的数在b中一定出现了n-1次
{
cout << q[i] << " ";
ans = max(ans, q[i]);//记录b数组最大的数,在a数组的最后输出
n --;//以此类推,a数组中第i小的元素在b数组中出现了(n - i)次
}
cout << ans << endl;
}
}
return 0;
}
D. Strong Vertices
题意:给出ab两数组,满足au - av >= bu - bv,则点u可单方向到达v,求出可以到达除自身外其余所有点的个数。
思路:改变一下不等式:au - bu >= av - bv,找到最大的第i点ab的差,该点可到达所有点
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;
int a[N], b[N];
PII q[N];
int main()
{
cin >> t;
while(t --)
{
cin >> n;
for(int i = 1; i <= n; i ++)
cin >> a[i];
for(int i = 1; i <= n; i ++)
cin >> b[i];
for(int i = 1; i <= n; i ++)
{
q[i].first = a[i] - b[i];
q[i].second = i;
}
sort(q + 1, q + n + 1);
priority_queue<int, vector<int>, greater<int>> a;//用正序的优先队列维护强顶点
int tt = 1;
int cnt = q[n].first;
a.push(q[n].second);
for(int i = n - 1; i > 0; i --)
{
if(q[i].first == cnt)
{
a.push(q[i].second);
}
else
break;
}
cout << a.size() << endl;
while(!a.empty())
{
cout << a.top() << " ";
a.pop();
}
cout << endl;
}
return 0;
}
E. Power of Points
题意:给出n个坐标点,当以第i个点为标记点连接其他坐标点时,求从1到max(ai)的每个点被经过的次数和;
思路:用pair数组进行排序,用前缀和以及后缀和维护即可
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 2e5 + 10;
PII q[N];
int t, n, a[N], b[N], c[N];
int main()
{
cin >> t;
while(t --)
{
cin >> n;
for(int i = 1; i <= n; i ++)
{
int x;
cin >> x;
q[i].first = x, q[i].second = i;
}
sort(q + 1, q + n + 1);
b[n + 1] = 0;
a[0] = 0;
for(int i = 1; i <= n; i ++)
a[i] = a[i - 1] + q[i].first;
for(int i = n; i >= 1; i --)
b[i] = b[i + 1] + q[i].first;
for(int i = 1; i <= n; i ++)
{
c[q[i].second] = q[i].first * (i - 1 - n + i) - a[i - 1] + b[i + 1] + n;
}
for(int i = 1; i <= n; i ++)
cout << c[i] << " ";
cout << endl;
}
return 0;
}