2024MX-MF-DAY1-text题解
【题目描述】
有
初始,每个人手上有
- 把自己手中的球分成数量相同且尽可能多的三份,扔掉剩余的球,再把分得的三份分别给自己、左边的人,以及右边的人。
问所有人均操作完一次后,有球最多的人有几个球。
【输入格式】
从标准输入读入数据。
一行两个整数
【输出格式】
输出到标准输出。
一行表示一个整数表示答案。
【数据范围】
对于
对于
对于
思路
赛时思路:
假设每个人手上的球颜色不同,那么显然每种颜色的球 最多延申(被传递)
点击查看代码
#include <iostream>
using namespace std;
int main(){
int n, m;
cin >> n >> m;
int x = m / 3, val = 0;
for(int i = 2,last = m;i <= m;i++){
int now = last / 3 + m;
if(now == last || n == i){
val = now;
break;
}
last = now;
}
cout << x + (m + x) / 3 + (val + x) / 3 << '\n';
return 0;
}
折线(line)
【题目描述】
图 1:
如上,在平面直角坐标系
1 n
,询问点 的坐标;2 l r
,询问折线段 的长度;
【输入格式】
从标准输入读入数据。
第一行一个正整数
接下来 1 n
或 2 l r
。
【输出格式】
输出到标准输出。
对于每一个询问,如果是
【数据范围】
对于
思路
点击查看代码
#include <iostream>
using namespace std;
long long x[1000000];
int main() {
int n;
cin >> n;
for (int i = 1; i < 1000000; i++) {
if (i % 2 == 1) {
x[i] = x[i - 1] + (i + 1) / 2;
} else {
x[i] = x[i - 1] + i / 2 + 1;
}
}
for (int i = 1; i <= n; i++) {
int a, b, c;
cin >> a;
if (a == 1) {
cin >> b;
if (b % 4 == 1) {
cout << b / 4 + 1 << " " << (b / 4 + 1) * (-1) + 1 << endl;
} else if (b % 4 == 2) {
cout << b / 4 + 1 << " " << b / 4 + 1 << endl;
} else if (b % 4 == 3) {
cout << (b / 4 + 1) * (-1) << " " << b / 4 + 1 << endl;
} else {
cout << (b / 4) * (-1) << " " << (b / 4) * (-1) << endl;
}
} else {
cin >> b >> c;
cout << x[c - 1] - x[b - 1] << endl;
}
}
return 0;
}
STL
【题目描述】
有
现在他希望在字符串之间以及最后一个字符串之后的空格中添上
例如:
小 L 对此感到好奇,于是他给你
l r op
:询问
【输入格式】
从标准输入读入数据。
本题包含多组数据。
第一行两个正整数
接下来一行
接下来
【输出格式】
输出到标准输出。
对每个询问,首先输出一行 Yes
或 No
表示解是否存在。若该询问对应的
【数据范围】
对于
思路
首先我们注意到这个 SPJ 是在诈骗。
注意到
那我们如何构造一个合法方案呢?
看到后缀表达式,我们考虑用一个栈来记录信息。栈中每个元素表示一个区间 <
,再在 >
;遇到 <
,在 ,
,最后在 >
即可。
以上任何一步需要弹栈时栈空或最终栈中元素个数不为
那要是我们不需要构造方案呢?
现在依次考虑上述无解的两个条件。
最终栈中元素个数:注意到每遇到一个
于是我们预处理一个每个位置对栈中元素个数贡献的前缀和
弹栈时栈不空:对于所有
当然,我们可以运用 ST 表等 RMQ 算法求出区间后缀和最小值,但是超纲了。
对于
满足
中是否有
分别排序后离线链表即可。时间复杂度为
点击查看代码
#include<bits/stdc++.h>
using namespace std;
int n,m,c[1000005],pre[1000005],Log[1000005],st[22][1000005],l,r,op;char s[15];
struct Link
{
int pre,nxt;char c;
}a[10000005];
int query(int l,int r)
{
if(l>r)return 0x3f3f3f3f;
int k=Log[r-l+1];
return min(st[k][l],st[k][r-(1<<k)+1]);
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>s;
if(s[0]=='i')c[i]=1;
else if(s[0]=='v')c[i]=2;
else c[i]=3;
}
for(int i=1;i<=n;i++)pre[i]=pre[i-1]+(c[i]==1?-1:(c[i]==2?0:1));
for(int i=1;i<=n;i++)st[0][i]=pre[i];
for(int i=1;(1<<i)<=n;i++)
{
for(int j=1;j+(1<<i)-1<=n;j++)st[i][j]=min(st[i-1][j],st[i-1][j+(1<<(i-1))]);
}
for(int i=2;i<=n;i++)Log[i]=Log[i>>1]+1;
while(m--)
{
cin>>l>>r>>op;
if(pre[r]-pre[l-1]!=-1||query(l,r-1)-pre[l-1]<=-1)cout<<"No\n";
else
{
cout<<"Yes\n";
if(op==1)
{
stack<pair<int,int> >st;
int tot=0;
for(int i=r;i>=l;i--)
{
if(c[i]==1)
{
a[++tot].c='i';a[++tot].c='n';a[++tot].c='t';
for(int j=0;j<3;j++)a[tot-j].pre=tot-j-1,a[tot-j].nxt=tot-j+1;
if(st.size())a[tot].nxt=st.top().first;
else a[tot].nxt=0;
a[tot-2].pre=0;
st.push(make_pair(tot-2,tot));
}
else if(c[i]==2)
{
int l=st.top().first,r=st.top().second;st.pop();
a[++tot].c='v';a[++tot].c='e';a[++tot].c='c';
a[++tot].c='t';a[++tot].c='o';a[++tot].c='r';a[++tot].c='<';
for(int j=0;j<7;j++)a[tot-j].pre=tot-j-1,a[tot-j].nxt=tot-j+1;
a[tot-6].pre=0;a[tot].nxt=l;a[l].pre=tot;
a[++tot].c='>';
a[tot].pre=r;a[tot].nxt=a[r].nxt;a[r].nxt=tot;
st.push(make_pair(tot-7,tot));
}
else
{
int pl=st.top().first,pr=st.top().second;st.pop();
int ql=st.top().first,qr=st.top().second;st.pop();
a[++tot].c='p';a[++tot].c='a';a[++tot].c='i';a[++tot].c='r';a[++tot].c='<';
for(int j=0;j<5;j++)a[tot-j].pre=tot-j-1,a[tot-j].nxt=tot-j+1;
a[tot-4].pre=0;a[tot].nxt=pl;a[pl].pre=tot;
a[++tot].c=',';
a[tot].pre=pr;a[tot].nxt=ql;a[pr].nxt=tot;a[ql].pre=tot;
a[++tot].c='>';
a[tot].pre=qr;a[tot].nxt=a[qr].nxt;a[qr].nxt=tot;
st.push(make_pair(tot-6,tot));
}
}
for(int i=st.top().first;i;i=a[i].nxt)cout<<a[i].c;
for(int i=1;i<=tot;i++)a[i].pre=a[i].nxt=a[i].c=0;
cout<<'\n';
}
}
}
return 0;
}
【题目描述】
现在有
1.等概率随机地选择一个之前从未选过的球(白球、黑球都可以)。
2.将该球和它相邻的两个球都染黑。
3.如果所有白球都染黑,那么结束。
求出染黑所有白球的期望步数,答案对
【输入格式】
一行一个正整数
【输出格式】
一行一个数字,表示期望步数,对
【数据范围与提示】
对于
思路
点击查看代码
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int Pow(int a, int b) {
int res = 1;
while (b) {
if (b & 1)
res = 1LL * res * a % mod;
a = 1LL * a * a % mod;
b >>= 1;
}
return res;
}
int a[15], f[15];
int n;
int sum = 0;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) f[i] = i;
do {
for (int i = 1; i <= n; i++) a[i] = 0;
int cnt = 0;
for (int i = 1; i <= n; i++) {
int now = f[i];
int pre = ((now == 1) ? n : (now - 1));
int nxt = ((now == n) ? 1 : (now + 1));
cnt += (!a[pre]);
a[pre] = 1;
cnt += (!a[now]);
a[now] = 1;
cnt += (!a[nxt]);
a[nxt] = 1;
if (cnt == n) {
sum = (sum + i) % mod;
break;
}
}
} while (next_permutation(f + 1, f + n + 1));
int s = 1;
for (int i = 1; i <= n; i++) s = 1LL * s * i % mod;
cout << 1LL * sum * Pow(s, mod - 2) % mod;
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!