2025.1.21——1300
2025.1.21——1300
A 1300
Qingshan has a string
A string
for all .
For Div. 2 contestants, note that this condition is different from the condition in problem B.
For example,
Qingshan wants to make
- insert
to any position of (getting a new ).
Please tell Qingshan if it is possible to make
Input
The input consists of multiple test cases. The first line contains a single integer
The first line of each test case contains a single integer
The second line of each test case contains a string
It is guaranteed that
B 1300
Tema decided to improve his ice cream making skills. He has already learned how to make ice cream in a cone using exactly two balls.
Before his ice cream obsession, Tema was interested in mathematics. Therefore, he is curious about the minimum number of balls he needs to have in order to make exactly
There are plenty possible ice cream flavours:
Two ice creams are considered different if their sets of ball flavours are different. For example,
For example, having the following ice cream balls:
Note, that Tema do not need to make all the ice cream cones at the same time. This means that he making ice cream cones independently. Also in order to make a following cone
Help Tema answer this question. It can be shown that answer always exist.
Input
Each test consists of multiple test cases. The first line of input contains a single integer
The first line of each test case contains a single integer
------------------------思考------------------------
-
字符串/贪心策略+思维/贪心策略/二分/数学
A
- 首先保证
数量相等,一个方向是不动原字符串/对称轴不变,构造出一个一对一的字符串,但是做不到。 - 只能在两边加串,贪心消除即可,操作次数至多
次。暴力拼接更新字符串即可,注意点就是 下标要细节一下。本质是将首尾字符进行移动。
B
- 是道好题。二分找出上界,剩余部分贪心,思想上证明一下。
------------------------代码------------------------
A
#include <bits/stdc++.h>
#define int long long //
#define endl '\n' // attention: interactive/debug
#define el cout << endl
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
#define bugv(VEC, ST) \
for (int i = ST; i < VEC.size(); i++) \
cout << VEC[i] << ' '; \
el;
void _();
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cout << fixed << setprecision(10);
int T = 1;
cin >> T;
while (T--)
_();
return 0;
}
void _()
{
int n;
cin >> n;
string s;
cin >> s;
int cnt[2] = {};
for (auto v : s)
cnt[v - '0']++;
if (cnt[0] - cnt[1])
{
cout << -1;
el;
return;
}
int l = 0, r = n - 1;
vector<int> res;
// int t = 1e3;
while (l < r)
{
// t--;
// if (!t)
// break;
if (s[l] - s[r])
{
l++, r--;
continue;
}
if (s[l] == '1')
{
res.push_back(l);
s = s.substr(0, l) + "01" + s.substr(l);
}
else
{
res.push_back(r + 1);
// bug2(s.substr(0, r + 1), s.substr(r + 1));
s = s.substr(0, r + 1) + "01" + s.substr(r + 1);
}
l++, r++;
// bug2(l, r);
}
cout << res.size();
el;
bugv(res, 0);
}
B
#include <bits/stdc++.h>
#define int long long //
#define endl '\n' // attention: interactive/debug
#define el cout << endl
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
#define bugv(VEC, ST) \
{ \
for (int I = ST; I < VEC.size(); I++) \
cout << VEC[I] << ' '; \
el; \
}
void _();
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cout << fixed << setprecision(10);
int T = 1;
cin >> T;
while (T--)
_();
return 0;
}
void _()
{
int n;
cin >> n;
int l = 1, r = 1e10;
while (r - l - 1)
{
int mid = l + r >> 1;
if (mid * (mid - 1) >> 1 > n)
r = mid;
else
l = mid;
}
int res = l;
res += n - (l * (l - 1) >> 1);
cout << res << endl;
}
// void _()
// {
// auto cal = [](int mid)
// {
// return mid * (mid - 1) >> 1;
// };
// for (int i = 2; i <= 20; i++)
// bug2(i, cal(i));
// }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!