当前位置:网站首页>J-luggage lock of ICPC Shenyang station in 2021 regional games (simple code)
J-luggage lock of ICPC Shenyang station in 2021 regional games (simple code)
2022-07-03 14:22:00 【Int me】
The question : One will be given 4 Turn the lock of position to another four position lock , You can turn multiple consecutive locks at a time +1 perhaps -1, Ask how many times you can turn it into a target lock
Input
6 1234 2345 1234 0123 1234 2267 1234 3401 1234 1344 1234 2468
Output
1 1 4 5 1 4
General idea :
What I do with this question is to divide it into two questions :
1. One is to give an array of positive numbers , such as 4,5,6,2, You can drop a continuous interval at a time , How many times at least .
It's harder , If there is a negative number , What if it can rise and fall continuously :4,-2,5 The answer is 11.
2. And then calculate 4 Bit difference , such as 4562 To 5671 The difference is -1,-1,-1,1, As long as the above problems are solved , Bring in this array to get the answer .
however 4562 To 5671 just so so 9,-1,-1,1, Each person can add or subtract 2^4 Second case , Search lists this 16 Array to find the minimum answer .
Specific solution :
The first 1 A question :
With the original title :[NOIP2018 Improvement group ] Lay the road - Luogu
For arrays that are all positive numbers, it's simple : The answer is sum
for(int i=1;i<=n;i++)
if(a[i-1]<a[i])
sum+=a[i]-a[i-1];
For arrays with negative numbers , You can add one more between positive and negative numbers 0, A negative number becomes its absolute value
1,2,-1,-2,3 -> 1,2,0,1,2,0,3
Of course, this is just an easy way to understand , add 0 too troublesome , It can be calculated directly , In fact, it means adding 0 Calculation after :
void solve(){
int sum=0;
for(int i=1;i<=4;i++){
if(ans[i-1]*ans[i]<0) sum+=abs(ans[i]); //ans It's a length of 5 Array of ,ans[0]=0, Back 4 One is effective
else if(abs(ans[i])>abs(ans[i-1])) sum+=abs(ans[i])-abs(ans[i-1]);
}
minn=min(minn,sum);
}
The first 2 A question :
It's not a problem , But point it out ans Array Is the difference array of input data
If you know this bit ans[i]<0, Then again +10, If ans[i]>=0, Then again -10, Enumerate 16 Array of
Because the first 1 Problem time complexity O(n) It's solved ,3^4 Enumeration of is also ok :
void dfs(int d){
if(d==5){
solve(); // Array ans Produce a result
return;
}
ans[d]=x[d-1]-y[d-1]; // Take difference
dfs(d+1);
ans[d]=x[d-1]-y[d-1]+10; // This bit +10
dfs(d+1);
ans[d]=x[d-1]-y[d-1]-10; // This bit -10
dfs(d+1);
}
Code :
#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]; // Take difference
dfs(d+1);
ans[d]=x[d-1]-y[d-1]+10; // This bit +10
dfs(d+1);
ans[d]=x[d-1]-y[d-1]-10; // This bit -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;
}
边栏推荐
- x86汇编语言-从实模式到保护模式 笔记
- 修改数据库中的记录为什么报这个错
- 7-9 find a small ball with a balance
- Message subscription and publishing
- Learn to punch in today
- Exercise 9-3 plane vector addition
- 7-28 monkeys choose King (Joseph problem)
- Find the sum of the elements of each row of the matrix
- Although not necessarily the best, it must be the hardest!
- Webpage connection database ~ simple implementation of addition, deletion, modification and query complete code
猜你喜欢
[clean up the extraordinary image of Disk C]
Programmable logic device software testing
JS matrix zero
Doris学习笔记之数据表的创建
好看、好用、强大的手写笔记软件综合评测:Notability、GoodNotes、MarginNote、随手写、Notes Writers、CollaNote、CollaNote、Prodrafts、Noteshelf、FlowUs、OneNote、苹果备忘录
7-7 12-24 hour system
7-9 find a small ball with a balance
Exercise 10-3 recursive implementation of exponential functions
JVM class loading
QT learning 17 dialog box and its types
随机推荐
JS input number and standard digit number are compared. The problem of adding 0 to 0
Print. JS -- web page file printing
Leetcode (4) - - trouver la médiane de deux tableaux ordonnés positifs
Reflection -- basic usage
虽然不一定最优秀,但一定是最努力的!
JVM垃圾回收机
String substitution
QT learning 25 layout manager (4)
Jiuyi cloud black free encryption free version source code
Find the sum of the elements of each row of the matrix
The mail function of LNMP environment cannot send mail
必贝特医药冲刺科创板:年营收97万亏损1.37亿 拟募资20亿
Thinking about the arrangement problem in the backtracking problem (leetcode questions 46 and 47)
allegro,orcad, net alias,port,off-page connector之间的异同点和如何选取
玖逸云黑免费无加密版本源码
6-9 statistics of single digits (15 points)
Fabric. JS document
Leetcode(4)——尋找兩個正序數組的中比特數
Redis:字符串類型數據的操作命令
JS matrix zero