专栏文章
题解:P13132 [GCJ 2018 Qualification] Saving The Universe Again
P13132题解参与者 1已保存评论 0
文章操作
快速查看文章及其快照的属性,并进行相关操作。
- 当前评论
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @mioxua3a
- 此快照首次捕获于
- 2025/12/03 02:55 3 个月前
- 此快照最后确认于
- 2025/12/03 02:55 3 个月前
看题
要求通过最少次相邻交换,将总伤害降低到 。不能做到就输出
IMPOSSIBLE。怎么做?
每个 的伤害由前面所有 的乘积决定,所以我们要尽量把 向后移,减小 的影响。
首先,不断从右往左找
首先,不断从右往左找
cs,换成 sc,每次交换后计算一次总伤害;如果伤害 ,结束;如果无法再换,输出 IMPOSSIBLE。上代码
CPP#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define copy(a,b) copy(begin(a),end(a),begin(b))
#define ld long double
#define dot(x) fixed<<setprecision(x)
#define foru(a,b,c) for(ll a=b;a<=c;a++)
ll T,d;
string p;
ll calc(string s){
ll res=0,pw=1;
for(auto c:s){
if(c=='C') pw*=2;
else res+=pw;
}
return res;
}
int main(){
cin>>T;
foru(t,1,T){
cin>>d>>p;
ll ans=0;
while(1){
if(calc(p)<=d) break;
bool ok=0;
for(ll i=p.size()-2;i>=0;i--){
if(p[i]=='C'&&p[i+1]=='S'){
swap(p[i],p[i+1]);
ans++,ok=1;
break;
}
}
if(!ok){
ans=-1;
break;
}
}
cout<<"Case #"<<t<<": ";
if(ans==-1) cout<<"IMPOSSIBLE"<<endl;
else cout<<ans<<endl;
}
return 0;
}
相关推荐
评论
共 0 条评论,欢迎与作者交流。
正在加载评论...