vj p1589题解
http://www.vijos.cn/Problem_Show.asp?id=1589
这题.....郁闷了首先...
第一眼认为是差分约束系统,可怜的我不会差分....后来在大牛的指点下,明白了一个道理:原来这个题是可以用贪心的哦!
以结束位点先后排序,
用v记录点i是否有西瓜
依次枚举每个条件,
如果范围内的西瓜>=最小西瓜数,则跳过
否则
在没有西瓜的地方填上缺少数量的西瓜(当然尽量往后填,这样可以减少后面的西瓜使用,很好证明的贪心策略!)
当然还要累加使用的西瓜数
于是就这样搞定了........
我汗....不知差分约束系统该怎么写,希望大牛留言补上!先orz.....
如下为在下的AC代码.....
1Code
2var n,m:longint;
3 f:array[0..6000]of boolean;
4 c,s,e:array[0..3000] of longint;
5procedure init;
6 var i:longint;
7begin
8 readln(n,m);
9 for i:=1 to m do
10 begin
11 readln(s[i],e[i],c[i]);
12 end;
13end;
14procedure qsort(l,r:longint);
15 var i,j,x,t:Longint;
16begin
17 i:=l;j:=r;
18 x:=e[(i+j) shr 1];
19 repeat
20 while e[i]<x do inc(i);
21 while e[j]>x do dec(j);
22 if i<=j then
23 begin
24 t:=e[i];e[i]:=e[j];e[j]:=t;
25 t:=s[i];s[i]:=s[j];s[j]:=t;
26 t:=c[i];c[i]:=c[j];c[j]:=t;
27 inc(i);dec(j);
28 end;
29 until i>j;
30 if l<j then qsort(l,j);
31 if i<r then qsort(i,r);
32end;
33procedure find;
34 var i,j,sum,ans:longint;
35begin
36 fillchar(f,sizeof(f),0);
37 ans:=0;
38 for i:=1 to m do
39 begin
40 sum:=0;
41 for j:=s[i] to e[i] do if f[j] then inc(sum);
42 if sum>=c[i] then continue;
43 sum:=c[i]-sum;
44 ans:=ans+sum;
45 for j:=e[i] downto s[i] do
46 begin
47 if not(f[j]) then
48 begin
49 dec(sum);
50 f[j]:=true;
51 if sum=0 then break;
52 end;
53 end;
54 end;
55 writeln(ans);
56end;
57begin
58 init;
59 qsort(1,m);
60 find;
61end.
2var n,m:longint;
3 f:array[0..6000]of boolean;
4 c,s,e:array[0..3000] of longint;
5procedure init;
6 var i:longint;
7begin
8 readln(n,m);
9 for i:=1 to m do
10 begin
11 readln(s[i],e[i],c[i]);
12 end;
13end;
14procedure qsort(l,r:longint);
15 var i,j,x,t:Longint;
16begin
17 i:=l;j:=r;
18 x:=e[(i+j) shr 1];
19 repeat
20 while e[i]<x do inc(i);
21 while e[j]>x do dec(j);
22 if i<=j then
23 begin
24 t:=e[i];e[i]:=e[j];e[j]:=t;
25 t:=s[i];s[i]:=s[j];s[j]:=t;
26 t:=c[i];c[i]:=c[j];c[j]:=t;
27 inc(i);dec(j);
28 end;
29 until i>j;
30 if l<j then qsort(l,j);
31 if i<r then qsort(i,r);
32end;
33procedure find;
34 var i,j,sum,ans:longint;
35begin
36 fillchar(f,sizeof(f),0);
37 ans:=0;
38 for i:=1 to m do
39 begin
40 sum:=0;
41 for j:=s[i] to e[i] do if f[j] then inc(sum);
42 if sum>=c[i] then continue;
43 sum:=c[i]-sum;
44 ans:=ans+sum;
45 for j:=e[i] downto s[i] do
46 begin
47 if not(f[j]) then
48 begin
49 dec(sum);
50 f[j]:=true;
51 if sum=0 then break;
52 end;
53 end;
54 end;
55 writeln(ans);
56end;
57begin
58 init;
59 qsort(1,m);
60 find;
61end.