【链接】 我是链接,点我呀:)
【题意】
【题解】
dp 设dp[i][j]表示i时刻已经用了j次阻止机会,后面能抢到的最少金额. 我们可以用sort+优先队列求出choose[i] 即表示i时刻你会选择哪一个红包(注意是排序后的红包QAQ) 那么在i时刻有两种选择 1.这个人进行干扰 那么转移到dp[i+1][j+1] 2.这个人不进行干扰 那么如果i时刻有的选 就转移到dp[a[choose[i]].d+1][j]+a[choose[i]].w 如果没得选就转移到dp[i+1][j]每次取最小值就好
记得开long
【代码】
import java.io.*;
import java.util.*;
public class Main {
static InputReader in;
static PrintWriter out;
static class RedEnvelope{
int s,t,d,w,id;
}
public static Comparator<RedEnvelope> cmp1 = new Comparator<Main.RedEnvelope>() {
@Override
public int compare(RedEnvelope o1, RedEnvelope o2) {
return o1.s-o2.s;
}
};
public static Comparator<RedEnvelope> cmp2 = new Comparator<Main.RedEnvelope>() {
@Override
public int compare(RedEnvelope o1, RedEnvelope o2) {
if (o1.w==o2.w) {
return o2.d-o1.d;
}else {
return o2.w-o1.w;
}
}
};
static int N = (int)1e5;
static int n,m,k;
static RedEnvelope a[];
static int choose[];
static PriorityQueue<RedEnvelope> pq;
static long dp[][];
static long dfs(int t,int cnt) {
if (dp[t][cnt]!=-1) return dp[t][cnt];
if (t>n) return 0;
//打扰t时刻
long temp1 = (long)1e17;
if (cnt+1<=m) temp1 = Math.min(temp1, dfs(t+1,cnt+1));
//不打扰
long temp2 = (long)1e17;
if (choose[t]!=-1) {
temp2 = Math.min(temp2,dfs(a[choose[t]].d+1,cnt)+a[choose[t]].w);
}else {
temp2 = Math.min(temp2, dfs(t+1,cnt));
}
dp[t][cnt] = Math.min(temp1, temp2);
return dp[t][cnt];
}
public static void main(String[] args) throws IOException{
in = new InputReader();
out = new PrintWriter(System.out);
//code start from here
a = new RedEnvelope[N+10];
for (int i = 1;i <= N;i++) a[i] = new RedEnvelope();
pq = new PriorityQueue<>(cmp2);
choose = new int[N+10];
dp = new long[N+10][200+10];
for (int i = 0;i <= N;i++)
for (int j = 0;j <= 200;j++)
dp[i][j] = -1;
n = in.nextInt();m = in.nextInt();k = in.nextInt();
for (int i = 1;i <= k;i++) {
a[i].s = in.nextInt();
a[i].t = in.nextInt();
a[i].d = in.nextInt();
a[i].w = in.nextInt();
}
Arrays.sort(a, 1,k+1,cmp1);
for (int i = 1;i <= k;i++) a[i].id = i;//排完序再标号!QAQ
int j = 1;
for (int i = 1;i <= n;i++) {
for (;j<=k;) {
if (a[j].s<=i) {
pq.add(a[j]);
j++;
}else break;
}
while (!pq.isEmpty()) {
RedEnvelope temp = pq.peek();
if (temp.t<i) {
pq.poll();
continue;
}
choose[i] = temp.id;
break;
}
if (pq.isEmpty()) choose[i] = -1;
}
out.println(dfs(1,0));
out.close();
}
static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer;
public InputReader() {
br = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
}
public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}