Codeforces Round #483 (Div. 2)题解
2 seconds
256 megabytes
standard input
standard output
Two players play a game.
Initially there are nn integers a1,a2,…,ana1,a2,…,an written on the board. Each turn a player selects one number and erases it from the board. This continues until there is only one number left on the board, i. e. n−1n−1 turns are made. The first player makes the first move, then players alternate turns.
The first player wants to minimize the last number that would be left on the board, while the second player wants to maximize it.
You want to know what number will be left on the board after n−1n−1 turns if both players make optimal moves.
The first line contains one integer nn (1≤n≤10001≤n≤1000) — the number of numbers on the board.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1061≤ai≤106).
Print one number that will be left on the board.
3
2 1 3
2
3
2 2 2
2
In the first sample, the first player erases 33 and the second erases 11. 22 is left on the board.
In the second sample, 22 is left on the board regardless of the actions of the players.
分析:看清题意,sort一下,奇数输出n / 2 + 1,偶数输出n / 2
/** :;LaEaHKEEGpPXU7;,
.:75pKH11252U252XapZgRQgD6XJscLr;,.
:LXpRgGaX521JLw1JswJJsJs22XHPPEZEGDOMMRDOa7.
.r2EDDZEpZPZP6KpHX5SXH5XXa5KwaXaSX5UJ1c77sLs2GMQQ6r .
,LpgOGpEZGZEZEpZKpHHU5wP5HEDgpXpHa2SSa5aSXULr7rrirrJXRBp; ;B
,J6MRZH6EgEEZE6E6EZZPZXXwSSGQXr::aPpP5USUHaHaKa5Lvrr7ri;rLHBB2: Kc
rpQDOpPPOGGZOGOZG6GEOEOEDPPGBa. .PaSSUXSUUUaUSaKXKS177r7rrrirSBBR7 .O,
:UBQOKPK6ZOOOEDEO6GZE6EpEpDgDBR: UBpXHa5aSaUS5SUS5XapPHJc7rrv7rr7sgBBs .g.
;gBMPXpO6GEOEOEOEGEOEE6EZEEDRGBB EB5pKSXpHKaHSX552S5aUHHEX17c7vr7777s5RBS: .R;
.sQBPXpDZOODOgODGOGgEDEOEOGgGgOOMB: LBKKSXSHa52aaKXHXKaa5aSaaHXSJLcL7vcc777JDBBg2;. Qi
;2ggp2EDDOggGEDGgDDOgGGZDOOZOGg6gEBX vBZaHUKaaUXXXSXXKXpXHXH5wwaa52U1wssLsLJccv1UDQBQ67. O7
:ZZUU5PROOEOZOZGGOODZOGgODZOOgOggRgRB; ;:..R6XaKKpP6PGppKPHpHpPX5aU21UUa5Sw52UwUJJv77L77sSpQMDU; ;B5
,SRJ7sSHGggEOZOEG6OODpOZgggOQQBBBBBQBBQ.,;;. LBOgOgDRDDZODMgQRgRDaa552a252UUa25w5UaU2sLvccs7r7sJZBBMr ,XQJ:
LQHr77J6RGOZDZOEGEDGgDRORQBBBQRDPU1Jscwa7.,::.:J7r;::::. ..,:;i7UOgRRRgDPH5SUSU52U2HHa1JJJLJLccLr71RBB, 7R2,
:RZv;77JSgGOZEODEDGOEggQBBBMS7;:,:,,.,.,.:7L;:,;.: ,: . ..... .:;rJU6GgGRggEZHPaKXX2S221Js1Lc7r:7QB. .XX:
7g1;;7rcXG6gpGDZGgZOOQBBQpr. ::::;:;:::Jr::sr;::;:.:vs,:::::::::::,, ,:7L5HGOggRgZUUU5wSUaJLc7r7BOiDr ...
.XX;;;irLHGKpZZZEKgDRBBB6i ,;;:;;;;r;;:s177:,;L7:;7:.rHi,:::::::::::::::::,. .,;7ZRQgO6KUUJsLwsJ7KBM. .....
JZ:;rrrc5EPHp6XgpRBBBE; :i:;;;;;:;;;:c7::r7;,::::::::rv:,:::::::::::,:,:,:,,,, .;sORQRGX21wsXU: .... .
.Br;iir72EHPHZ6EgBBR7. ..;ii:;;;;;;;;;:71r::.:7, .::7;:;H;.::,:j::::::,:::::,,,::,,, ,7wEDRZBMr .......
1D;:r;rwOPXPKH6BBX, .:;:;;;:::;:;;;;;::Ls,;ss..r. ,c77;sLU,:,:::,:::,:,:,:,,,:,,,:::. .:.rP:.......
D2:i;rJpKHXHXgg7 .,,::::;:::::;:;:;;:;SL7sS2, :. ::::,:U7.:::,:::,:,,,,,:,,.,,,.,:, ;L:. .. ...
:Qc;i7LGZPa6gBM, ...,,::::::::::::;;::;.JJ;ic: ;:::,v1,.:::;7,,,:,:,:,,,,.:,,,,.. :2wr. .......
sRr:rrwZGgBQR7. .,,::::::::::::::;::;:::Hr:7i ,;;:::U: .,,,:r.,,:,:,:,,,:,,,..... 7K2:. ..........
OX:irsXgQZ:. .,,:::,,:::,::::::::;;;::r5;r7: :;;;:7L ..,.,;:,:,:,,,:,:,,,,...... .rU6w;..............
.BJ71EK5;. .,,::,:,:,,,:,:,::::::::;:;.Ls;r7 :;:::s, ..,,,r:.:.,,,.:, .,..... . .;s5XJr,..,.............
1Mv::. .:,:,,,,,,,:::::::,;:::;:;::..J7;c: ,. ,rri:27 , .,:;. ,:,:,,:..,.... .rpPL;:.. ... .............
..7Ls: .,,,:,,,:,:,:::,:,:::::,:::::: .Srrr, .,:;;;::::..:7r;r1 .: :E:..,,:,...,,.., ..XBQ7, ................... ..
,;7v7r7:, ,,,,:.,,,,,,:,,::,:,,.::.,:.,:, :Jrr; ,r7,:.. ,:::L: . .7RJ .,.:.,......,,:MBs ..............,........
.:;vJs7i, ,,,., ..,,:,,,:,,,:,:.,rs,,.:,, ;J;c, .,::;; Lr.E: .,...,....:,:1Z: .........,.,...........:,.
.;,,:r7J1wv;,. ....,.:. .,.,,:,,,:,,::::,sS;.:::, cLrr. . .,. .,..: ;; r5 ........,,:;s7. ....,.,.,...........,.,.,,.
,BBs::: . ........,.,,,,,.:,,,:,:,,.;s7r,,,,, cc7; ,.,,. .:7rrJGMPOEL1, ..... ,,:rSr, : ,.,.,.,.,.........,,,,,. ..
rZL. ....... ..,,,.,,,..,,,.,;,...7J:s: .. .wr7, ... .rJpQBBBBBBBQgKP77s .. .,7S2,,....,....,.,.,......,,.,. .:cX2
;SH7, . . . .. .,.,.,.,,,...,..r, ::Ur;7L . .57;. .rPBBQBBBPws:;r::::.,:P. .;S5;,..,...,.,.,.,.... ..,,, ..,7HSJvr
r7r;: . ... ..........,...,.. 7L;,rS;;ivr . 1r: . .rZBBK7;.JL,::Jrs;.:;,:;J, .,;LDv..:.,...,.,.,.,...,.,,:,, ,7sKGwc77;
.:7L7:, . ,r,..,.,,,...,..:iLL 7s;r;cv: U7. :vi;:. .. :Er:::.Ls. . ,7::::vHEi ,:,,.,,,.,.,,,,,,::::, .:1QBKJJUsssc
.:rvw1JsL7;, ,;..........,.:ir :r .ELr77v:,. Pr .. . Ls ,w. rr.r:JPr. ::,..,,,.,,,,:,:::,:,..7XgRE52US25w1c
.:;7JRQpX: ; .........,i: .,J ;gri;7r : 1; .X: . ,v :aK1, ,,:.,,,.,,,,:::::,;::::cOBB6K55UXUX5XUw
;J,XQB7,: . ...,: .:7Z.7Zi7;r: , v, :w:;,.,:::7LUsc:..:,:.:,,,,,:,:::::::,:;c5BBPS5wSUSUaUaUa2
J7 rEBBg..,. ..... . ,L1PLKr:,:;r;;::, :, :;SXsJU1XLLr....:,,,,,,::::,::::::,::LL,,rZXUJU25552aUUUU
JL .:sXBB:... ., . ,OH777;,rZRBBBQL ..r, ,:r7::r:,;;....:,,,,,,,,.::::;::;:::.7gL UK2UUS1aw2wU25J
U7 .r71BB; . :.. .. :QJ;.;1pBBBBBGRBRi i: . .w7. ..,,:,,,,,:,:,:::::::,.,2BR: .DSaUSUUS525w5U1
2r :7rJBQ7 .;. . KL::JGBBBgE6Hp6XMQ; . ... ,2s ,,,,:::::,:::::::,,.:sQQB2 ;DHa5U52S2U25USJ
a: :7;1BB2 .v, sL;LPQgDBPH6KPQBpGBG . ... .. aP:. .. ,::,:::::,,,,.;rSgBQDa; :gSSwSUUU525US2U
5; ;;r2QBB, .s: :a7:PBZ2,:BEaZPKgBZOgB, . ...:LEHri::,:.,,.,,,::rsXRBgGKEJi, 2GH252SUS5S11wH5
U: ;:rwDBBr .c, rB6r 167.,,RQPpEP6OpKEBc ., . : ::sS7;cKB6HHa1XOQRRQBRgEZDBH; .. ;RZUPSSU525USSpK2
s; ;::aBg, .; .Gv,H::,,::,;BQDOGHPXpKBs .,. . .,. .r:;:6gOEBQRRRDggQgOHgMGL, .;PEHHXaaKaPXPKPaXS
s7.:,s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.:LS6DMgMgZKgEOPOGGDDGEXULvLSOBBBBBQBgDQDJ5rrr7JOQQDDZGEgDD
wp rR5, .HQRX7r72RQBBBBBQBMEK6Uc7sc7cHOU:, .:LHgOQg6ZOPZpZGGEDERZPwcr7LKDDaJr:. rBX;;:r:ir1DBQMgRgDp
7D,sw: , Z. .LgBBQBBOsJaQBBBBBBXrr. .:rwZBBQgROgDgDOERRMgROOScrLsPP5r, 7Qs,::::::rUgRgZOG6
,Q57:r ,: ,r U: .;; .vL7L;r:UG7.:sr;JSgQBgDEZXXUSXpHa21svr7r7s2v:. iQK...:,:::;LLJJws
OBHs;. :;;,v H: :6X; rpL;7pSrrr7r;::,....,,:;iirrvvvr, :BBPs;;:,,,,::;::
::rPDEL:..7:c;7r pK .: :KL.:r:.. ..:icsS5sr:,. JgGgBQDOSJss77r
:r;::: , PH r, ,;5: ,::::;7Ls7Lc7;: ,:7JP17rJUs
.:J: :; .,:K6BQS7:.,.,.
:r7Ji;r7;::;;.
. **/ #include <bits/stdc++.h>
using namespace std; #define FFI(a, b) for(int i = a; i < b; i++)
#define RR(i, a, b) for(int i = a; i > b; i++)
#define ME(a, b) memset(a, b, sizeof(a))
#define SC(x) scanf("%d", &x)
#define PR(x) printf("%d\n", x)
#define INF 0x3f3f3f3f
#define MAX 1006
#define MOD 1000000007
#define E 2.71828182845
#define M 8
#define N 6
typedef long long LL;
const double PI = acos(-1.0);
typedef pair<int, int> Author;
vector<pair<string, int> > VP; int main(void){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false); cin.tie();
int n, num[MAX] = {};
cin>>n;
FFI(, n + ) cin>>num[i];
sort(num, num + n + );
if(n & ) cout<<num[n / + ]<<endl;
else cout<<num[n / ]<<endl;
return EXIT_SUCCESS;
}
1 second
256 megabytes
standard input
standard output
One day Alex decided to remember childhood when computers were not too powerful and lots of people played only default games. Alex enjoyed playing Minesweeper that time. He imagined that he saved world from bombs planted by terrorists, but he rarely won.
Alex has grown up since then, so he easily wins the most difficult levels. This quickly bored him, and he thought: what if the computer gave him invalid fields in the childhood and Alex could not win because of it?
He needs your help to check it.
A Minesweeper field is a rectangle n×mn×m, where each cell is either empty, or contains a digit from 11 to 88, or a bomb. The field is valid if for each cell:
- if there is a digit kk in the cell, then exactly kk neighboring cells have bombs.
- if the cell is empty, then all neighboring cells have no bombs.
Two cells are neighbors if they have a common side or a corner (i. e. a cell has at most 88 neighboring cells).
The first line contains two integers nn and mm (1≤n,m≤1001≤n,m≤100) — the sizes of the field.
The next nn lines contain the description of the field. Each line contains mm characters, each of them is "." (if this cell is empty), "*" (if there is bomb in this cell), or a digit from 11 to 88, inclusive.
Print "YES", if the field is valid and "NO" otherwise.
You can choose the case (lower or upper) for each letter arbitrarily.
3 3
111
1*1
111
YES
2 4
*.*.
1211
NO
In the second example the answer is "NO" because, if the positions of the bombs are preserved, the first line of the field should be *2*1.
You can read more about Minesweeper in Wikipedia's article.
分析:这题数太小,答案出错就是数字不对和*周围有.,所以暴力分析一遍即可
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define MAX 110
#define ME(a, b) memset(a, b, sizeof(a)) int dir[][] = {-,-,-,,-,,,-,,,,-,,,,}; //8个方向
char matrix[MAX][MAX];
int main(void){
ios::sync_with_stdio(false);cin.tie();
int i, j, k, n, m, temp; cin>>n>>m;
for(i = ; i <= n; i++)for(j = ; j <= m; j++)cin>>matrix[i][j];
for(i = ; i <= n; i++){
for(j = ; j <= m; j++){
if(matrix[i][j] == '*'){
for(k = ; k < ; k++){
int a = i + dir[k][];
int b = j + dir[k][];
if(matrix[a][b] == '.'){
cout<<"NO"<<endl;
return ;
}
}
}
else if(matrix[i][j] >= '' && matrix[i][j] <= ''){
temp = matrix[i][j] - '';
for(k = ; k < ; k++){
int a = i + dir[k][];
int b = j + dir[k][];
if(matrix[a][b] == '*') temp--;
}
if(temp != ){
cout<<"NO"<<endl;
return ;
}
}
}
}
cout<<"YES"<<endl; return EXIT_SUCCESS;
}
C. Finite or not?
1 second
256 megabytes
standard input
standard output
You are given several queries. Each query consists of three integers pp, qq and bb. You need to answer whether the result of p/qp/q in notation with base bb is a finite fraction.
A fraction in notation with base bb is finite if it contains finite number of numerals after the decimal point. It is also possible that a fraction has zero numerals after the decimal point.
The first line contains a single integer nn (1≤n≤1051≤n≤105) — the number of queries.
Next nn lines contain queries, one per line. Each line contains three integers pp, qq, and bb (0≤p≤10180≤p≤1018, 1≤q≤10181≤q≤1018, 2≤b≤10182≤b≤1018). All numbers are given in notation with base 1010.
For each question, in a separate line, print Finite if the fraction is finite and Infinite otherwise.
2
6 12 10
4 3 10
Finite
Infinite
4
1 1 2
9 36 2
4 12 3
3 5 4
Finite
Finite
Finite
Infinite
612=12=0,510612=12=0,510
43=1,(3)1043=1,(3)10
936=14=0,012936=14=0,012
412=13=0,13
分析:题意就是最简分数转成c进制看阔不阔以是有限小数,考查对分数的基本理解,先求a /b 的__gcd,再对b,和c一起__gcd即可
#include <bits/stdc++.h>
using namespace std;
#define LL long long int main(void){
LL n, a, b, c;
ios::sync_with_stdio(false);cin.tie(); cin>>n;
while(n--){
cin>>a>>b>>c;
b /= __gcd(a, b); //b转换成最简分母
c = __gcd(b, c);
while(c != ){ //可化成c进制的小数
b /= __gcd(b, c); //b转换成c进制
c = __gcd(b, c);
}
if(b == ) cout<<"Finite"<<endl;
else cout<<"Infinite"<<endl;
}
return EXIT_SUCCESS;
}
2 seconds
512 megabytes
standard input
standard output
For an array bb of length mm we define the function ff as
f(b)={b[1]if m=1f(b[1]⊕b[2],b[2]⊕b[3],…,b[m−1]⊕b[m])otherwise,f(b)={b[1]if m=1f(b[1]⊕b[2],b[2]⊕b[3],…,b[m−1]⊕b[m])otherwise,
where ⊕⊕ is bitwise exclusive OR.
For example, f(1,2,4,8)=f(1⊕2,2⊕4,4⊕8)=f(3,6,12)=f(3⊕6,6⊕12)=f(5,10)=f(5⊕10)=f(15)=15f(1,2,4,8)=f(1⊕2,2⊕4,4⊕8)=f(3,6,12)=f(3⊕6,6⊕12)=f(5,10)=f(5⊕10)=f(15)=15
You are given an array aa and a few queries. Each query is represented as two integers ll and rr. The answer is the maximum value of ff on all continuous subsegments of the array al,al+1,…,aral,al+1,…,ar.
The first line contains a single integer nn (1≤n≤50001≤n≤5000) — the length of aa.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤230−10≤ai≤230−1) — the elements of the array.
The third line contains a single integer qq (1≤q≤1000001≤q≤100000) — the number of queries.
Each of the next qq lines contains a query represented as two integers ll, rr (1≤l≤r≤n1≤l≤r≤n).
Print qq lines — the answers for the queries.
3
8 4 1
2
2 3
1 2
5
12
6
1 2 4 8 16 32
4
1 6
2 5
3 4
1 2
60
30
12
3
In first sample in both queries the maximum value of the function is reached on the subsegment that is equal to the whole segment.
In second sample, optimal segment for first query are [3,6][3,6], for second query — [2,5][2,5], for third — [3,4][3,4], for fourth — [1,2][1,2].
分析:前面3道考基础,后面2道考DP,这题算是斜三角DP吧(我自己取的名字),DP[i][j] = DP[i][j - 1] xor DP[i + 1][j],再求3者最小值即可
/** :;LaEaHKEEGpPXU7;,
.:75pKH11252U252XapZgRQgD6XJscLr;,.
:LXpRgGaX521JLw1JswJJsJs22XHPPEZEGDOMMRDOa7.
.r2EDDZEpZPZP6KpHX5SXH5XXa5KwaXaSX5UJ1c77sLs2GMQQ6r .
,LpgOGpEZGZEZEpZKpHHU5wP5HEDgpXpHa2SSa5aSXULr7rrirrJXRBp; ;B
,J6MRZH6EgEEZE6E6EZZPZXXwSSGQXr::aPpP5USUHaHaKa5Lvrr7ri;rLHBB2: Kc
rpQDOpPPOGGZOGOZG6GEOEOEDPPGBa. .PaSSUXSUUUaUSaKXKS177r7rrrirSBBR7 .O,
:UBQOKPK6ZOOOEDEO6GZE6EpEpDgDBR: UBpXHa5aSaUS5SUS5XapPHJc7rrv7rr7sgBBs .g.
;gBMPXpO6GEOEOEOEGEOEE6EZEEDRGBB EB5pKSXpHKaHSX552S5aUHHEX17c7vr7777s5RBS: .R;
.sQBPXpDZOODOgODGOGgEDEOEOGgGgOOMB: LBKKSXSHa52aaKXHXKaa5aSaaHXSJLcL7vcc777JDBBg2;. Qi
;2ggp2EDDOggGEDGgDDOgGGZDOOZOGg6gEBX vBZaHUKaaUXXXSXXKXpXHXH5wwaa52U1wssLsLJccv1UDQBQ67. O7
:ZZUU5PROOEOZOZGGOODZOGgODZOOgOggRgRB; ;:..R6XaKKpP6PGppKPHpHpPX5aU21UUa5Sw52UwUJJv77L77sSpQMDU; ;B5
,SRJ7sSHGggEOZOEG6OODpOZgggOQQBBBBBQBBQ.,;;. LBOgOgDRDDZODMgQRgRDaa552a252UUa25w5UaU2sLvccs7r7sJZBBMr ,XQJ:
LQHr77J6RGOZDZOEGEDGgDRORQBBBQRDPU1Jscwa7.,::.:J7r;::::. ..,:;i7UOgRRRgDPH5SUSU52U2HHa1JJJLJLccLr71RBB, 7R2,
:RZv;77JSgGOZEODEDGOEggQBBBMS7;:,:,,.,.,.:7L;:,;.: ,: . ..... .:;rJU6GgGRggEZHPaKXX2S221Js1Lc7r:7QB. .XX:
7g1;;7rcXG6gpGDZGgZOOQBBQpr. ::::;:;:::Jr::sr;::;:.:vs,:::::::::::,, ,:7L5HGOggRgZUUU5wSUaJLc7r7BOiDr ...
.XX;;;irLHGKpZZZEKgDRBBB6i ,;;:;;;;r;;:s177:,;L7:;7:.rHi,:::::::::::::::::,. .,;7ZRQgO6KUUJsLwsJ7KBM. .....
JZ:;rrrc5EPHp6XgpRBBBE; :i:;;;;;:;;;:c7::r7;,::::::::rv:,:::::::::::,:,:,:,,,, .;sORQRGX21wsXU: .... .
.Br;iir72EHPHZ6EgBBR7. ..;ii:;;;;;;;;;:71r::.:7, .::7;:;H;.::,:j::::::,:::::,,,::,,, ,7wEDRZBMr .......
1D;:r;rwOPXPKH6BBX, .:;:;;;:::;:;;;;;::Ls,;ss..r. ,c77;sLU,:,:::,:::,:,:,:,,,:,,,:::. .:.rP:.......
D2:i;rJpKHXHXgg7 .,,::::;:::::;:;:;;:;SL7sS2, :. ::::,:U7.:::,:::,:,,,,,:,,.,,,.,:, ;L:. .. ...
:Qc;i7LGZPa6gBM, ...,,::::::::::::;;::;.JJ;ic: ;:::,v1,.:::;7,,,:,:,:,,,,.:,,,,.. :2wr. .......
sRr:rrwZGgBQR7. .,,::::::::::::::;::;:::Hr:7i ,;;:::U: .,,,:r.,,:,:,:,,,:,,,..... 7K2:. ..........
OX:irsXgQZ:. .,,:::,,:::,::::::::;;;::r5;r7: :;;;:7L ..,.,;:,:,:,,,:,:,,,,...... .rU6w;..............
.BJ71EK5;. .,,::,:,:,,,:,:,::::::::;:;.Ls;r7 :;:::s, ..,,,r:.:.,,,.:, .,..... . .;s5XJr,..,.............
1Mv::. .:,:,,,,,,,:::::::,;:::;:;::..J7;c: ,. ,rri:27 , .,:;. ,:,:,,:..,.... .rpPL;:.. ... .............
..7Ls: .,,,:,,,:,:,:::,:,:::::,:::::: .Srrr, .,:;;;::::..:7r;r1 .: :E:..,,:,...,,.., ..XBQ7, ................... ..
,;7v7r7:, ,,,,:.,,,,,,:,,::,:,,.::.,:.,:, :Jrr; ,r7,:.. ,:::L: . .7RJ .,.:.,......,,:MBs ..............,........
.:;vJs7i, ,,,., ..,,:,,,:,,,:,:.,rs,,.:,, ;J;c, .,::;; Lr.E: .,...,....:,:1Z: .........,.,...........:,.
.;,,:r7J1wv;,. ....,.:. .,.,,:,,,:,,::::,sS;.:::, cLrr. . .,. .,..: ;; r5 ........,,:;s7. ....,.,.,...........,.,.,,.
,BBs::: . ........,.,,,,,.:,,,:,:,,.;s7r,,,,, cc7; ,.,,. .:7rrJGMPOEL1, ..... ,,:rSr, : ,.,.,.,.,.........,,,,,. ..
rZL. ....... ..,,,.,,,..,,,.,;,...7J:s: .. .wr7, ... .rJpQBBBBBBBQgKP77s .. .,7S2,,....,....,.,.,......,,.,. .:cX2
;SH7, . . . .. .,.,.,.,,,...,..r, ::Ur;7L . .57;. .rPBBQBBBPws:;r::::.,:P. .;S5;,..,...,.,.,.,.... ..,,, ..,7HSJvr
r7r;: . ... ..........,...,.. 7L;,rS;;ivr . 1r: . .rZBBK7;.JL,::Jrs;.:;,:;J, .,;LDv..:.,...,.,.,.,...,.,,:,, ,7sKGwc77;
.:7L7:, . ,r,..,.,,,...,..:iLL 7s;r;cv: U7. :vi;:. .. :Er:::.Ls. . ,7::::vHEi ,:,,.,,,.,.,,,,,,::::, .:1QBKJJUsssc
.:rvw1JsL7;, ,;..........,.:ir :r .ELr77v:,. Pr .. . Ls ,w. rr.r:JPr. ::,..,,,.,,,,:,:::,:,..7XgRE52US25w1c
.:;7JRQpX: ; .........,i: .,J ;gri;7r : 1; .X: . ,v :aK1, ,,:.,,,.,,,,:::::,;::::cOBB6K55UXUX5XUw
;J,XQB7,: . ...,: .:7Z.7Zi7;r: , v, :w:;,.,:::7LUsc:..:,:.:,,,,,:,:::::::,:;c5BBPS5wSUSUaUaUa2
J7 rEBBg..,. ..... . ,L1PLKr:,:;r;;::, :, :;SXsJU1XLLr....:,,,,,,::::,::::::,::LL,,rZXUJU25552aUUUU
JL .:sXBB:... ., . ,OH777;,rZRBBBQL ..r, ,:r7::r:,;;....:,,,,,,,,.::::;::;:::.7gL UK2UUS1aw2wU25J
U7 .r71BB; . :.. .. :QJ;.;1pBBBBBGRBRi i: . .w7. ..,,:,,,,,:,:,:::::::,.,2BR: .DSaUSUUS525w5U1
2r :7rJBQ7 .;. . KL::JGBBBgE6Hp6XMQ; . ... ,2s ,,,,:::::,:::::::,,.:sQQB2 ;DHa5U52S2U25USJ
a: :7;1BB2 .v, sL;LPQgDBPH6KPQBpGBG . ... .. aP:. .. ,::,:::::,,,,.;rSgBQDa; :gSSwSUUU525US2U
5; ;;r2QBB, .s: :a7:PBZ2,:BEaZPKgBZOgB, . ...:LEHri::,:.,,.,,,::rsXRBgGKEJi, 2GH252SUS5S11wH5
U: ;:rwDBBr .c, rB6r 167.,,RQPpEP6OpKEBc ., . : ::sS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6OZgDgGDGD
7QB; ,Jc76DaZXOZgDPEBBX, :c1HEZ1c;:,.,:;cHDgQQggMZgGOZO6OEGpXsLLsJXHPOBBBc;vss5EMggZDODOgED6
LZJ::iwrrr72EPgXU5OBBBBBBBp7: .i5P6wL;, .:LS6DMgMgZKgEOPOGGDDGEXULvLSOBBBBBQBgDQDJ5rrr7JOQQDDZGEgDD
wp rR5, .HQRX7r72RQBBBBBQBMEK6Uc7sc7cHOU:, .:LHgOQg6ZOPZpZGGEDERZPwcr7LKDDaJr:. rBX;;:r:ir1DBQMgRgDp
7D,sw: , Z. .LgBBQBBOsJaQBBBBBBXrr. .:rwZBBQgROgDgDOERRMgROOScrLsPP5r, 7Qs,::::::rUgRgZOG6
,Q57:r ,: ,r U: .;; .vL7L;r:UG7.:sr;JSgQBgDEZXXUSXpHa21svr7r7s2v:. iQK...:,:::;LLJJws
OBHs;. :;;,v H: :6X; rpL;7pSrrr7r;::,....,,:;iirrvvvr, :BBPs;;:,,,,::;::
::rPDEL:..7:c;7r pK .: :KL.:r:.. ..:icsS5sr:,. JgGgBQDOSJss77r
:r;::: , PH r, ,;5: ,::::;7Ls7Lc7;: ,:7JP17rJUs
.:J: :; .,:K6BQS7:.,.,.
:r7Ji;r7;::;;.
. **/ #include <bits/stdc++.h>
using namespace std; #define FFI(a, b) for(int i = a; i < b; i++)
#define FFJ(a, b) for(int j = a; j < b; j++)
#define RR(a, b) for(int i = a; i > b; i++)
#define ME(a, b) memset(a, b, sizeof(a))
#define SC(x) scanf("%d", &x)
#define PR(x) printf("%d\n", x)
#define INF 0x3f3f3f3f
#define MAX 5005
#define MOD 1000000007
#define E 2.71828182845
#define M 8
#define N 6
typedef long long LL;
const double PI = acos(-1.0);
typedef pair<int, int> Author;
vector<pair<string, int> > VP; LL num[MAX];
LL dp[MAX][MAX]; inline int max(int a, int b){return a > b ? a : b;}
int main(void){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false); cin.tie();
int i, j, n, q, l, r, ma; cin>>n;
FFI(, n){
cin>>num[i];
dp[i][i] = num[i];
}
FFI(, n) FFJ(, n - i) dp[j][j + i] = dp[j][j + i - ] xor dp[j + ][j + i]; //斜三角
FFI(, n) FFJ(, n - i) dp[j][j + i] = max(dp[j][j + i], max(dp[j][j + i - ], dp[j + ][j + i])); cin>>q;
while(q--){
cin>>l>>r;
l--;r--;
cout<<dp[l][r]<<endl;
} return EXIT_SUCCESS;
}
3 seconds
256 megabytes
standard input
standard output
You work in a big office. It is a 9 floor building with an elevator that can accommodate up to 4 people. It is your responsibility to manage this elevator.
Today you are late, so there are queues on some floors already. For each person you know the floor where he currently is and the floor he wants to reach. Also, you know the order in which people came to the elevator.
According to the company's rules, if an employee comes to the elevator earlier than another one, he has to enter the elevator earlier too (even if these employees stay on different floors). Note that the employees are allowed to leave the elevator in arbitrary order.
The elevator has two commands:
- Go up or down one floor. The movement takes 1 second.
- Open the doors on the current floor. During this operation all the employees who have reached their destination get out of the elevator. Then all the employees on the floor get in the elevator in the order they are queued up while it doesn't contradict the company's rules and there is enough space in the elevator. Each employee spends 1 second to get inside and outside the elevator.
Initially the elevator is empty and is located on the floor 1.
You are interested what is the minimum possible time you need to spend to deliver all the employees to their destination. It is not necessary to return the elevator to the floor 1.
The first line contains an integer n (1 ≤ n ≤ 2000) — the number of employees.
The i-th of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 9, ai ≠ bi) — the floor on which an employee initially is, and the floor he wants to reach.
The employees are given in the order they came to the elevator.
Print a single integer — the minimal possible time in seconds.
2
3 5
5 3
10
2
5 3
3 5
12
分析:这题最JB有意思,DP[a][b][c][d][e][f]装小时间,a,b,c,d是几层,f是向上走,还是想下走,很实用啊
/** :;LaEaHKEEGpPXU7;,
.:75pKH11252U252XapZgRQgD6XJscLr;,.
:LXpRgGaX521JLw1JswJJsJs22XHPPEZEGDOMMRDOa7.
.r2EDDZEpZPZP6KpHX5SXH5XXa5KwaXaSX5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RBBB6i ,;;:;;;;r;;:s177:,;L7:;7:.rHi,:::::::::::::::::,. .,;7ZRQgO6KUUJsLwsJ7KBM. .....
JZ:;rrrc5EPHp6XgpRBBBE; :i:;;;;;:;;;:c7::r7;,::::::::rv:,:::::::::::,:,:,:,,,, .;sORQRGX21wsXU: .... .
.Br;iir72EHPHZ6EgBBR7. ..;ii:;;;;;;;;;:71r::.:7, .::7;:;H;.::,:j::::::,:::::,,,::,,, ,7wEDRZBMr .......
1D;:r;rwOPXPKH6BBX, .:;:;;;:::;:;;;;;::Ls,;ss..r. ,c77;sLU,:,:::,:::,:,:,:,,,:,,,:::. .:.rP:.......
D2:i;rJpKHXHXgg7 .,,::::;:::::;:;:;;:;SL7sS2, :. ::::,:U7.:::,:::,:,,,,,:,,.,,,.,:, ;L:. .. ...
:Qc;i7LGZPa6gBM, ...,,::::::::::::;;::;.JJ;ic: ;:::,v1,.:::;7,,,:,:,:,,,,.:,,,,.. :2wr. .......
sRr:rrwZGgBQR7. .,,::::::::::::::;::;:::Hr:7i ,;;:::U: .,,,:r.,,:,:,:,,,:,,,..... 7K2:. ..........
OX:irsXgQZ:. .,,:::,,:::,::::::::;;;::r5;r7: :;;;:7L ..,.,;:,:,:,,,:,:,,,,...... .rU6w;..............
.BJ71EK5;. .,,::,:,:,,,:,:,::::::::;:;.Ls;r7 :;:::s, ..,,,r:.:.,,,.:, .,..... . .;s5XJr,..,.............
1Mv::. .:,:,,,,,,,:::::::,;:::;:;::..J7;c: ,. ,rri:27 , .,:;. ,:,:,,:..,.... .rpPL;:.. ... .............
..7Ls: .,,,:,,,:,:,:::,:,:::::,:::::: .Srrr, .,:;;;::::..:7r;r1 .: :E:..,,:,...,,.., ..XBQ7, ................... ..
,;7v7r7:, ,,,,:.,,,,,,:,,::,:,,.::.,:.,:, :Jrr; ,r7,:.. ,:::L: . .7RJ .,.:.,......,,:MBs ..............,........
.:;vJs7i, ,,,., ..,,:,,,:,,,:,:.,rs,,.:,, ;J;c, .,::;; Lr.E: .,...,....:,:1Z: .........,.,...........:,.
.;,,:r7J1wv;,. ....,.:. .,.,,:,,,:,,::::,sS;.:::, cLrr. . .,. .,..: ;; r5 ........,,:;s7. ....,.,.,...........,.,.,,.
,BBs::: . ........,.,,,,,.:,,,:,:,,.;s7r,,,,, cc7; ,.,,. .:7rrJGMPOEL1, ..... ,,:rSr, : ,.,.,.,.,.........,,,,,. ..
rZL. ....... ..,,,.,,,..,,,.,;,...7J:s: .. .wr7, ... .rJpQBBBBBBBQgKP77s .. .,7S2,,....,....,.,.,......,,.,. .:cX2
;SH7, . . . .. .,.,.,.,,,...,..r, ::Ur;7L . .57;. .rPBBQBBBPws:;r::::.,:P. .;S5;,..,...,.,.,.,.... ..,,, ..,7HSJvr
r7r;: . ... ..........,...,.. 7L;,rS;;ivr . 1r: . .rZBBK7;.JL,::Jrs;.:;,:;J, .,;LDv..:.,...,.,.,.,...,.,,:,, ,7sKGwc77;
.:7L7:, . ,r,..,.,,,...,..:iLL 7s;r;cv: U7. :vi;:. .. :Er:::.Ls. . ,7::::vHEi ,:,,.,,,.,.,,,,,,::::, .:1QBKJJUsssc
.:rvw1JsL7;, ,;..........,.:ir :r .ELr77v:,. Pr .. . Ls ,w. rr.r:JPr. ::,..,,,.,,,,:,:::,:,..7XgRE52US25w1c
.:;7JRQpX: ; .........,i: .,J ;gri;7r : 1; .X: . ,v :aK1, ,,:.,,,.,,,,:::::,;::::cOBB6K55UXUX5XUw
;J,XQB7,: . ...,: .:7Z.7Zi7;r: , v, :w:;,.,:::7LUsc:..:,:.:,,,,,:,:::::::,:;c5BBPS5wSUSUaUaUa2
J7 rEBBg..,. ..... . ,L1PLKr:,:;r;;::, :, :;SXsJU1XLLr....:,,,,,,::::,::::::,::LL,,rZXUJU25552aUUUU
JL .:sXBB:... ., . ,OH777;,rZRBBBQL ..r, ,:r7::r:,;;....:,,,,,,,,.::::;::;:::.7gL UK2UUS1aw2wU25J
U7 .r71BB; . :.. .. :QJ;.;1pBBBBBGRBRi i: . .w7. ..,,:,,,,,:,:,:::::::,.,2BR: .DSaUSUUS525w5U1
2r :7rJBQ7 .;. . KL::JGBBBgE6Hp6XMQ; . ... ,2s ,,,,:::::,:::::::,,.:sQQB2 ;DHa5U52S2U25USJ
a: :7;1BB2 .v, sL;LPQgDBPH6KPQBpGBG . ... .. aP:. .. ,::,:::::,,,,.;rSgBQDa; :gSSwSUUU525US2U
5; ;;r2QBB, .s: :a7:PBZ2,:BEaZPKgBZOgB, . ...:LEHri::,:.,,.,,,::rsXRBgGKEJi, 2GH252SUS5S11wH5
U: ;:rwDBBr .c, rB6r 167.,,RQPpEP6OpKEBc ., . : ::sS7;cKB6HHa1XOQRRQBRgEZDBH; .. ;RZUPSSU525USSpK2
s; ;::aBg, .; .Gv,H::,,::,;BQDOGHPXpKBs .,. . .,. .r:;:6gOEBQRRRDggQgOHgMGL, .;PEHHXaaKaPXPKPaXS
s7.:,sBa :..S, vE::::; 7BBMgZH6pQBr . ,:, ,.,r2RHSSpMZPKRRZpgggav, ;UpEZaaaKHHUHPPaUJHGO
a;.775: .::rU. gR:,:. :;:BBBBQar . ,, ,.,7rggJwU6DDGMgOGgXc:, ,rPGOpKSXSXSKUKHU1UHgOEP
1i :SZJrLXpBBMRB: .v Ba,. :sv: . irsBUSpEEGPPpg65r:..,;sKZDZHSX5XaHU55K2wUGMRZ6Z6
:K :7asvwc2MgEQB, :gg.:iB7 .,,,:. ...,. . .. .:iM6GEpSSXZOEs:...;spZpKKXXSX5Xa5Ja5wwKGQDEpp6OZ
w; ,LXLr77sLwL5aR. gRgQgaQBi ,vJwvi,. .;;:;7: . . ,... rRDRJUSXpgas:,.:;U66SaHa2X5aUa552X12SgQgpZpEpGOD
H: ;XJ;77rv7rJUaQ;rgaPgXXHBB; . . .ri:;; ,: . :6QZEKHXpXXL:,::r1PKXaa2XSS5a5S5UJ22aOBgEpOppZOOgG
iwsJsr7rsLrrLcJ2gQGXHwa1SUHBB: . ... . .., : 1BRKpOEGp2Jr;::rsXP1Uaa2U5a5aUa5SsSpgQRZGZEEZZOOgDg
,cs5aXaP552ssLwRSHXaSS5asXBB. . . . LBBgGgZHsr;vrr;rcXpP5UUS5X5S2XS5SS1URBggpEZgDDGOGggRD
,;irrs2KgQRPJJJSUXKXSHJpBM . . . ZBBOUsr;:. ,,,rHgZPUaSaUSUSSXaUPXsSBB6ZZp6EEGOOGDEggO
;BBQQZaSJ5J15SJDQ7 .iEBL:. . .,:,:;SRQGZXPXXSHaS2UUSUSswQB6OZgG6pZZGODEOGDE
JgUri1aGEpEpXSS5LBQ, .rLap2Jr ;712OO6ZggRDZXHXpKK2211LLLc7wgBa2PODRGE6OZgDgGDGD
7QB; ,Jc76DaZXOZgDPEBBX, :c1HEZ1c;:,.,:;cHDgQQggMZgGOZO6OEGpXsLLsJXHPOBBBc;vss5EMggZDODOgED6
LZJ::iwrrr72EPgXU5OBBBBBBBp7: .i5P6wL;, .:LS6DMgMgZKgEOPOGGDDGEXULvLSOBBBBBQBgDQDJ5rrr7JOQQDDZGEgDD
wp rR5, .HQRX7r72RQBBBBBQBMEK6Uc7sc7cHOU:, .:LHgOQg6ZOPZpZGGEDERZPwcr7LKDDaJr:. rBX;;:r:ir1DBQMgRgDp
7D,sw: , Z. .LgBBQBBOsJaQBBBBBBXrr. .:rwZBBQgROgDgDOERRMgROOScrLsPP5r, 7Qs,::::::rUgRgZOG6
,Q57:r ,: ,r U: .;; .vL7L;r:UG7.:sr;JSgQBgDEZXXUSXpHa21svr7r7s2v:. iQK...:,:::;LLJJws
OBHs;. :;;,v H: :6X; rpL;7pSrrr7r;::,....,,:;iirrvvvr, :BBPs;;:,,,,::;::
::rPDEL:..7:c;7r pK .: :KL.:r:.. ..:icsS5sr:,. JgGgBQDOSJss77r
:r;::: , PH r, ,;5: ,::::;7Ls7Lc7;: ,:7JP17rJUs
.:J: :; .,:K6BQS7:.,.,.
:r7Ji;r7;::;;.
. **/ #include <bits/stdc++.h>
using namespace std; #define FFI(a, b) for(int i = a; i < b; i++)
#define FFJ(a, b) for(int j = a; j < b; j++)
#define RR(a, b) for(int i = a; i > b; i++)
#define ME(a, b) memset(a, b, sizeof(a))
#define SC(x) scanf("%d", &x)
#define PR(x) printf("%d\n", x)
#define INF 0x3f3f3f3f
#define MAX 1001
#define MOD 1000000007
#define E 2.71828182845
#define M 8
#define N 6
typedef long long LL;
const double PI = acos(-1.0);
typedef pair<int, int> Author;
vector<pair<string, int> > VP; int n;
int a[],b[];
int dp[][][][][][]; //n个点到4个方向的上下值
int Solve(int ind,int x1, int x2, int x3, int x4, int y){
if(dp[ind][x1][x2][x3][x4][y] != -) return dp[ind][x1][x2][x3][x4][y];
int ans=, c1 = ind, c2 = x4, cur;
if(y==){
cur=a[ind];
x4=b[ind];
ind++;
}
else{
cur=x4;
x4=;
}
if(ind>n && x1== && x2== && x3== && x4==)
return ;
if(x1!=)
ans=min(ans,Solve(ind,x2,x3,x4,x1,)+max(cur-x1,x1-cur)+);
if(x2!=)
ans=min(ans,Solve(ind,x1,x3,x4,x2,)+max(cur-x2,x2-cur)+);
if(x3!=)
ans=min(ans,Solve(ind,x1,x2,x4,x3,)+max(cur-x3,x3-cur)+);
if(x4!=)
ans=min(ans,Solve(ind,x1,x2,x3,x4,)+max(cur-x4,x4-cur)+);
if(x1== && ind<=n)
ans=min(ans,Solve(ind,x2,x3,x4,x1,)+max(cur-a[ind],a[ind]-cur)+);
else if(x2== && ind<=n)
ans=min(ans,Solve(ind,x1,x3,x4,x2,)+max(cur-a[ind],a[ind]-cur)+);
else if(x3== && ind<=n)
ans=min(ans,Solve(ind,x1,x2,x4,x3,)+max(cur-a[ind],a[ind]-cur)+);
else if(x4== && ind<=n)
ans=min(ans,Solve(ind,x1,x2,x3,x4,)+max(cur-a[ind],a[ind]-cur)+);
return dp[c1][x1][x2][x3][c2][y]=ans;
}
int main(void){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false); cin.tie(); cin>> n;
for(int i = ;i <= n + ; i++){
if(i <= n) cin>>a[i]>>b[i];
for(int j = ; j <= ; j++)
for(int k = ; k <= ; k++)
for(int x = ; x <= ; x++)
for(int y = ; y <= ; y++)
for(int z = ; z <= ; z++)
dp[i][j][k][x][y][z] = -;
}
cout<<Solve(, , , , , ) + a[]<<endl;
return EXIT_SUCCESS;
}
Codeforces Round #483 (Div. 2)题解的更多相关文章
- Codeforces Round #182 (Div. 1)题解【ABCD】
Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...
- Codeforces Round #608 (Div. 2) 题解
目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 ...
- Codeforces Round #525 (Div. 2)题解
Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...
- Codeforces Round #528 (Div. 2)题解
Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...
- Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F
Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...
- Codeforces Round #677 (Div. 3) 题解
Codeforces Round #677 (Div. 3) 题解 A. Boring Apartments 题目 题解 简单签到题,直接数,小于这个数的\(+10\). 代码 #include &l ...
- Codeforces Round #665 (Div. 2) 题解
Codeforces Round #665 (Div. 2) 题解 写得有点晚了,估计都官方题解看完切掉了,没人看我的了qaq. 目录 Codeforces Round #665 (Div. 2) 题 ...
- Codeforces Round #160 (Div. 1) 题解【ABCD】
Codeforces Round #160 (Div. 1) A - Maxim and Discounts 题意 给你n个折扣,m个物品,每个折扣都可以使用无限次,每次你使用第i个折扣的时候,你必须 ...
- Codeforces Round #383 (Div. 2) 题解【ABCDE】
Codeforces Round #383 (Div. 2) A. Arpa's hard exam and Mehrdad's naive cheat 题意 求1378^n mod 10 题解 直接 ...
随机推荐
- 读书笔记_Effective C++_条款一:将C++视为一个语言联邦
C++起源于C,最初的名称为C with Classes,意为带类的C语言,然而,随着C++的不断发展和壮大,在很多功能上已经远远超越了C,甚至一些C++程序员反过来看C代码会觉得不习惯. C++可以 ...
- Container(容器)与 Injector(注入)
Container(容器): History: containerd于2014年出生于Docker,最初是Docker引擎的低层运行时管理器.继2017年3月被CNCF接受之后,containerd已 ...
- Bukkit编程之动态向yml文件中添加属性
yaml = new Yaml(); String goods = args[0]; String goodsNum = args[1]; YamlConfiguration yc = new Yam ...
- JavaScript 深入了解对象中的属性
本篇主要介绍JS中对象的属性,包括:属性的分类.访问方式.检测属性.遍历属性以及属性特性等内容. 目录 1. 介绍:描述属性的命名方式.查找路径以及分类 2. 属性的访问方式:介绍'.'访问方式.'[ ...
- [11]Windows内核情景分析---设备驱动
设备驱动 设备栈:从上层到下层的顺序依次是:过滤设备.类设备.过滤设备.小端口设备[过.类.过滤.小端口] 驱动栈:因设备堆栈原因而建立起来的一种堆栈 老式驱动:指不提供AddDevice的驱动,又叫 ...
- PM2管理工具的使用
linux上PM2可以管理服务程序,防止程序无故关闭,具有程序守护功能,自动重启服务器程序,监控程序等好处,很方便,具体自己去体会! 官网地址: http://pm2.keymetrics.io/ ...
- django 1.9.7 css控制模板样式
问题:css样式不能控制html样式(针对开发环境,不针对生产环境) 现象: django.template.exceptions.TemplateSyntaxError: Invalid block ...
- Vue系列之 => 使用钩子函数的第二个参数传参
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Spring boot FastJson
介绍:FastJson 是ailibaba 的一款解析Json的开源框架 使用方式1 引入jar 包 <dependency> <groupId>com.alibaba& ...
- java.security.NoSuchAlgorithmException: AES KeyGenerator not available
异常信息 Caused by: Java.lang.IllegalStateException: Unable to acquire AES algorithm. This is required t ...