A. Patrick and Shopping time limit/test 1 second memory limit per test 256 megabytes input standard input output stand ARD output
Today, Patrick waits for a visit from his friend Spongebob. To prepare for the visit, Patrick needs-buy some goodies in two stores located There is a D1 meter long road between he house and the "the" and "a D2 meter long road between his house and the Seco nd shop. Also, there is a road of length D3 directly connecting this two to each. Help Patrick calculate the "minimum distance" and "he needs" to "walk" to "" to "" both "to" "to".
Patrick always starts at his house. He should visit both shops moving only along the three of existing roads and return to his house. He doesn ' t mind visiting of the same shop or passing the same road. The only goal are to minimize the total distance traveled. Input
The "a" of the input contains three integers d1, D2, D3 (1≤d1, D2, d3≤108)-the lengths of the paths. D1 is the length of the path connecting Patrick's house and the "the" D2 is the length of the path connecting Patrick's house and the second shop; D3 is the length of the path connecting both shops. Output
Print the minimum distance that Patrick would have to walk in order to visit both shops and return to his house. Sample Test (s) input
10 20 30
Output
60
Input
1 1 5
Output
4
Note
The "The" is shown on the "the" problem statement. One of the optimal routes Is:house, second shop house.
In the second sample one of the the optimal Routes Is:house-a-house second.
Discuss:
D1+d2+d3
D1*2+d2*2
D1*2+d3*2
D2*2+d3*2
Take the minimum value to
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
int d1,d2,d3;
int main ()
{
#ifdef yzy
freopen ("Yzy.txt", "R", stdin);
#endif
cin >> D1 >> D2 >> D3;
int ans = d1 + d2 + d3;
ans = min (ans,d1*2+d2*2);
ans = min (ans,d1*2+d3*2);
ans = min (ans,d2*2+d3*2);
cout << ans;
return 0;
}
B. Spongebob and joke time limit/test 2 seconds memory limit per test 256 megabytes input standard input output Standa RD output
While Patrick is gone shopping, Spongebob decided to play a little trick on his friend. The naughty sponge browsed through Patrick ' s personal stuff and found a sequence A1, A2, ..., am of length m, consisting O f integers from 1 to n, not necessarily distinct. Then He picked some sequence F1, F2, ..., fn of length n and for each number AI got number bi = Fai. To finish the prank he erased the initial sequence AI.
It ' s hard to express how sad Patrick is when he returned home from shopping! We'll just say that Spongebob immediately got really sorry about what he has do and he are now trying to restore the OR Iginal sequence. Help him does this or determine the this is impossible. Input
The "I" of the input contains two integers n and m (1≤n, m≤100)-the lengths of sequences fi and bi Respec Tively.
The second line contains n integers, determining sequence f1, F2, ..., FN (1≤fi≤n).
The last line contains m integers, determining sequence B1, B2, ..., BM (1≤bi≤n). Output
Print "Possible" if there is exactly one sequence AI, such that bi = FAI for all I-1 to M. Then print m integers a1, A2, ..., am.
If There are multiple suitable sequences AI, print "ambiguity".
If Spongebob has made a mistake in he calculations and no suitable sequence AI exists, print "impossible". Sample Test (s) input
3 3
3 2 1 1 2 3
Output
Possible
3 2 1
Input
3 3
1 1 1 1 1 1
Output
Ambiguity
Input
3 3
1 2 1 3 3 3
Output
Impossible
Note
In the ' 3 is replaced by 1 and vice versa while 2 never changes. The answer exists and is unique.
The second sample all numbers are replaced by 1, so it is impossible to unambiguously restore the original sequence.
In the third sample fi≠3 to all I, so no sequence AI transforms to such bi and we can say for sure that Spongebob has Made a mistake.
Several types of discussions are:
If there is and only one of the unique solutions for any bi,f
If there are multiple solutions for a bi,f
If there is no solution in a bi,f impossible!
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
const int MAXN = 1e5 +;
int a[maxn],n,m,i,j;
Vector <int> POS[MAXN];
int main ()
{
#ifdef yzy
freopen ("Yzy.txt", "R", stdin);
#endif
cin >> n >> m;
for (i = 1; I <= n; i++) {
int x; scanf ("%d", &x);
Pos[x].push_back (i);
}
BOOL flag = 0;
for (i = 1; I <= m; i++) {
int x; scanf ("%d", &x);
if (!pos[x].size ()) {
printf ("impossible");
return 0;
}
if (Pos[x].size () > 1) {
flag = 1;
Continue;
}
A[i] = pos[x][0];
}
if (flag) {
printf ("ambiguity");
return 0;
}
printf ("possible\n");
for (i = 1; I <= m i++) printf ("%d", A[i]);
return 0;
}
C. Day at the Beach time limit/test 2 seconds memory limit per test 256 megabytes input standard input output standard Output
One day Squidward, Spongebob and Patrick decided to the beach. Unfortunately, the weather is bad and so the friends were unable to ride waves. However, they decided to spent their time building sand.
At the "End of" the day there were n castles built by friends. Castles are numbered from 1 to N, and the height of the i-th castle is equal Tohi. When friends were about to leave, Squidward noticed, which castles are not ordered by their height, and this looks ugly. Now friends are going to reorder the castles into a way to obtain this condition Hi≤hi + 1 holds for all I from 1 to n-1 .
Squidward suggested the following process of sorting castles:castles are split into blocks-groups of consecutive castle S. Therefore the blocks from I to J would include castles I, i + 1, ..., J. A block could consist of a single castle. The partitioning is chosen in such a way this every is a part of Castle one block. Each of the sorted independently from blocks, which is the sequence hi, Hi + 1, ..., HJ becomes sorted. The partitioning should satisfy the condition this is sorted, the sequence hi becomes sorted. This may always being achieved by saying that whole sequence is a single block.
Even Patrick understands that increasing the number of blocks in partitioning would ease the sorting process. Now friends ask your to count the maximum possible number of blocks in a partitioning this satisfies all the above requirem Ents. Input
The ' The ' input contains a single integer n (1≤n≤100)-the number of castles Spongebob, Patrick and S Quidward made from sand during the day.
The next line contains n integers hi (1≤hi≤109). The i-th of integers corresponds to the height of the i-th castle. Output
Print the maximum possible number of blocks in a valid partitioning. Sample Test (s) input
3
1 2 3
Output
3
Input
4
2 1 3 2
Output
2
Note
In the ' The ' partitioning looks like this: [1][2][3].
In the second sample the partitioning is: [2, 1][3, 2]
Not difficult to prove (think 233) Local best = global optimal
Well, since we're going to be the largest block, it's obvious that each piece is as small as possible
pretreatment min[i] = Min{hj | I <= j <= N}
It's not hard to prove that as long as the current highest value <=min[i] can be divided
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXN = 1e5 +;
int min[maxn],h[maxn],n,i,j,ans = 1;
int main ()
{
#ifdef yzy
freopen ("Yzy.txt", "R", stdin);
#endif
cin >> N;
for (i = 1; I <= n; i++) scanf ("%d", &h[i));
Min[n] = H[n];
for (i = n-1 i > 0; i--) {
min[i] = h[i]; Min[i] = min (min[i+1],min[i]);
}
int H = h[1];
for (i = 1; I <= n; i++) {
H = max (H,h[i]);
if (min[i+1] >= H) {
++ans; continue;
}
}
cout << ans;
return 0;
}
D. Spongebob and squares time limit/test 2 seconds memory limit per test 256 megabytes input standard input output STA Ndard output
Spongebob is already tired trying to reason his weird the actions and calculations, so he simply asked your to find all pairs O f N and M, such that there are exactly x distinct squares in the table consisting of n rows and M columns. For example, in a 3x5 table there are 15squares with side one, 8 squares with side two and 3 squares with side. The total number of distinct squares in a 3x5 table IS15 + 8 + 3 = 26. Input
The ' The ' input contains a single integer x (1≤x≤1018)-the number of squares inside the tables Spongebob is interested I