不知道是这半个月做CF做多了,还是因为这次Div3题出的比较水,这次做的感觉比之前好多了,ABCD都是水题,45分钟做完了ABD题,C题纯数学题,可能是这几天做CF做的有点多感觉做C题时有点状态不对了,以为只要变量是long long表达式是int也不会溢出,结果还是溢出了,而且居然没发现,在最后也没把正确的程序交上去,不然至少可以进前1500了,这星期还是先不做CF竞赛了,多练一些基础题目把
- A. Prefixes
题意:给定一串只包含’a’或’b’的字符串,为了使字符串偶数位置的前缀a和b的数量相同,求需要修改的最少字符数
思路:枚举一下每偶数位判断一下当前两字符数量即可
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e3+10;
const int inf = 0x3f3f3f3f;
int n;
int main()
{
string s;
cin >> n >> s;
int cnt1 = 0, cnt2 = 0, ans = 0;;
for (int i = 0; i <= n; i++) {
if (i%2 == 0 && i != 0) {
if (cnt1 > cnt2) {
s[i-1] = 'b';
cnt1--;
cnt2++;
ans++;
} else if (cnt1 < cnt2){
s[i-1] = 'a';
cnt1++;
cnt2--;
ans++;
}
}
if (i == n)
break;
if (s[i] == 'a')
cnt1++;
else
cnt2++;
}
cout << ans << "\n";
cout << s << "\n";
return 0;
}
- B. Shooting
题意:有n个罐头,给出每个罐头的耐久度a[i],若之前射掉的罐头数为x,则射掉当前罐头需要发射的子弹数为a[i]*x+1,求出射掉所有罐头需要的最少子弹数
思路:按照耐久度从大到小排序,从最大的那个罐头开始射即可
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e3+10;
const int inf = 0x3f3f3f3f;
int n;
struct node {
int index, val;
node(int index = 0, int val = 0) : index(index), val(val) {
}
}a[maxn];
bool cmp(node x, node y)
{
return x.val > y.val;
}
int main()
{
cin >> n;
vector<int> res;
for (int i = 0; i < n; i++) {
int t;
cin >> t;
a[i] = node(i, t);
}
sort(a, a+n, cmp);
int ans = 0, cnt = 0;
for (int i = 0; i < n; i++) {
ans += i*a[i].val+1;
res.push_back(a[i].index);
}
cout << ans << "\n";
for (int i = 0; i < res.size(); i++)
printf("%d ", res[i]+1);
printf("\n");
return 0;
}
- C. White Sheet
题意:有两个黑色矩形和一个白色矩形,给出每个矩形的左下角和右上角坐标,判断白色矩形是否没被两黑色矩形覆盖
思路:用白色矩形面积减去其与两黑色矩形相交部分的面积,若有重复部分加上那部分,判断得出结果是否为0即可。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5+10;
const int inf = 0x3f3f3f3f;
ll x1, x2, y1, y2, x3, x4, y3, y4, x5, x6, y5, y6;
int main()
{
cin >> x1 >> y1 >> x2 >> y2;
cin >> x3 >> y3 >> x4 >> y4;
cin >> x5 >> y5 >> x6 >> y6;
ll s = (x2-x1)*(y2-y1);
ll s1 = (min(x2,x4)-max(x1,x3))*(min(y2,y4)-max(y1,y3));
ll s2 = (min(x2,x6)-max(x1,x5))*(min(y2,y6)-max(y1,y5));
ll s3 = (min(x2,min(x4, x6))-max(x1,max(x3,x5)))*(min(y2,min(y4,y6))-max(y1,max(y3,y5)));
s -= (s1 >= 0 ? s1 : 0)+(s2 >= 0 ? s2 : 0);
if (s <= 0 && s3 > 0)
s += s3;
if (s == 0)
cout << "NO\n";
else
cout << "YES\n";
return 0;
}
- D. Swords
题意:原先有n种类型的剑,每种类型剑数量相同都为x,有y个小偷进来,每个小偷取走某种类型的z把剑,最后第i个类型的剑剩下a[i]把。其中a[i]已知,x、y、z未知,求出最小的y,及其对应的z
思路:根据样例可以发现z为每种类型取走剑数的最大公因数,即可保证y最小,用gcd求出每种类型取走剑数的最大公因数即可
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5+10;
const int inf = 0x3f3f3f3f;
ll n, a[maxn], b[maxn];
int main()
{
cin >> n;
ll maxx = 0, ans = 0, k = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
maxx = max(a[i], maxx);
}
for (int i = 0; i < n; i++) {
b[i] = maxx-a[i];
k = __gcd(b[i], k);
}
for (int i = 0; i < n; i++) {
ans += b[i]/k;
}
printf("%lld %lld", ans, k);
return 0;
}