【CodeCraft-19 and Codeforces Round #537 (Div. 2) C】Creative Snap

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

横坐标1..2^n对应着2^n个复仇者的基地,上面有k个复仇者(位置依次给出)。
你是灭霸你要用以下方法消灭这k个复仇者:
一开始你获取整个区间[1..2^n]
假设你当前获取的区间为[l,r]
mid = (l+r)/2
那么你每次有两种选择
1.将整个区间全都毁掉,如果这个区间里没有复仇者,那么花费为A,否则花费为B复仇者个数区间长度
2.将区间分为[l,mid]和[mid+1,r]分开毁掉(即分别获取[l,mid]和[mid+1,r]这两个区间,然后累加递归处理后的花费)
问你最小花费是多少

【题解】

如果2^n比较大的话,k个复仇者其实所占据的区间会非常少
也就是说有很多区间都是空着的。。
而空着的区间其实就没有必要用第二种选择了,因为显然直接返回A是最好的
所以我们只需要模拟题目说的方法就好。
只需要用两个二分查找获取当前区间里面有多少个复仇者就好
如果没有复仇者了就直接返回A,否则模拟题目描述就ok
(同一个基地可能有多个复仇者).

【代码】

import java.io.*;
import java.util.*;

public class Main {

    final static int N = (int)1e5;
    static InputReader in;
    static PrintWriter out;
    static int n,k,A,B,a[],r;

    static int leftmorethan(int x) {
        int l = 1,r = k,temp=-1;
        while (l<=r) {
            int mid = (l+r)>>1;
            if (x<=a[mid]) {
                temp = mid;
                r = mid - 1;
            }else {
                l = mid + 1;
            }
        }
        return temp;
    }

    static int rightlessthan(int x) {
        int l = 1,r = k,temp = -1;
        while (l<=r) {
            int mid = (l+r)>>1;
            if (x>=a[mid]) {
                temp = mid;
                l = mid + 1;
            }else {
                r = mid-1;
            }
        }
        return temp;
    }

    static long dfs(int l,int r) {
        int L = leftmorethan(l);
        int R = rightlessthan(r);
        boolean ok = true;
        if (L==-1 || R==-1) ok = false;
        if (L>R) ok = false;
        if (!ok) return 1l*A;

        if (l==r) return 1l*B*(R-L+1);

        int mid = (l+r)>>1;
        long ans;
        //select all
        ans = 1l*B*(R-L+1)*(r-l+1);

        //divede
        ans = Math.min(ans, dfs(l,mid)+dfs(mid+1,r));
        return ans;
    }

    public static void main(String[] args) throws IOException{
        in = new InputReader();
        out = new PrintWriter(System.out);

        //code start from here
        a = new int[N+10];
        n = in.nextInt();k = in.nextInt();A = in.nextInt();B = in.nextInt();
        for (int i = 1; i <= k;i++) a[i] = in.nextInt();
        r = 1;
        for (int i = 1;i <= n;i++) r = r*2;
        Arrays.sort(a, 1,k+1);
        out.print(dfs(1,r));
        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/10357906.html

时间: 2024-08-30 07:45:48

【CodeCraft-19 and Codeforces Round #537 (Div. 2) C】Creative Snap的相关文章

CodeCraft-19 and Codeforces Round #537 (Div. 2) - D. Destroy the Colony(动态规划+组合数学)

Problem  CodeCraft-19 and Codeforces Round #537 (Div. 2) - D. Destroy the Colony Time Limit: 2000 mSec Problem Description Input Output For each question output the number of arrangements possible modulo 10^9+7. Sample Input abba21 41 2 Sample Output

【Codeforces Round #446 (Div. 2) A】Greed

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 贪心选容量大的瓶子就好 [代码] #include <bits/stdc++.h> #define int long long using namespace std; const int N = 1e5; int n; int a[N+10],b[N+10]; main(){ #ifdef LOCAL_DEFINE freopen("F:\\c++source\\rush_in.txt", "r

【Codeforces Round #446 (Div. 2) C】Pride

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 想一下,感觉最后的结果肯定是从某一段开始,这一段的gcd为1,然后向左和向右扩散的. 则枚举那一段在哪个地方. 我们设这一段中所有的数字都做了一次gcd. 假设在i..j这一段. 则求gcd的顺序是(i,i+1),(i+1,i+2)...(j-1,j) 这样a[j]=gcd(a[i],a[i+1]..a[j])了 即顺序求了一遍gcd. 这样,预处理一下i到j的gcd. 如果gcd[i][j]==1,则获取到一个可行方案. 求一

【Codeforces Round #447 (Div. 2) A】QAQ

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] C语言程序练习题 [代码] #include <bits/stdc++.h> using namespace std; string s; int main(){ #ifdef LOCAL_DEFINE freopen("F:\\c++source\\rush_in.txt", "r", stdin); #endif ios::sync_with_stdio(0),cin.tie(0);

【Codeforces Round #451 (Div. 2) D】Alarm Clock

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 尺取法+二分. 类似滑动窗口. 即左端点为l,右端点为r. 维护a[r]-a[l]+1总是小于等于m的就好. (大于m就右移左端点) 然后看看里面的数字个数是不是小于k; 不是的话让l..r中最右边那个数字删掉就好. ->链表优化一下即可. [代码] /* 1.Shoud it use long long ? 2.Have you ever test several sample(at least therr) yourself

【Codeforces Round #452 (Div. 2) A】 Splitting in Teams

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 贪心 1优先和2组队. 如果1没有了 就结束. 如果1还有多余的. 那么就自己3个3个组队 [代码] #include <bits/stdc++.h> using namespace std; const int N = 2e5; int n; int a[3]; int main(){ #ifdef LOCAL_DEFINE freopen("rush_in.txt", "r", st

【Codeforces Round #452 (Div. 2) B】Months and Years

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 闰,平,平 平,闰,平 平,平,闰 平,平,平 4种情况都考虑到就好. 可能有重复的情况. 但是没关系啦. [代码] #include <bits/stdc++.h> using namespace std; const int N = 24; int p[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; int r[12] = {31,29,31,30,31,30,31,31,30,3

【Codeforces Round #452 (Div. 2) C】 Dividing the numbers

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] n为偶数. l = 1, r = n (l,r)放在一组 l++,r-- 新的l,r放在另外一组 直到l+1==r 这个时候,判断两组的和,如果一样的话,分散在两组 差为1否则差为0 n为奇数 l = 2,r = n (l,r)放在一组 l++,r-- 新的l,r放在另外一组 直到l+1==r 这个时候,判断两组的和,如果一样的话,分散在两组 差为0(把1放在那个较少的组) 否则,差为1 1随意放在哪一组都可以 [代码] #in

【Codeforces Round #456 (Div. 2) C】Perun, Ult!

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] set1 < pair < int,int > > set1;记录关键点->某个人怪物永远打不死了,第一维是时间,第二维是下标 int dic[1e5+10] //记录对应下标的怪物它此时此刻在何时打不死了 set2 < pair< int,int > > set2;关键点2->有怪物要更新了的时间点,以及记录的信息下标idx2 之所以这样记录.是为了尽可能多地让怪物存活时间长一