Editorial for Bí hiểm
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này
Code mẫu của flashmt
#include <iostream> #include <algorithm> using namespace std; int n,k,re,a[100100],d[100100],mx[100100]; int ok(int maxVal) { long long biggest=0; for (int i=1;i<=maxVal;i++) { if (biggest+1<i) return 0; if (d[i]) { biggest+=1LL*i*d[i]; if (biggest>=k) return 1; } } return 0; } int main() { int test; cin >> test; while (test--) { re=mx[0]=-1; cin >> n >> k; for (int i=1;i<=n;i++) scanf("%d",&a[i]), mx[i]=max(mx[i-1],a[i]); int l=1,r=n,m,last=0; while (l<=r) { m=(l+r)/2; if (m<last) for (int i=m+1;i<=last;i++) d[a[i]]--; else for (int i=last+1;i<=m;i++) d[a[i]]++; if (ok(mx[m])) re=m, r=m-1; else l=m+1; last=m; } cout << re << endl; for (int i=1;i<=m;i++) d[a[i]]--; } }
Code mẫu của ladpro98
program riddle; uses math; const maxn=trunc(1e5)+5; fi=''; var a,org:array[1..maxn] of longint; l,r,i,tt,t,n,m,res,k:longint; inp:text; procedure sort(l,r:longint); var i,j,p,t:longint; begin if l>=r then exit; i:=l;j:=r; p:=a[random(r-l+1)+l]; repeat while a[i]<p do inc(i); while a[j]>p do dec(j); if i<=j then begin if i<j then begin t:=a[i];a[i]:=a[j];a[j]:=t; end; inc(i);dec(j); end; until i>j; sort(l,j);sort(i,r); end; function ok(p:longint):boolean; var t,i:longint; begin for i:=1 to p do a[i]:=org[i]; sort(1,p);t:=0; for i:=1 to p do begin if a[i]<=t+1 then inc(t,a[i]) else break; if t>=k then exit(true); end; if t<k then exit(false); end; begin assign(inp,fi);reset(inp); readln(inp,t); for tt:=1 to t do begin readln(inp,n,k); for i:=1 to n do read(inp,org[i]); l:=1;r:=n;res:=-1; while l<=r do begin m:=(l+r) shr 1; if ok(m) then begin res:=m; r:=m-1; end else l:=m+1; end; writeln(res); end; end.
Code mẫu của RR
#include <iostream> #include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <vector> #include <queue> #include <stack> #include <map> #include <set> #include <iomanip> #include <bitset> #include <complex> #define FOR(i,a,b) for(int i = a; i <= b; ++i) #define FORD(i,a,b) for(int i = a; i >= b; --i) #define REP(i,a) for(int i = 0; i < a; ++i) #define MP make_pair #define PB push_back using namespace std; int cnt[100111], a[100111]; int main() { int ntest; scanf("%d", &ntest); while (ntest--) { int n, k; scanf("%d%d", &n, &k); int can = 0; FOR(i,1,n) scanf("%d", &a[i]); memset(cnt, 0, sizeof cnt); bool ok = false; FOR(i,1,n) { if (a[i] > can + 1) { ++cnt[a[i]]; continue; } int save = can; can += a[i]; for(int x = save + 1; x <= can + 1 && x <= 100001; ++x) can += cnt[x] * x; if (can >= k) { ok = true; printf("%d\n", i); break; } } if (!ok) puts("-1"); } return 0; }
Code mẫu của hieult
#include<cstdio> #include<cmath> #include<cstring> #include<cstdlib> #include<cassert> #include<ctime> #include<algorithm> #include<iterator> #include<iostream> #include<cctype> #include<string> #include<vector> #include<map> #include<set> #include<queue> #include<list> //#include<conio.h> #define ep 0.000000001 #define maxn 10011 #define oo 1111111 #define base 100000000 #define TR(c, it) for(typeof((c).begin()) it=(c).begin(); it!=(c).end(); it++) #define chia 21266327 double const PI=4*atan(1); using namespace std; typedef pair<int, int> II; typedef vector<int> VI; typedef vector<II> VII; typedef vector<VI> VVI; typedef vector<VII> VVII; int n,k,a[100111],test,sl[100111],b[100111],run; long long tong; bool check; int tinh(int x){ memset(sl,0,sizeof(sl)); for(int i = 0;i<=x;i++) sl[a[i]]++; run = 0; for(int i = 0;i<=100000;i++){ for(int j = 0;j<sl[i];j++) b[run++] = i; } tong = 0; check = true; for(int i = 0;i<=x;i++){ if(b[i]>tong+1){ check = false; break; } else tong = tong+b[i]; } if(tong>=k && check) return true; else return false; } int main(){ // freopen("RIDDLE.in","r",stdin); scanf("%d",&test); for(int itest = 1;itest<=test;itest++){ scanf("%d %d",&n,&k); for(int i = 0;i<n;i++) scanf("%d",&a[i]); int u = 0, v = n , r; while(u<v){ r = (u+v)/2; if(tinh(r)) v = r; else u = r+1; // printf("%d\n",r); } if(u==n) printf("-1\n"); else printf("%d\n",u+1); } // getch(); }
Comments