当前位置:网站首页>2021年区域赛ICPC沈阳站J-Luggage Lock(代码简洁)
2021年区域赛ICPC沈阳站J-Luggage Lock(代码简洁)
2022-07-03 13:44:00 【int 我】
题意:将给一个4位的锁转到另一个四位的锁,可以一次转动多个连续的锁+1或者-1,问多少次可以转成目标锁
输入
6 1234 2345 1234 0123 1234 2267 1234 3401 1234 1344 1234 2468
输出
1 1 4 5 1 4
总体思路:
这题我的做法就是分成两个题:
1.一个是给一个正数数组,比如4,5,6,2,每次可以下降一个连续的区间,需要最少多少次。
难一点,如果其中有负数,可以连续上升和下降又怎么算:4,-2,5的答案是11。
2.然后计算出4位的差,比如4562到5671的差就是-1,-1,-1,1,上面问题只要解决了,带入该数组可以出来答案。
但是4562到5671还可以9,-1,-1,1,每位加还是减就有2^4次种情况,搜索列举出这16个数组求出最小答案即可。
具体解法:
第1个问题:
对于全是正数的数组很简单:答案就是sum
for(int i=1;i<=n;i++)
if(a[i-1]<a[i])
sum+=a[i]-a[i-1];对于有负数的数组,可以在正数和负数中间多加一个0,负数变为其绝对值
1,2,-1,-2,3 -> 1,2,0,1,2,0,3
当然这只是便于理解的做法,加上0太麻烦,可以直接算,其实也就是在中间加上0后的计算:
void solve(){
int sum=0;
for(int i=1;i<=4;i++){
if(ans[i-1]*ans[i]<0) sum+=abs(ans[i]); //ans是长度为5的数组,ans[0]=0,后面4个是有效的
else if(abs(ans[i])>abs(ans[i-1])) sum+=abs(ans[i])-abs(ans[i-1]);
}
minn=min(minn,sum);
}第2个问题:
就不算问题了 ,但是指明一下说的ans数组是输入数据的差数组
如果可知该位ans[i]<0,那么再+10,如果ans[i]>=0,那么再-10,列举出16种数组即可
因为第1个问题时间复杂度O(n)解决了,3^4的枚举也可以的:
void dfs(int d){
if(d==5){
solve(); //数组ans产生一个结果
return;
}
ans[d]=x[d-1]-y[d-1]; //取差
dfs(d+1);
ans[d]=x[d-1]-y[d-1]+10; //该位+10
dfs(d+1);
ans[d]=x[d-1]-y[d-1]-10; //该位-10
dfs(d+1);
}代码:
#include<bits/stdc++.h>
using namespace std;
#define fo(a) for(int i=0;i<a;i++)
#define int long long
#define PII pair<int,int>
#define fi first
#define se second
#define M 1000005
int T,minn;
string s;
vector<int>x,y;
vector<int>ans(5);
void solve(){
int sum=0;
for(int i=1;i<=4;i++){
if(ans[i-1]*ans[i]<0) sum+=abs(ans[i]);
else if(abs(ans[i])>abs(ans[i-1])) sum+=abs(ans[i])-abs(ans[i-1]);
}
minn=min(minn,sum);
}
void dfs(int d){
if(d==5){
solve();
return;
}
ans[d]=x[d-1]-y[d-1]; //取差
dfs(d+1);
ans[d]=x[d-1]-y[d-1]+10; //该位+10
dfs(d+1);
ans[d]=x[d-1]-y[d-1]-10; //该位-10
dfs(d+1);
}
signed main(){
cin>>T;
while(T--){
minn=1e9;
x.clear();y.clear();
cin>>s;
fo(4) x.push_back(s[i]-'0');
cin>>s;
fo(4) y.push_back(s[i]-'0');
dfs(1);
cout<<minn<<'\n';
}
return 0;
}
边栏推荐
- How to use lxml to judge whether the website announcement is updated
- Formation of mil-100 (FE) coated small molecule aspirin [email protected] (FE) | glycyrrhetinic acid modified metal organ
- 消息订阅与发布
- Understanding of closures
- Redis:字符串類型數據的操作命令
- Collection of mobile adaptation related articles
- 金属有机骨架MIL-88负载阿霉素DOX|叶酸修饰UiO-66-NH2负载阿霉素[email protected]纳米粒子
- Exercise 9-3 plane vector addition
- QT learning 23 layout manager (II)
- Eight sorts
猜你喜欢

泰凌冲刺科创板:拟募资13亿 国家大基金与小米长江是股东

【吉林大学】考研初试复试资料分享

Exercise 10-2 recursive factorial sum

UiO-66-COOH装载苯达莫司汀|羟基磷灰石( HA) 包裹MIL-53(Fe)纳米粒子|装载黄芩苷锰基金属有机骨架材料

7-8 overspeed judgment
[email protected]纳米粒子"/>金属有机骨架MIL-88负载阿霉素DOX|叶酸修饰UiO-66-NH2负载阿霉素[email protected]纳米粒子

Metal organic framework MOFs loaded with non steroidal anti-inflammatory drugs | zif-8 wrapped Prussian blue loaded quercetin (preparation method)
[email protected])|制备路线"/>叶酸修饰的金属-有机骨架(ZIF-8)载黄芩苷|金属有机骨架复合磁性材料([email protected])|制备路线

JVM class loading

Leetcode (4) - - trouver la médiane de deux tableaux ordonnés positifs
随机推荐
js . Find the first palindrome string in the array
Exercise 9-3 plane vector addition
信创产业现状、分析与预测
Dlopen() implements dynamic loading of third-party libraries
7-7 12-24 hour system
QT learning 17 dialog box and its types
Exercise 6-2 using functions to sum special A-string sequences
Leetcode(4)——尋找兩個正序數組的中比特數
天图投资冲刺港股:资产管理规模249亿 投了小红书与奈雪
Current situation, analysis and prediction of information and innovation industry
Metal organic framework MOFs loaded with non steroidal anti-inflammatory drugs | zif-8 wrapped Prussian blue loaded quercetin (preparation method)
Redis: redis data structure and key operation commands
C library function - qsort()
Eight sorts
simpleParallax. JS (create poor visual effects for website pictures)
JS first summary
JVM family - overview, program counter day1-1
Solution to failure or slow downloading of electron when electron uses electron builder to package
Use vscode to view hex or UTF-8 codes
Fabric. JS document