SHOW:
|
|
- or go back to the newest paste.
| 1 | // Author : Saurav Kalsoor | |
| 2 | - | // Basket Full of Balls - JAVA |
| 2 | + | // Shifting Compartments - JAVA |
| 3 | ||
| 4 | ||
| 5 | import java.util.*; | |
| 6 | ||
| 7 | public class Test {
| |
| 8 | - | |
| 8 | + | |
| 9 | - | static Scanner sc = new Scanner(System.in); |
| 9 | + | static Scanner sc = new Scanner(System.in); |
| 10 | public static void main(String[] args) {
| |
| 11 | - | public static void main(String[] args) {
|
| 11 | + | int n = sc.nextInt(); |
| 12 | - | int n = sc.nextInt(); |
| 12 | + | int q = sc.nextInt(); |
| 13 | - | int k = sc.nextInt(); |
| 13 | + | |
| 14 | String str = sc.next(); | |
| 15 | - | ArrayList<Integer> arr = new ArrayList<>(); |
| 15 | + | |
| 16 | - | for(int i=0; i < n ;i++){
|
| 16 | + | ArrayList<Integer> l = new ArrayList<>(); |
| 17 | - | arr.add(sc.nextInt()); |
| 17 | + | ArrayList<Integer> r = new ArrayList<>(); |
| 18 | ArrayList<Integer> k = new ArrayList<>(); | |
| 19 | ||
| 20 | - | System.out.println(bucketFullOfBalls(n, k, arr)); |
| 20 | + | for(int i=0; i < q ;i++){
|
| 21 | - | } |
| 21 | + | l.add(sc.nextInt()); |
| 22 | r.add(sc.nextInt()); | |
| 23 | - | public static int bucketFullOfBalls(int n, int k, ArrayList<Integer> arr){
|
| 23 | + | k.add(sc.nextInt()); |
| 24 | - | HashMap<Integer, Integer> count = new HashMap<>(); |
| 24 | + | |
| 25 | - | for(int x : arr){
|
| 25 | + | |
| 26 | - | count.put(x, count.getOrDefault(x, 0) + 1); |
| 26 | + | System.out.println(shiftingCompartments(n, q, str, l, r, k)); |
| 27 | } | |
| 28 | ||
| 29 | - | ArrayList<Integer> temp = new ArrayList<>(); |
| 29 | + | public static String shiftingCompartments(int n, int q, String s, ArrayList<Integer> l, ArrayList<Integer> r, ArrayList<Integer> k){
|
| 30 | - | for(Map.Entry<Integer, Integer> m : count.entrySet()){
|
| 30 | + | char[] str = s.toCharArray(); |
| 31 | - | if(m.getValue() >= k){
|
| 31 | + | |
| 32 | - | temp.add(m.getKey()); |
| 32 | + | for(int i=0; i < q; i++){
|
| 33 | - | } |
| 33 | + | cycle(str, l.get(i), r.get(i), k.get(i)); |
| 34 | } | |
| 35 | ||
| 36 | - | int m = temp.size(); |
| 36 | + | return String.valueOf(str); |
| 37 | - | if(m == 0){
|
| 37 | + | |
| 38 | - | return -1; |
| 38 | + | |
| 39 | public static void myReverse(char[] str, int i, int j){
| |
| 40 | while(i < j){
| |
| 41 | char temp = str[i]; | |
| 42 | str[i] = str[j]; | |
| 43 | - | int l = temp.get(0), r = l, i = 1; |
| 43 | + | str[j] = temp; |
| 44 | - | while(i < m){
|
| 44 | + | i++; |
| 45 | - | int j = i - 1; |
| 45 | + | j--; |
| 46 | - | while(i < m && temp.get(i) == temp.get(i-1) + 1){
|
| 46 | + | |
| 47 | - | i++; |
| 47 | + | |
| 48 | - | } |
| 48 | + | |
| 49 | - | i--; |
| 49 | + | public static void cycle(char[] str, int l, int r, int k){
|
| 50 | int n = r-l+1; | |
| 51 | - | if(temp.get(i) - temp.get(j) > r - l){
|
| 51 | + | k = k%n; |
| 52 | - | l = temp.get(j); |
| 52 | + | myReverse(str, l, l+n-k-1); |
| 53 | - | r = temp.get(i); |
| 53 | + | myReverse(str, l+n-k, r); |
| 54 | - | } |
| 54 | + | myReverse(str, l, r); |
| 55 | } | |
| 56 | - | i+=2; |
| 56 | + | |
| 57 |