SHOW:
|
|
- or go back to the newest paste.
| 1 | // Author : Saurav Kalsoor | |
| 2 | - | // Minimize Unique Numbers - JAVA |
| 2 | + | // Partition - JAVA |
| 3 | ||
| 4 | import java.util.*; | |
| 5 | ||
| 6 | public class Test {
| |
| 7 | static Scanner sc = new Scanner(System.in); | |
| 8 | public static void main(String[] args) {
| |
| 9 | int n = sc.nextInt(); | |
| 10 | - | int k = sc.nextInt(); |
| 10 | + | int k = sc.nextInt(); |
| 11 | int x = sc.nextInt(); | |
| 12 | ||
| 13 | - | for(int i = 0;i < n; i++){
|
| 13 | + | |
| 14 | - | arr.add(sc.nextInt()); |
| 14 | + | for(int i = 0;i < n; i++){
|
| 15 | - | } |
| 15 | + | arr.add(sc.nextInt()); |
| 16 | } | |
| 17 | - | System.out.println(minimizeUnique(arr, k)); |
| 17 | + | |
| 18 | System.out.println(partition(n, k, x, arr)); | |
| 19 | } | |
| 20 | - | public static int minimizeUnique(ArrayList<Integer> arr, int k){
|
| 20 | + | |
| 21 | - | HashMap<Integer, Integer> count = new HashMap<>(); |
| 21 | + | public static int partition(int n, int k, int x, ArrayList<Integer> arr){
|
| 22 | Collections.sort(arr); | |
| 23 | - | for(int num : arr){
|
| 23 | + | int count = 1; |
| 24 | - | count.put(num, count.getOrDefault(num, 0) + 1); |
| 24 | + | PriorityQueue<Integer> pq = new PriorityQueue<>(); |
| 25 | - | } |
| 25 | + | int i = 1; |
| 26 | while(i < n){
| |
| 27 | - | Set<MyPair> st = new TreeSet<>(new Comparator<MyPair>() {
|
| 27 | + | int diff = arr.get(i) - arr.get(i-1); |
| 28 | - | @Override |
| 28 | + | if(diff > k){
|
| 29 | - | public int compare(MyPair a, MyPair b) {
|
| 29 | + | count++; |
| 30 | - | if(a.second == b.second) |
| 30 | + | pq.offer(diff); |
| 31 | - | return a.first - b.first; |
| 31 | + | } |
| 32 | - | return a.second - b.second; |
| 32 | + | i++; |
| 33 | - | } |
| 33 | + | } |
| 34 | - | }); |
| 34 | + | |
| 35 | while(!pq.isEmpty() && x > 0){
| |
| 36 | - | for(Map.Entry<Integer, Integer> x : count.entrySet()){
|
| 36 | + | int diff = pq.poll(); |
| 37 | - | st.add(new MyPair(x.getKey(), x.getValue()) ); |
| 37 | + | int req = (diff - 1)/k; |
| 38 | - | } |
| 38 | + | if(x >= req){
|
| 39 | x -= req; | |
| 40 | - | int res = st.size(); |
| 40 | + | count--; |
| 41 | }else{
| |
| 42 | - | for(MyPair x : st){
|
| 42 | + | break; |
| 43 | - | if(k >= x.second){
|
| 43 | + | } |
| 44 | - | res--; |
| 44 | + | } |
| 45 | - | k -= x.second; |
| 45 | + | |
| 46 | - | }else{
|
| 46 | + | return count; |
| 47 | - | break; |
| 47 | + | |
| 48 | - | } |
| 48 | + | |
| 49 | - | } |
| 49 | + | |
| 50 | ||
| 51 | - | return res; |
| 51 | + |