8.14_407_课堂总结
T637642 比较数字
错误代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
string s;
cin >> s;
for(int i = 0; i < s.size(); i++){
if(s[i] == '0'){
s[i] = '1';
}
}
cout << s;
return 0;
}
分析
根据最初的想法,只要遇到了0,就替换成1,因为比0的数中1是最小的,可是这种方法真的是对的吗?
不是!为什么呢?因为我没AC 让我们来看下面一组数据:
输入:12302000
根据最初的代码,输出为:12312111,可正确的答案并不是我们的输出,而是12311111
再看几组数据,如1234505000、1110123000,我们就得出了以下结论:
将第一个0后面的数以及它本身全部替换为1即可
正解代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
string s;
bool flag = false;
cin >> s;
for(int i = 0; i < s.size(); i++){
if(s[i] == '0'){
flag = true;
}
if(flag){
s[i] = '1';
}
}
cout << s;
return 0;
}
T637627 张的弓
错误代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int t;
cin >> t;
while(t--){
int n, m, k;
cin >> n >> m >> k;
int h = k / n + 1, l;
if(h % 2 == 0){
l = n - (k % n);
}else{
l = k % n + 1;
}
cout << abs(1 - h) + abs(1 - l) << "\n";
}
return 0;
}
分析
不开long long见祖宗!!!o (T^T o)
T637602 可可口乐
可可口乐?
错误代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
string s, k = "cocacola";
cin >> s;
int cnt = 0;
for(int i = 0; i < s.size(); i++){
if(s[i] != k[i]){
cnt++;
for(int j = i + 1; j < s.size(); j++){
if(s[j] == k[i]){
swap(s[i], s[j]);
}
}
}
}
cout << cnt;
return 0;
}
分析
只交换能配对上的两个数不一定是最优解,上面的写法算是个贪心,正解是用BFS/DFS
正解代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
string k = "cocacola";
map<string, bool> mp;
struct Node{
string s;
int step;
};
void bfs(string s){
queue<Node> q;
q.push({s, 0});
mp[s] = true;
while(q.size()){
Node f = q.front();
q.pop();
if(f.s == k){
cout << f.step;
return;
}
int p;
for(int i = 0; i < 8; i++){
if(f.s[i] != k[i]){
p = i;
break;
}
}
for(int i = p + 1; i < 8; i++){
if(f.s[i] != f.s[p]){
string t = f.s;
swap(t[i], t[p]);
if(!mp[t]){
mp[t] = true;
q.push({t, f.step + 1});
}
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
string s;
cin >> s;
bfs(s);
return 0;
}
T637561 小梦的守卫
错误代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a[200001], d[200001], mxs;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int n, q;
cin >> n >> q;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
int l = 1, r = n;
ll s = 0;
d[0] = n;
while(l <= r){
s++;
d[s] = d[s - 1];
a[l]--;
if(l != r){
a[r]--;
}
if(a[l] == 0){
d[s]--;
mxs = max(mxs, s);
l++;
}
if(a[r] == 0){
d[s]--;
mxs = max(mxs, s);
r--;
}
}
while(q--){
int t;
cin >> t;
if(t >= mxs){
cout << "Zombies ate your brain!\n";
continue;
}
cout << d[t] << "\n";
}
return 0;
}
分析
数组开小了,把d数组开大一些可以拿20pts,MLE,正解是用两个前缀和数组记录左边血量和右边血量,再用二分查找一共去世了多少人,如果所有人都没了,输出无解
正解代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a[200001], cnt[200001], Cnt[200001];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int n, q;
cin >> n >> q;
for(int i = 1; i <= n; i++){
cin >> a[i];
cnt[i] = cnt[i - 1] + a[i];
}
for(int i = n; i >= 1; i--){
Cnt[n - i + 1] = Cnt[n - i] + a[i];
}
while(q--){
ll t;
cin >> t;
int l = upper_bound(cnt + 1, cnt + 1 + n, t) - cnt - 1;
int r = upper_bound(Cnt + 1, Cnt + 1 + n, t) - Cnt - 1;
if(l + r > n){
cout << "Zombies ate your brain!\n";
}else{
cout << n - l - r << "\n";
}
}
return 0;
}