【Codeforces 1114B】Yet Another Array Partitioning Task

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


让你把数组分成k个连续的部分
使得每个部分最大的m个数字的和最大

【题解】


把原数组降序排序
然后选取前m*k个数字打标记
然后对于原数组
一直贪心地取
直到这个区间选了m个打标记的数字为止。
然后就划分一个区间>_<

【代码】

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

public class Main {
    
    
    static int N = (int)2e5;
    static InputReader in;
    static PrintWriter out;
    
    static class Pair implements Comparable<Pair>{
        int x,id;
        
        public Pair(int x,int id) {
            this.x = x;this.id = id;
        }

        @Override
        public int compareTo(Pair o) {
            // TODO Auto-generated method stub
            return o.x-this.x;
        }
        
    }
        
    public static void main(String[] args) throws IOException{
        //InputStream ins = new FileInputStream("E:\\rush.txt");
        InputStream ins = System.in;
        in = new InputReader(ins);
        out = new PrintWriter(System.out);
        //code start from here
        new Task().solve(in, out);
        out.close();
    }
    
    
    static class Task{
        public void solve(InputReader in,PrintWriter out) {
            int n,m,k;
            int []a = new int [N+10];
            int []tag = new int [N+10];
            Pair []b = new Pair[N+10];
            n = in.nextInt();m = in.nextInt();k = in.nextInt();
            for (int i = 1;i <= n;i++) a[i] = in.nextInt();
            for (int i = 1;i <= n;i++) {
                b[i] = new Pair(a[i],i);
            }
            Arrays.sort(b, 1,n+1);
            long ans1 = 0;
            for (int i = 1;i <=m*k;i++) {
                tag[b[i].id]= 1;
                ans1 = ans1 + b[i].x;
            }
            out.println(ans1);
            int cnt = 0;
            int cnt2 = 0;
            for (int i = 1;i <= n;i++){
                if (tag[i]==1) {
                    cnt++;
                    if (cnt==m) {
                        cnt = 0;
                        out.print(i+" ");
                        cnt2++;
                        if (cnt2==k-1){
                            return;
                        }
                    }
                }
            }
        }
    }

    

    static class InputReader{
        public BufferedReader br;
        public StringTokenizer tokenizer;
        
        public InputReader(InputStream ins) {
            br = new BufferedReader(new InputStreamReader(ins));
            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());
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/AWCXV/p/10361144.html