一、第一题AcWing 4396. 取石子
2、比赛时写的代码:
#include<bits/stdc++.h>
using namespace std;
int n1,n2,k1,k2;
int main()
{
cin >> n1 >> n2 >> k1 >> k2;
while( n1-- && n2--){
if(n1 == 0){
cout << "Second";
break;
}
if(n2 == 0){
cout << "First";
break;
}
}
return 0;
}
3、总结反思:
本次周赛的第一题算是打卡题,基本上没什么难度。
二、第二题AcWing 4397. 卡牌
2、比赛时写的代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 200010;
int dis[N];
int a[N],b[N];
int main()
{
ios::sync_with_stdio(false);
int n,k;
cin >> n >> k;
int sum = 0;
int cnt = 0;
for(int i=0; i < n; i ++){
cin >> a[i];
}
for(int i=0; i < n; i ++){
cin >> b[i];
}
for (int i = 0; i < n; i ++ ){
sum += min(a[i],b[i]);
if(a[i] < b[i]) cnt ++;
if(b[i] < a[i]) dis[i] = abs(a[i]-b[i]);
}
sort(dis,dis+n);
for (int i = 0; i < n; i ++ ){
if(cnt >= k) break;
sum += dis[i];
cnt ++;
}
cout << sum;
return 0;
}
3、总结反思:
很遗憾的是这次并没有把第二题写出来,当时思考了四五十分钟,最终由于实力不足只能选择放弃。测试样例通过从16/20改到14/20,直接给我整不自信了。
先看一下y总提供的参考代码:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 200010;
int n, m;
int a[N], b[N], d[N];
int main()
{
scanf("%d%d", &n, &m);
int sum = 0;
for (int i = 1; i <= n; i ++ ) scanf("%d", &a[i]), sum += a[i];
for (int i = 1; i <= n; i ++ ) scanf("%d", &b[i]), d[i] = b[i] - a[i];
sort(d + 1, d + n + 1);
for (int i = 1; i <= n - m; i ++ )
if (d[i] >= 0) break;
else sum += d[i];
printf("%d\n", sum);
return 0;
}
其实现在赛后来看,这题难度不算大。y总的这个题解的主要思想就是,先把所有的a[i]都存入sum中,然后再翻牌,由题可知,翻牌的数量是小于等于n-k的(题解中的m即k)。所以可以先用一个d[N]数组来,存储b[i]-a[i]的差值。因为要保证数字之和尽可能小,所以可以先对d[N]数组进行排序,然后选取d[i]<0的数组d[i],最后输出sum即可。
三、第三题AcWing 4398. 查询字符串
2、本题目前在我的能力范围之外,暂不做总结。