ACM-ICPC Asia Training League 暑假第一阶段第二场 BEGIJ
B Button Bashing
You recently acquired a new microwave, and noticed that it provides a large number of buttons to be able to quickly specify the time that the microwave should be running for. There are buttons both for adding time, and for subtracting time. You wonder how efficient you can be when entering cooking times: you want to minimize the number of required button presses.
The microwave can be running for at least 00 seconds, and at most 11 hour. If a button press would result in a cooking time of less than 00 seconds, the microwave will set the cooking time to 00 seconds. If a button press would result in a cooking time of more than 11 hour, the microwave will set the cooking time to 11 hour. Initially, the microwave will run for 00 seconds. There will always be a button adding at least 11 second to the cooking time.
Given the buttons that the microwave provides for entering cooking times, determine the least amount of button presses required to let the microwave run for a certain amount of time. If it is not possible to enter the desired cooking time precisely, determine the smallest achievable cooking time above the target, and the minimum number of button presses required for that cooking time, instead. The microwave does not allow to adjust the cooking time once it has started cooking.
Input Format
On the first line one positive number: the number of test cases, at most 100100. After that per test case:
- one line with two space-separated integers nn and tt (1 \le n \le 16, 0 \le t \le 3600)(1≤n≤16,0≤t≤3600): the number of buttons available to change the cooking time, and the desired cooking time in seconds, respectively.
- one line with nn space-separated integers b_ibi (-3600 \le b_i \le 3600)(−3600≤bi≤3600): the number of seconds added to the cooking time when button ii is pressed.
Output Format
Per test case:
- one line with two space-separated integers: the minimum number of button presses required to reach the required cooking time, and the minimum number of extra seconds that the microwave must be running for, respectively.
样例输入
2 3 50 -10 10 60 1 50 20
样例输出
2 0 3 10
n个按钮,时间为t。每个按钮能加或减时间,求时间为t最少按几次按钮,如果不能到t,那么大于t的最解决t的多少。
1 #include <bits/stdc++.h> 2 #define INF 0x3f3f3f3f 3 using namespace std; 4 int T, n, t; 5 int a[20], dp[4000]; 6 typedef pair<int,int> P; 7 int main() { 8 scanf("%d", &T); 9 while(T--) { 10 scanf("%d%d", &n, &t); 11 for(int i = 0; i < n; i ++) cin >> a[i]; 12 memset(dp, INF, sizeof(dp)); 13 queue<P> que; 14 que.push(P(0,0)); 15 dp[0] = 0; 16 int MIN = 1e5, Time = 1e5; 17 while(que.size()) { 18 P p = que.front(); que.pop(); 19 if(p.second >= t) { 20 if(p.second < Time) { 21 Time = p.second; 22 MIN = p.first; 23 } else if(p.second == Time) { 24 MIN = min(MIN, p.first ); 25 } 26 } 27 for(int i = 0; i < n; i ++) { 28 int tt = p.second + a[i]; 29 if(tt < 0) tt = 0; 30 if(tt > 3600) tt = 3600; 31 if(dp[tt] > p.first+1) { 32 dp[tt] = p.first + 1; 33 que.push(P(p.first+1,tt)); 34 } 35 } 36 } 37 cout << MIN << ' ' << Time -t<< endl; 38 } 39 return 0; 40 }
E. Excellent Engineers
You are working for an agency that selects the best software engineers from Belgium, the Netherlands and Luxembourg for employment at various international companies. Given the very large number of excellent software engineers these countries have to offer, the agency has charged you to develop a tool that quickly selects the best candidates available from the agency's files.
Before a software engineer is included in the agency's files, he has to undergo extensive testing. Based on these tests, all software engineers are ranked on three essential skills: communication skills, programming skills, and algorithmic knowledge. The software engineer with rank one in the category algorithmic knowledge is the best algorithmic expert in the files, with rank two the second best, etcetera.
For potential customers, your tool needs to process the agency's files and produce a shortlist of the potentially most interesting candidates. A software engineer is a potential candidate that is to be put on this shortlist if there is no other software engineer in the files that scores better on all three skills. That is, an engineer is to be put on the shortlist if there is no other software engineer that has better communication skills, better programming skills, and more algorithmic knowledge.
Input Format
On the first line one positive number: the number of test cases, at most 100100. After that per test case:
- one line with a single integer nn (1 \le n \le 10^5)(1≤n≤105): the number of software engineers in the agency's files.
- nn lines, each with three space-separated integers r_1r1, r_2r2 and r_3r3 (1 \le r_1, r_2, r_3 \le n)(1≤r1,r2,r3≤n): the rank of each software engineer from the files with respect to his communication skills, programming skills and algorithmic knowledge, respectively.
For each skill ss and each rank xx between 11 and nn, there is exactly one engineer with r_s = xrs=x.
Output Format
Per test case:
- one line with a single integer: the number of candidates on the shortlist.
样例输入
3 3 2 3 2 3 2 3 1 1 1 3 1 2 3 2 3 1 3 1 2 10 1 7 10 3 9 7 2 2 9 5 10 8 4 3 5 7 5 2 6 1 3 9 6 6 8 4 4 10 8 1
样例输出
1 3 7
n个人,三个排名,对于任意一个人,然后存在一个三门排名都比他好的话这个人就不选,问最终有几个人选了。先按r1从小到大排序,然后集合按r2从小到大排序,遍历n个人,每次到集合里查找。
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int N = 1e5+10; 5 struct engineer{ 6 int r1, r2, r3; 7 engineer(){} 8 engineer(int r1, int r2, int r3) :r1(r1), r2(r2), r3(r3){} 9 }; 10 11 struct comparefirst { 12 bool operator() (engineer A, engineer B) { 13 return A.r1 < B.r1; 14 } 15 }cmp1; 16 17 struct comparesecond { 18 bool operator() (engineer A, engineer B) { 19 return A.r2 < B.r2; 20 } 21 }; 22 23 engineer E[N]; 24 set<engineer, comparesecond> S; 25 26 int main() { 27 set<engineer> :: iterator it, itprev, itnext; 28 int t, n, r1, r2, r3; 29 cin >> t; 30 while(t--) { 31 cin >> n; 32 for(int i = 0; i < n; i ++) { 33 cin >> r1 >> r2 >> r3; 34 E[i] = engineer(r1,r2,r3); 35 } 36 sort(E, E+n, cmp1); 37 int ans = 1; 38 S.clear(); 39 S.insert(E[0]); 40 for(int i = 1; i < n; i ++) { 41 itnext = S.lower_bound(E[i]); 42 itprev = itnext; 43 itprev--; 44 if(itnext == S.begin() || E[i].r3 < itprev->r3) { 45 ans++; 46 for(it = itnext; it != S.end() && E[i].r3 < it->r3;) 47 S.erase(it++); 48 S.insert(E[i]); 49 } 50 } 51 cout << ans << endl; 52 } 53 return 0; 54 }
G. Growling Gears
The Best Acceleration Production Company specializes in multi-gear engines. The performance of an engine in a certain gear, measured in the amount of torque produced, is not constant: the amount of torque depends on the RPM of the engine. This relationship can be described using a torque-RPM curve.
For the latest line of engines, the torque-RPM curve of all gears in the engine is a parabola of the form T = -aR^2 +bR+cT=−aR2+bR+c,where RR is the RPM of the engine,and TT is the resulting torque.
Given the parabolas describing all gears in an engine, determine the gear in which the highest torque is produced. The first gear is gear 11, the second gear is gear 22, etc. There will be only one gear that produces the highest torque: all test cases are such that the maximum torque is at least 11 higher than the maximum torque in all the other gears.
Input Format
On the first line one positive number: the number of test cases, at most 100100. After that per test case:
- one line with a single integer nn (1 \le n \le 10)(1≤n≤10): the number of gears in the engine.
- nn lines, each with three space-separated integers aa, bb and cc (1 \le a, b, c \le 10^4)(1≤a,b,c≤104): the parameters of the parabola T = -aR^2 +bR+cT=−aR2+bR+c describing the torque-RPM curve of each engine.
Output Format
Per test case:
- one line with a single integer: the gear in which the maximum torque is generated.
样例输入
3 1 1 4 2 2 3 126 1400 2 152 208 2 3 127 1400 2 154 208
样例输出
1 2 2
求抛物线的最高点。
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int N = 1010; 4 int x[N], y[N]; 5 double aa[11]; 6 int main() { 7 int t, n, a, b,c; 8 scanf("%d", &t); 9 while(t--) { 10 cin >> n; 11 for(int i = 1; i <= n; i ++) { 12 cin >> a >> b >> c; 13 aa[i] = (-4.0*a*c-b*b)/(-4.0*a); 14 } 15 double MAX = aa[1]; 16 int index = 1; 17 for(int i = 2; i <= n; i ++) { 18 if(MAX < aa[i]) { 19 MAX = aa[i]; 20 index = i; 21 } 22 } 23 cout << index << endl; 24 } 25 return 0; 26 }
I. Interesting Integers
Undoubtedly you know of the Fibonacci numbers. Starting with F_1 = 1F1=1 and F_2 = 1F2=1,every next number is the sum of the two previous ones. This results in the sequence 1, 1, 2, 3, 5, 8, 13, \cdot\cdot\cdot1,1,2,3,5,8,13,⋅⋅⋅ .
Now let us consider more generally sequences that obey the same recursion relation
Gi =G_{i-1} +G_{i-2}Gi=Gi−1+Gi−2 for i>2i>2
but start with two numbers G_1 \le G_2G1≤G2 of our own choice. We shall call these Gabonacci sequences. For example, if one uses G_1 = 1G1=1 and G_2 = 3G2=3, one gets what are known as the Lucas numbers: 1,3,4,7,11,18,29,\cdot\cdot\cdot1,3,4,7,11,18,29,⋅⋅⋅ . These numbers are – apart from 11 and 33 – different from the Fibonacci numbers.
By choosing the first two numbers appropriately, you can get any number you like to appear in the Gabonacci sequence. For example, the number n appears in the sequence that starts with 11 and n - 1n−1, but that is a bit lame. It would be more fun to start with numbers that are as small as possible, would you not agree?
Input Format
On the first line one positive number: the number of test cases, at most 100100. After that per test case:
- one line with a single integer nn (2 \le n \le 10^9)(2≤n≤109): the number to appear in the sequence.
Output Format
Per test case:
- one line with two integers aa and bb (0 < a \le b)(0<a≤b),such that,for G_1 = aG1=a and G_2 = b, G_k = nG2=b,Gk=n for some kk. These numbers should be the smallest possible, i.e., there should be no numbers a{'}a′ and b{'}b′ with the same property, for which b{'} < bb′<b, or for which b{'} = bb′=b and a{'} < aa′<a.
样例输入
5 89 123 1000 1573655 842831057
样例输出
1 1 1 3 2 10 985 1971 2 7
给定一个数n,是否存在G1,G2是的有Gm = n G(n) = G(n-1) + G(n-2)。对与G的每一项,可以看成由多少个G1和G2组成,然后就是求ax+by=n中a 和 b的值了。
1 #include <bits/stdc++.h> 2 #define ll long long 3 #define INF 0x3f3f3f3f 4 using namespace std; 5 pair<ll,ll> p[50]; 6 ll f[50]; 7 ll extgcd(ll a, ll b, ll &x, ll &y){ 8 ll d = a; 9 if(b != 0){ 10 d = extgcd(b,a%b,y,x); 11 y -= (a/b)*x; 12 }else{ 13 x = 1; y = 0; 14 } 15 return d; 16 } 17 int main() { 18 int t, n; 19 f[1] = f[2] = 1; 20 for(int i = 3; i < 50; i ++) f[i] = f[i-1] + f[i-2]; 21 p[1].first = 1, p[1].second = 0; 22 for(int i = 2; i <= 50; i ++) { 23 p[i].first = f[i-2], p[i].second = f[i-1]; 24 } 25 cin >> t; 26 while(t--) { 27 cin >> n; 28 ll a = 1e13, b = 1e13; 29 ll aa, bb; 30 for(int i = 49; i >= 3; i --) { 31 ll x = p[i].first, y = p[i].second; 32 if(x + y > n) continue; 33 if(n%__gcd(x,y) != 0) continue; 34 extgcd(x,y,aa,bb); 35 aa *= n; 36 bb *= n; 37 ll cnt = (bb-aa)/(x+y); 38 aa += cnt*y; 39 bb -= cnt*x; 40 if(aa > bb) { 41 aa -= y; 42 bb += x; 43 } 44 if(aa > bb || aa < 1) continue; 45 if(bb < b) { 46 b = bb; 47 a = aa; 48 } else if(bb == b) { 49 a = aa; 50 } 51 } 52 cout << a << ' ' << b << endl; 53 } 54 }
J. Jury Jeopardy
What would a programming contest be without a problem featuring an ASCII-maze? Do not despair: one of the judges has designed such a problem.
The problem is about a maze that has exactly one entrance/exit, contains no cycles and has no empty space that is completely enclosed by walls. A robot is sent in to explore the entire maze. The robot always faces the direction it travels in. At every step, the robot will try to turn right. If there is a wall there, it will attempt to go forward instead. If that is not possible, it will try to turn left. If all three directions are unavailable, it will turn back.
The challenge for the contestants is to write a program that describes the path of the robot, starting from the entrance/exit square until it finally comes back to it. The movements are described by a single letter: 'F'
means forward, 'L'
is left, 'R'
is right and 'B'
stands for backward.Each of 'L'
, 'R'
and 'B'
does not only describe the change in orientation of the robot, but also the advancement of one square in that direction. The robot's initial direction is East. In addition, the path of the robot always ends at the entrance/exit square.
The judge responsible for the problem had completed all the samples and testdata, when disaster struck: the input file got deleted and there is no way to recover it! Fortunately the output and the samples are still there. Can you reconstruct the input from the output? For your convenience, he has manually added the number of test cases to both the sample output and the testdata output.
Input Format
On the first line one positive number: the number of test cases. After that per test case:
- one line with a single string: the movements of the robot through the maze.
Output Format
On the first line one positive number: the number of test cases, at most 100100. After that per test case:
- one line with two space-separated integers hh and ww (3 \le h, w \le 100)(3≤h,w≤100): the height and width of the maze, respectively.
- hh lines, each with ww characters, describing the maze: a
'#'
indicates a wall and a'.'
represents an empty square.
The entire contour of the maze consists of walls, with the exception of one square on the left: this is the entrance. The maze contains no cycles (i.e. paths that would lead the robot back to a square it had left in another direction) and no empty squares that cannot be reached from the entrance. Every row or column – with the exception of the top row, bottom row and right column – contains at least one empty square.
样例输入
3 FFRBLF FFRFRBRFBFRBRFLF FRLFFFLBRFFFRFFFRFRFBRFLBRFRLFLFFR
样例输出
3 4 4 #### ...# ##.# #### 7 5 ##### ...## ##.## #...# ##.## ##.## ##### 7 7 ####### #...#.# #.#...# #.#.### ..###.# #.....# #######
根据走的路求地图、
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int N = 300; 4 char str[N][N]; 5 char s[100100]; 6 int dx[] = {0, -1, 0, 1}, dy[] = {1, 0, -1, 0}; 7 int main() { 8 int t; 9 cin >> t; 10 printf("%d\n",t); 11 while(t--) { 12 cin >> s; 13 memset(str,0, sizeof(str)); 14 int x = 150, y = 150; 15 int last = 0; 16 str[x][y] = '.'; 17 for(int i = 0; s[i]; i ++) { 18 if(s[i] == 'F') { 19 } else if(s[i] == 'B') { 20 last = (last+2)%4; 21 } else if(s[i] == 'L') { 22 last = (last+1)%4; 23 } else if(s[i] == 'R') { 24 last = (last-1+4)%4; 25 } 26 x += dx[last], y += dy[last]; 27 str[x][y] = '.'; 28 } 29 int x1 = N, y1 = N, x2 = 0, y2 = 0; 30 for(int i = 1; i < N; i ++) { 31 for(int j = 1; j < N; j ++) { 32 if(str[i][j] == '.') { 33 x1 = min(x1, i); 34 y1 = min(y1, j); 35 x2 = max(x2, i); 36 y2 = max(y2, j); 37 } 38 } 39 } 40 x1--;x2++;y2++; 41 printf("%d %d\n",x2-x1+1,y2-y1+1); 42 for(int i = x1; i <= x2; i ++) { 43 for(int j = y1; j <= y2; j ++) { 44 if(str[i][j] == '.') printf("."); 45 else printf("#"); 46 }printf("\n"); 47 } 48 } 49 return 0; 50 }