1743C:Save the Magazines
题目链接:https://codeforces.com/problemset/problem/1743/C
经验:有关数组越界,从后往前数要防止数到超出0的范围,同时要防止尾部a[0]添加超过1次。
Ps:感觉自己写的代码好冗长(っ╥╯﹏╰╥c)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ll t;
cin >> t;
while(t--){
ll n;
cin >> n;
string s;
cin >> s;
ll a[n];
ll ans = 0;
for (ll i = 0; i < n;i++){
cin >> a[i];
}
for (ll i = n - 1; i >= 0;i--){
if(s[i]=='1'){
if(i==0){
ans += a[i];
break;
}
if(s[i-1]=='0'){
if(a[i-1]>a[i]){
ans += a[i - 1];
i--;
}else{
ans += a[i];
i--;
}
}else{
ll origin = a[i];
bool c = false;
while(s[i-1]!='0'){
if(a[i-1]<origin){
ans += origin;
origin = a[i - 1];
}else{
ans += a[i - 1];
}
if(i==1){
ans += origin;
c = true;
break;
}
i--;
}
if(!c){
if(a[i-1]>origin){
ans += a[i - 1];
}else{
ans += origin;
}
i--;
}else{
break;
}
}
}
}
cout << ans << endl;
}
return 0;
}
1994B:Fun Game
题目链接:https://codeforces.com/problemset/problem/1994/B
经验:一直卡在第十个样例超时,因为我把字符串的比较写在循环里面了,而两个字符串如果长度相等的话,时间复杂度是O(n)的,这样叠加起来就导致第十个样例超时了。两个字符串的比较只有长度不等的时候,时间复杂度是O(n)的。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
string s, t;
cin >> s >> t;
if(s==t){
cout << "YES" << endl;
continue;
}
for (int i = 0; i < n;i++){
if (s[i] == '1')
{
cout << "YES" << endl;
break;
}
else if (t[i] == '1')
{
cout << "NO" << endl;
break;
}
}
}
return 0;
}