Https://www.hackerrank.com/contests/world-codesprint-6/challenges/bonetrousle
Given a number n, and the number of K, 1--k this k, asked to select the number of B, so that the number of B and equal to N.
First of all consider the minimum value, in the 1--k select the number of the first B, is the smallest, recorded as MI. Maximum, the number of B after the addition, recorded as MX
Notice one thing: if Mi <= n <= mx. So it's absolutely doable. Because Mi always increases by 1 (while ensuring that the requirements are met), it is possible to have a solution within this range.
So first enumerate from the MI, each time the number of B to the maximum K (number of b-1 changed to k-1 ...) and so on, because the number cannot be repeated.
is always enumerated to MI >= N. Then the output scheme can be.
Bug point: After B number is too big, will explode LL. So, when the next number is added to >= N, enough, to prove that N is <= MX, there is a viable solution.
Then the simulation can be. Complexity O (b)
#include <cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>using namespacestd;#defineINF (0X3F3F3F3F)typedefLong Long intLL; #include<iostream>#include<sstream>#include<vector>#include<Set>#include<map>#include<queue>#include<string>Const intMAXN = 1e5 + -; LL A[MAXN];voidWork () {//Ios::sync_with_stdio (false);LL N, K, B;//scanf ("%lld%lld%lld", &n, &k, &b);CIN >> N >> k >>b; //cout << n << "<< k <<" "<< b << Endl;LL mi =0, mx =0; for(inti =1; I <= b; ++i) {mi+=i; } for(LL i = k; I >= K-b +1; --i) {mx+=i; //if (MX < 0) while (1); if(MX >= N) Break;//OK } //cout << mi << "" << mx << endl; if(MX <N) {printf ("-1\n"); return ; } if(Mi >N) {printf ("-1\n"); return ; } LL Now=mi; intto =b; LLGet=K; for(inti =1; I <= b; ++i) A[i] =i; while(true) { now= Now-a[to] +Get; A[to]=Get; if(Now >=N) {LL cut= Now-N; A[to]-=cut; for(inti =1; I <= B-1; ++i) {printf ("%lld", A[i]); } printf ("%lld\n", A[b]); Break; } to--; Get--; }}intMain () {#ifdef LOCAL freopen ("Data.txt","R", stdin);#endif intT; CIN>>T;//cout << t << Endl; while(t--) work (); return 0;}
View Code
Bonetrousle Hackerrank Math + thinking problem