【Codeforces 1106E】Lunar New Year and Red Envelopes

【链接】 我是链接,点我呀:)
【题意】

给你k个红包,每个红包可以在si..ti的时间范围内拿走。
抢完红包之后你得到wi元,然后你需要在di+1时刻才能继续抢红包
时间是线性的从1..n
然后某个人可以阻止你在x时刻抢红包,然后你的时间跳过1s(-1s)直接到达x+1时刻.
这个人可以阻止你m次。
请问这个人采用最优阻止策略下,你最少抢到的金额。
(如果有多个可以抢的红包,那么抢wi最大的,如果仍然相同抢di最大的,再相同的话就无所谓了,因为选哪个都一样了)

【题解】

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());
        }
    }
}

原文地址:https://www.cnblogs.com/AWCXV/p/10354414.html

时间: 2024-08-01 17:04:13

【Codeforces 1106E】Lunar New Year and Red Envelopes的相关文章

【Codeforces 1106B】Lunar New Year and Food Ordering

[链接] 我是链接,点我呀:) [题意] 给你n个菜以及每个人需要的菜以及数量 如果某个人无法满足它对菜的需求的话 就用价格比较低的菜来填充它的要求. (如果价格低的菜不够了,那么就直接输出0) 否则输出每个人的消费总量 [题解] 把所有的菜按照价格升序排序. 对于每一个顾客的kind,num 先减少菜的数量. 然后定义一个变量last 表示last之前的菜都已经售空(排序后,每个人可能会有多余的部分,要用前面最小的若干部分填充) 所以每个人都会让价格低的前面的一部分菜清空. 我们每次给某个顾客

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

【codeforces 415D】Mashmokh and ACM(普通dp)

[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=2000),问满足[数列长度是k && 数列中每一个元素arr[i]在1~n之间 && 数列中元素可以重复]的数列有多少个?结果对10^9+7取余 解题思路:dp[i][j]表示长度是j,最后一位是i的种数 if(kk%i==0) dp[kk][j+1]+=dp[i][j] 1 #i

【Codeforces 368A】Brain&#39;s Photos 水题

黑白灰都是#Black&White #include <cstdio> int n,m; int main() { scanf("%d%d",&n,&m); int ok=0; for(int i=0;i<n;i++) for(int j=0;j<m;j++) { char s[5]; scanf("%s",s); if(s[0]!='W'&&s[0]!='B'&&s[0]!='G')

【Codeforces 1114C】Trailing Loves (or L&#39;oeufs?)

[链接] 我是链接,点我呀:) [题意] 问你n!的b进制下末尾的0的个数 [题解] 证明:https://blog.csdn.net/qq_40679299/article/details/81167283 这题的话m比较大, 做个质因数分解就ok>_< 算n!有多少个x因子的话 以5为例子 (n=25) 25 20 15 10 5 把他们都除5 5 4 3 2 1 然后再除5 1 所以总共有6个 转换成代码就是 while(n>0){ ans+=n/5; n = n/5; } [代码

【Codeforces 332C】Students&#39; Revenge

Codeforces 332 C 我爱对拍,对拍使我快乐... 题意:有\(n\)个议题,学生们会让议会同意\(p\)个,其中主席会执行\(k\)个, 每一个议题执行后主席会掉\(a_i\)的头发,不执行后议会会增加\(b_i\)的不开心值, 然后主席想让议会的不开心值最小,如果有多重方案就选自己头发掉的最少的: 而学生们想让主席的头发掉的最多,如果有多种方案让议会的不开心值最大. 问让议会同意哪\(p\)个会达到最好的效果. 思路1: 这是我的不对的思路. (虽然没提交 我们首先将所有的数按照

【Codeforces 429D】 Tricky Function

[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = Sj - Si f(i,j) = (i-j)^2 + (Si - Sj)^2 观察这个式子,我们发现可以用类似于平面最近点对的算法来求解该问题 [代码] #include<bits/stdc++.h> using namespace std; #define MAXN 100010 const

【Codeforces 105D】 Bag of mice

[题目链接] http://codeforces.com/contest/148/problem/D [算法] 概率DP f[w][b]表示还剩w只白老鼠,b只黑老鼠,公主胜利的概率,那么 : 1. 公主抓到白老鼠,概率为w/(w+b) 2. 公主抓到黑老鼠,那么龙也抓到黑老鼠,如果跑出来的老鼠是白颜色的,则概率为 : b / (w + b) * b / (w + b - 1) * w / (w + b - 2) * f[w-1][b-2] 否则,概率为 : b / (w + b) * (b -

【Codeforces 258A】 Game With Sticks

[题目链接] http://codeforces.com/contest/451/problem/A [算法] 若n和m中的最小值是奇数,则先手胜,否则后手胜 [代码] #include<bits/stdc++.h> using namespace std; int n,m; int main() { scanf("%d%d",&n,&m); if (min(n,m) % 2 == 0) printf("Malvika\n"); else