Editorial for Phân Trang
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
var f,a:array[0..6000] of longint; n,l,i,j:longint; function min(x,y:longint):longint; begin if x<y then min:=x else min:=y; end; function max(x,y:longint):longint; begin if x>y then max:=x else max:=y; end; begin read(n,l); for i:=1 to n do begin read(j); a[i]:=a[i-1]+j; f[i]:=l; end; for i:=1 to n do for j:=i-1 downto 0 do if a[i]-a[j]<=l then f[i]:=min(f[i],max(f[j],l-a[i]+a[j])) else break; writeln(f[n]); end.
Code mẫu của happyboy99x
#include <algorithm> #include <bitset> #include <cctype> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef pair<int, int> ii; typedef vector<ii> vii; typedef vector<vii> vvii; typedef vector<int> vi; typedef vector<vi> vvi; #define sz(a) int((a).size()) #define fi first #define se second #define pb push_back #define mp make_pair #define all(c) (c).begin(), (c).end() #define tr(c,i) for(typeof((c).begin()) i = (c).begin(), _e = (c).end(); i != _e; ++i) #define present(c,x) ((c).find(x) != (c).end()) #define cpresent(c,x) (find(all(c),x) != (c).end()) #define rep(i,n) for(int i = 0, _n = (n); i < _n; ++i) #define repd(i,n) for(int i = (n)-1; i >= 0; --i ) #define fo(i,a,b) for(int i = (a), _b = (b); i <= _b; ++i) #define fod(i,a,b) for(int i = (a), _b = (b); i >= _b; --i) #define INF 1000000000 #define N 6005 int w[N], l, n, f[N]; int main() { //freopen( "input.txt", "r", stdin ); //freopen( "output.txt", "w", stdout ); scanf("%d%d",&n,&l); fo(i,1,n) { scanf("%d",w+i); w[i] += w[i-1]; } fo(i,1,n) { f[i] = INF; repd(j,i) { if( w[i] - w[j] > l ) break; f[i] = min( f[i], max( f[j], l-(w[i]-w[j]) ) ); } } printf("%d\n", f[n]); return 0; }
Code mẫu của ladpro98
program ptrang; uses math; const fi=''; var s,a,f:array[0..6001] of longint; n,l,res:longint; procedure input; var inp:text; i:longint; begin assign(inp,fi); reset(inp); readln(inp,n,l); for i:=1 to n do readln(inp,a[i]); close(inp); end; procedure init; var i:longint; begin s[1]:=a[1]; s[0]:=0; for i:=2 to n do s[i]:=s[i-1]+a[i]; end; function tong(i,j:longint):longint; begin exit(s[j]-s[i-1]); end; procedure process; var i,j:longint; begin for i:=1 to n do begin f[i]:=high(longint); for j:=i downto 1 do if tong(j,i)>l then break else f[i]:=min(f[i],max(f[j-1],l-tong(j,i))); end; end; begin input; init; process; write(f[n]); end.
Code mẫu của RR
program PTRANG; uses math; const FINP=''; FOUT=''; MAXN=6001; var n,l:longint; d,w,t:array[0..MAXN] of longint; procedure inp; var f:text; i:longint; begin assign(f,FINP); reset(f); readln(f,n,l); for i:=1 to n do readln(f,w[i]); close(f); end; procedure ans; var f:text; begin assign(f,FOUT); rewrite(f); writeln(f,d[n]); close(f); end; procedure solve; var i,j:longint; begin t[0]:=0; for i:=1 to n do t[i]:=t[i-1]+w[i]; for i:=1 to n do begin d[i]:=l; for j:=i downto 1 do begin if t[i]-t[j-1]>l then break; d[i]:=min(d[i],max(l-(t[i]-t[j-1]),d[j-1])); end; end; end; begin inp; solve; ans; end.
Code mẫu của hieult
#include <stdio.h> //#include <conio.h> main() { int a[6002],b[6002],t,j,n,l,max; scanf("%d %d",&n,&l); for(int i=1;i<=n;i++) scanf("%d",&a[i]); b[0]=0; for(int i=1;i<=n;i++) { b[i]=l; t=l; j=i; while(j!=0) { t=t-a[j]; if(t<0) break; if(t>b[j-1]) max=t; else max=b[j-1]; if(max<b[i]) b[i]=max; j--; } } printf("%d",b[n]); //getch(); }
Code mẫu của ll931110
Program PTRANG; Const input = ''; output = ''; Var n,l: longint; a,s,F: array[1..6000] of longint; Procedure init; Var fi: text; i: longint; Begin Assign(fi, input); Reset(fi); Readln(fi, n, l); For i:= 1 to n do readln(fi, a[i]); Close(fi); End; Procedure sum; Var i: longint; Begin s[1]:= a[1]; For i:= 2 to n do s[i]:= s[i - 1] + a[i]; End; Function max(x,y: longint): longint; Begin If x < y then max:= y else max:= x; End; Procedure optimize; Var i,k: longint; Begin F[1]:= l - s[1]; For i:= 2 to n do Begin F[i]:= l - s[i]; If F[i] < 0 then F[i]:= 1000000000; For k:= 1 to i - 1 do If (F[i] > max(F[k], l - (s[i] - s[k]))) and (l - (s[i] - s[k]) >= 0) then F[i]:= max(F[k], l - (s[i] - s[k])); End; End; Procedure solve; Var fo: text; Begin Assign(fo, output); Rewrite(fo); Writeln(fo, F[n]); Close(fo); End; Begin init; sum; optimize; solve; End.
Code mẫu của khuc_tuan
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int L = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; ++i) a[i] = sc.nextInt(); int[] f = new int[n]; for (int i = 0; i < n; ++i) { f[i] = Integer.MAX_VALUE; for (int j = i, s = 0; j >= 0; --j) { s += a[j]; if (s > L) break; f[i] = Math.min(f[i], Math.max(L - s, j == 0 ? 0 : f[j - 1])); } } System.out.println(f[n - 1]); } }
Comments