-
Notifications
You must be signed in to change notification settings - Fork 7
/
LightOj-1017 - Brush (III).cpp
65 lines (52 loc) · 1.46 KB
/
LightOj-1017 - Brush (III).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/// Time- 0.006s
/// 1017 - Brush (III)
/// Classical dp + sort
#include<bits/stdc++.h>
using namespace std;
int main()
{
int T,n,w,k,x;
cin>>T;
int caso=0;
while(T--)
{
caso++;
cin>>n>>w>>k;
int y[n+1];
int cover[n+1];
int dp[n+1][k+1];
memset(dp,0,sizeof(dp));
memset(cover,0,sizeof(cover));
for(int i=1;i<=n;i++) cin>>x>>y[i];
sort(y+1,y+1+n);
/// first we can find out if we touch particular point how many ranges can be covered ?
for(int i=1;i<=n;i++)
{
for(int j=i;j>=1;j--)
{
if(y[i]-y[j]<=w) cover[i]++;
else break;
}
}
/// now if we touch a point some ranges of its points will be covered for their ranges so, it is optimal to choose maximum k points
/// Here is the fact ,dynamic programming.
for(int i=1;i<=n;i++)
{
for(int j=1;j<=k;j++)
{
if(i==cover[i])
{
dp[i][j]=cover[i];
}
/// i>cover[i]
else
{
dp[i][j]=max(dp[i-1][j],dp[i-cover[i]][j-1]+cover[i]);
}
}
}
/// so,after full iteration -> at most k moves upon n points result will be dp[n][k]
printf("Case %d: %d\n", caso,dp[n][k]);
}
return 0;
}