Question A
Test instructions: There are three locations, home h, supermarkets S1 and S2, then give you the distance between the three, and then ask you to walk from home S1 and S2 back home after the shortest distance is how much?
The puzzle: In four cases .... Manual simulation is coming out soon.
1/*zhen hao*/2 #include <bits/stdc++.h> 3 using namespace std; 4 5 typedef long Long ll; 6 7 ll dist[5]; 8 9 int main () { //freopen ("case.in", "R", stdin); ll D1 , D2, d3;12 cin >> D1 >> D2 >> d3;13 dist[0] = d1 + d2 + d3;14 dist[1] = D1 * 2 + d3 * 2;15< C9/>DIST[2] = D2 * 2 + d3 * 2;16 dist[3] = D1 * 2 + d2 * 2;17 cout << *min_element (Dist, dist + 4) << ; Endl;18 return 0;19}
code June
Question B
Test instructions: At first there is a sequence of a1......am, and then take a part of it consisting of a sequence of length n, F1......fn, and then a sequence B satisfies, now give you sequence F and sequence B, ask you can not uniquely determine a, if you can output possible, and output a sequence, if there are multiple answers, Output ambiguous, if there is a case of non-conforming output impossible;
The puzzle: Obviously, we record this f-value subscript, and then if there are multiple F-values corresponding to the same subscript is recorded, (the code is marked by-2), and then traverse B to find the corresponding F-value of the subscript is unique, if not unique, such as hit-2 first remember, because there may be a mismatch in the future, That is to first traverse the whole B sequence to see if there is impossible, and then judge whether there is ambiguous, and finally no output.
1/*zhen hao*/2 #include <bits/stdc++.h > 3 using namespace std; 4 5 const int MAXN = 1e5 + 100; 6 int VIS[MAXN], B[MAXN], A[MAXN]; 7 8 int Main () {9//freopen ("Case.in", "R", stdin), 1 int n, m;11 cin >> n >> m;12 memset (Vis,-, S Izeof vis); (int i = 1; I <= n; i++) {+ int t;15 scanf ("%d", &t); if (vis[t] = = 1 && ; VIS[T] =-2) vis[t] = I;17 else vis[t] = -2;18}19 for (int i = 1; I <= m; i++) scanf ("%d", B + i); OK = true;21 for (int i = 1; I <= m; i++) {if (vis[b[i]] = = 1) {puts ("impossible"); return 0;24 }25 if (vis[b[i]] = = 2) ok = false;26 else a[i] = vis[b[i]];27}28 if (!ok) {puts ("ambiguity"); return 0; }29 puts ("Possible"), 1 for (int i =; I <= m; i++) printf ("%d", A[i]); return 0;32}
code June
Question C
Test instructions: Give you the number of N, let you block, so that the number of blocks inside the sequence after the order and the whole sequence of the effect is the same, ask you up to how many pieces.
The solution: If the sequence is already sequenced, then the answer is N. Here we use the greedy method, I opened a data type node, inside the members have now,cur,num. We define the unordered sequence to be h, and the sequence is sh; we use this data type to represent the beginning of the current block, now refers to the sequence H subscript of the largest element in the current block, and Cur refers to the first position where this element appears in the ordered sequence sh, Num represents the number of occurrences of this element, initialized to 1, obviously cur+num-1 is the real position of this element in sh, and then when the loop to the current element equals this position, it means that the end of the block is reached, so just add one to the number of blocks, then update the node. It is also important to note that if the next element appears the same, then do not update to the new node, but num++, there is no attention to WA.
1/*zhen hao*/2 #include <bits/stdc++.h > 3 using namespace std; 4 5 const int MAXN = 1e5 + 100; 6 int H[MAXN], SH[MAXN]; 7 8 struct Node {9 int now;10 int cur;11 int num;12};13 int main () {//freopen ("case.in", "R", stdin) 16 int n;17 cin >> n;18 for (int i = 0; i < n; i++) {scanf ("%d", H + i); Sh[i] = h[i];21}22 s ORT (SH, sh + n); 0;24 Node temp = (node) {0, lower_bound (SH, sh + N, h[0])-SH, 1};25 for (int i = 0; i < n; i++) {if (i! = Temp.now) {H[i] > H[temp.now]) temp = (Node) {i, Lower_bound (sh, sh + N, h[i])-SH, 1};28 else if (h[i] = = H[temp.now]) temp.num++;29}30 if (temp.cur + temp.num-1 = i) {ans++;32 if (h[i + 1] = H[temp.now]) = temp = (Node) {i + 1, lower_bound (SH, sh + N, h[i + 1])-SH, 1};34}35 }36 printf ("%d\n", ans); PNS return 0;38}
code June
Title D
Test instructions: give you an X to indicate the number of schemes, the so-called scheme refers to the matrix of n * m, with 1*1,2*2 ... The number that can be moved in this matrix. Find out that the number of moves is added to all the satisfied matrices of X.
Solution: There are two ways:
First of all, I think of a kind of: n*m the composition of the scheme is: (k+1) *n*m + sigma (k*k)-sigma (k) * (N+m), where the Sigma is cumulative, k = min (n,m)-1, which is easy to deduce, then the enumeration of N, then the two-point answer to find the required m. This practice is cumbersome, there are two places to note, the first is to use Unsignedlonglong, and the second is when the situation to meet n*n to be judged alone, because of the accuracy of the relationship.
1/*zhen hao*/2 #include <bits/stdc++.h> 3 using namespace std; 4 5 typedef unsigned long long ll; 6 Const LL INF = 1e18 + 100; 7 Vector<pair<ll, ll> > ans; 8 9 ll Getsquaresum (ll i) {return 1ULL * I * (i + 1) * (2 * i + 1)/6;11}12 ll Get_num (ll X, ll y) {+ ll k = Min (x, y)-1;15 return 1ULL * (k + 1) * x * y + getsquaresum (k)-(x + y) * k * (1 + k)/2;16}17 int main () {19 Freopen ("Case.in", "R", stdin); ll n;21 cin >> n;22 for (ll i = 1;; i++) {at-ll temp = Getsquaresum ( i) if (temp >= N) {if (temp = = N) ans.push_back (Make_pair (i, i)); break;27}28 ll low = 1, high = n/i, mid;29 bool ok = false;30 when (low <= high) {To mid = low + (high-low)/2;32 ll t = Get_num (i, mid),//cout << x << "<< y <<" "<< t << endl;34 if (T > N) high = Mid-1;35 else if (T < n) Low = mid + 1;36 else {37 OK = true;38 break;39}40}41 if (OK) ans.push_back (Make_pair (i, mid));}43 int size = Ans.size (); (int i = size-1; I >= 0; i--) if (ans[i].first! = Ans[i].second) {$ ans.push_back (ma Ke_pair (Ans[i].second, Ans[i].first));}48 cout << ans.size () << endl;49 for (int i = 0; i < (int ) Ans.size (); i++) cout << ans[i].first << "" << ans[i].second << endl;51 return 0;52}code June
Reference to the solution, there is a more ingenious approach: observe the Sub Sigma (n-k) * (m-k), k = min (n,m), we first assume that this m=n words, will be less (m-n) *k, (m-n) is a fixed value, and then K can be written in the form of accumulation, So as long as you can determine whether the integer is the Sigma (k* (m-n)), that is, can divide the sigma (k) = N (n-1)/2, the complexity is O (n);
1/*zhen hao*/2 #include <bits/stdc++.h> 3 using namespace std; 4 5 typedef long Long ll; 6 7 vector<pair<ll, ll> > ans; 8 9 int main () { //freopen ("case.in "," R ", stdin); ll x;12 cin >> x;13 for (ll n = 1; x > 0; n++) { X-= n * n;15 ll t = n * (n + 1)/2;16 if (x% t = = 0) { ans.push_back (Make_pair (n, x/t + n)); Ans.push_back (Make_pair (x/t + N, N)), }20 }21 sort (ans.begin (), Ans.end ()) ; Ans.erase (Unique (Ans.begin (), Ans.end ()), Ans.end ()), int size = Ans.size (), printf ("%d\n", size); 25 for (int i = 0; i < size; i++) cout << ans[i].first << "" << ans[i].second << End l;27 return 0;28}
code June
Codeforces Round #332 (Div. 2)