制作地图的网站,提供设计的网站,呼和浩特哪里做网站,WordPress更改adminA.Rating Increase#xff08;思维#xff09;
题意#xff1a;
给出一个仅包含数字的字符串 s s s#xff0c;要求将该字符串按以下要求分成左右两部分 a , b a,b a,b#xff1a; 两个数字均不包含前导 0 0 0 两个数字均大于 0 0 0 b a b a ba
如果…A.Rating Increase思维
题意
给出一个仅包含数字的字符串 s s s要求将该字符串按以下要求分成左右两部分 a , b a,b a,b 两个数字均不包含前导 0 0 0 两个数字均大于 0 0 0 b a b a ba
如果有多个答案输出任意一个均可。
分析
既然题目要求 b a b a ba且不能包含前导 0 0 0那么将字符串中第一个数字以及之后的连续的 0 0 0分配给 a a a剩余部分属于 b b b然后判断 b b b是否大于 a a a即可。
注使用字符串比较时要注意不能直接使用 进行比较否则比较的是两个字符串的字典序而不是数字大小。
代码
#include bits/stdc.h
using namespace std;
typedef long long LL;
const int N 1e5 5e2;vectorint V[30];void solve() {string s;cin s;string a , b ;int i, len s.size();for (i 0; i len; i) {if (i ! 0 s[i] ! 0) break;a.push_back(s[i]);}for (; i len; i) {b.push_back(s[i]);}if (b.size() a.size() || (b.size() a.size() a b)) {cout a b endl;} else {cout -1 endl;}
}int main() {int Case;cin Case;while (Case--) {solve();}return 0;
}B.Swap and Delete思维
题意
给出仅包含 01 01 01的字符串 s s s在若干次操作该字符串变为字符串 t t t: 花费一个金币并删除字符串中一个字符 免费操作任意交换字符串中的字符
问最少花费多少金币能够使得最后的得到的字符串 t t t与原字符串 s s s中等长的前缀字符串相同下标的元素全部不同。
分析
既然操作2不需要花费金币那么肯定要尽可能的使用操作二。
因此先记录字符串 s s s中 1 1 1和 0 0 0的数量然后从前往后判断如果当前 s i 1 s_i 1 si1那么就把后面的 0 0 0交换过来并将记录的 0 0 0数量减一如果交换前发现此时后面已经没有 0 0 0了那么无论怎么交换均无法使得这个位置上的元素与原字符串 s s s不同因此所有后面的字符均需使用操作 1 1 1删除共消耗 n − i n - i n−i个金币。 s i 0 s_i 0 si0的情况同理。
代码
#include bits/stdc.h
using namespace std;
typedef long long LL;
const int N 2e5 5e2;void solve() {string s;cin s;int n s.size();int one 0, zero 0;int ans n;for (int i 0; i n; i) {if (s[i] 0) {zero;} else {one;}}for (int i 0; i n; i) {if (s[i] 1) {if (zero 0) {cout n - i endl;return;}zero--;} else {if (one 0) {cout n - i endl;return;}one--;}}cout 0 endl;
}int main() {int Case;cin Case;while (Case--) {solve();}return 0;
}C.Game with Multiset数学
题意
给出一个允许装下重复元素的集合并有以下两种操作 ADD x:将 2 x 2^{x} 2x放入集合 GET w:询问能否取出集合中的若干元素满足这些元素的总和为 w w w
分析
对于操作 1 1 1使用数组 c n t [ i ] cnt[i] cnt[i]表示存放的 2 i 2^{i} 2i的元素数量。
对于操作 2 2 2从大到小遍历集合中的元素通过运算得到当前剩余数字还能减去多少 2 i 2^{i} 2i并尽可能多的减去 2 i 2^{i} 2i。如果遍历结束后剩余数字为 0 0 0表示可以组成不为 0 0 0表示不能组成。
代码
#include bits/stdc.h
using namespace std;
typedef long long LL;
const int N 2e5 5e2;int mul_set[35];void add(int x) {mul_set[x];
}void get(int x) {for (int i 29; i 0; i--) {int cnt x / (1 i);//计算最多能减去多少个x - min(cnt, mul_set[i]) * (1 i);//注意减去的数量不能比集合中存的数量多}if (x 0) cout Yes endl;else cout No endl;
}int main() {int Case;cin Case;while (Case--) {int op, x;cin op x;if (op 1) add(x);else get(x);}return 0;
}D,E 更新中…
学习交流
以下为学习交流QQ群群号 546235402每周题解完成后都会转发到群中大家可以加群一起交流做题思路分享做题技巧欢迎大家的加入。