1. 程式人生 > >【Codeforces 1114B】Yet Another Array Partitioning Task

【Codeforces 1114B】Yet Another Array Partitioning Task

parse ext n+1 turn int 題意 exti txt .com

【鏈接】 我是鏈接,點我呀:)
【題意】


讓你把數組分成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());
        }
    }
}

【Codeforces 1114B】Yet Another Array Partitioning Task