codeforces 905 div.3
还是太菜了,干坐俩小时出了a,b,向学长学习
A
ACcode
#include<iostream>
#include<algorithm>
using namespace std;
void solve() {
int a; cin >> a;
int c = a % 10;
int d = a % 100 / 10;
int e = a % 1000 / 100;
int f = a / 1000;
if (c == 0)c = 10;
if (d == 0)d = 10;
if (e == 0)e = 10;
if (f == 0)f = 10;
int num = abs(f-1)+abs(f - e) + abs(e - d) + abs(d - c) + 4;
cout << num << '\n';
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t; cin >> t;
while (t--) {
solve();
}
return 0;
}
B
#include<iostream>
#include<string>
using namespace std;
const int M = 1e5 + 9;
int a[M];
void solve() {
int n, k; cin >> n >> k;//字母数量,可修改的数量
string str;
cin >> str;
for (int i = 0; i < M; i++)a[i] = 0;
for (int i = 0; i < n; i++) a[str[i] - 97]++;
int ret = 0;
for (int i = 0; i <26 ; i++)if (a[i] % 2 != 0)ret++;//奇数的数量
ret--;
if (ret <= k)cout << "YES" << '\n';
else cout << "NO" << '\n';
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t; cin >> t;
while (t--) {
solve();
}
return 0;
}
C
题意
给定一个有n个元素的数组和一个正整数k,你每次可以做如下操作,选定一个数,让其加一,你需要判断最少需要多少次操作使得数组内元素的乘积能被k整除
ACcode
#include<iostream>
#include<vector>
#include<climits>
using namespace std;
void solve() {
int n, k; cin >> n >> k;
vector<int>a(n + 5);
for (int i = 0; i < n + 5; i++)a[i] = 0;
for (int i = 1; i <= n; i++)cin >> a[i];
int mi = INT_MAX;
for (int i = 1; i <= n; i++) {
if (a[i] % k == 0) { cout << 0 << '\n'; return; }
}
for (int i = 1; i <= n; i++) {
if (a[i] < k)mi = min(mi, k - a[i]);
else{
int num = a[i] % k;
mi = min(mi, k-num);
}
}
if (k == 4) {
int ret = 0;
for (int i = 1; i <= n; i++) {
if (a[i] % 2)ret++;//不能整除2的数量
}
if (n - ret > 1) { cout << 0 << '\n'; return; }
else if (n - ret == 1 && ret >= 1)mi = min(mi, 1);
else if(ret>1) mi = min(mi, 2);
//cout << mi << '\n';
}
cout << mi << '\n';
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t; cin >> t;
while (t--) {
solve();
}
return 0;
}
D
题意
有q次修改,你需要判断每次修改之后是否有重叠的两条线段
思路
用multiset分别把左右边界存下来,每次比较如果最大的左界比最小的右界大,就有重叠
ACcode
#include<iostream>
#include<set>
using namespace std;
using ll = long long;
multiset<ll, less<ll> >R, L;//进入容器后自动从小到大排序
bool check() {//找最小的右界和最大的左界
if (L.size() < 1)return 0;
return *R.begin() < *(--L.end());
}
void solve()
{
char c; cin >> c;
int a, b; cin >> a >> b;
if (c == '+') {//增加界
R.insert(b); L.insert(a);
}
else {
R.erase(R.find(b)); L.erase(L.find(a));
}
if (check())cout << "YES" << '\n';
else cout << "NO" << '\n';
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t; cin >> t;
while (t--) {
solve();
}
return 0;
}
E
题意
给定一个n个正整数元素的数列,每次你可以选择一个元素让其乘2,输出最小操作数使得该数列变为非降序序列
ACcode
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
using ll = long long;
const int M = 1e5 + 9;
int a[M];
void solve()
{
int n; cin >> n;
vector<ll>a(n + 3);
for (int i = 1; i <= n; i++)cin >> a[i];
ll cnt = 0;
ll ans = 0;
for (int i = 2; i <= n; i++) {
ll R, L; ll ct = 0;
L = a[i - 1]; R = a[i];
while (L < R)ct--, L *= 2;
while (L > R)ct++, R *= 2;
cnt = max(0ll, cnt + ct);
ans += cnt;
}
cout << ans << '\n';
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t; cin >> t;
while (t--) {
solve();
}
return 0;
}