Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.ArrayList;
- import java.util.LinkedList;
- class IntegerList {
- LinkedList<Integer> list;
- public IntegerList() {
- list = new LinkedList<>();
- }
- public IntegerList(Integer... numebrs) {
- this();
- for(Integer i : numebrs) {
- list.add(i);
- }
- }
- public void add(int el, int idx) {
- if(idx > list.size()) {
- int n = idx - list.size();
- for(int i = 0; i < n; ++i) {
- list.add(0);
- }
- list.add(el);
- } else {
- list.add(idx, el);
- }
- }
- public void remove(int idx) {
- list.remove(idx);
- }
- public void set(int el, int idx) {
- list.set(idx, el);
- }
- public int get(int idx) {
- return list.get(idx);
- }
- public int size() {
- return list.size();
- }
- public int count(int el) {
- int tmp = 0;
- for(int i : list) {
- if(i == el)
- ++tmp;
- }
- return tmp;
- }
- public void removeDuplicates() {
- LinkedList<Integer> tmp = list;
- list = new LinkedList<>();
- for(int i = tmp.size() - 1; i >= 0; --i) {
- if(!list.contains(tmp.get(i))) {
- list.addFirst(tmp.get(i));
- }
- }
- }
- public int sumFirst(int k) {
- int sum = 0;
- for(int i = 0; i < k&&i < list.size(); ++i) {
- sum += list.get(i);
- }
- return sum;
- }
- public int sumLast(int k) {
- int sum = 0;
- for(int i = 0; i < k; ++i) {
- sum += list.get(list.size() - i - 1);
- }
- return sum;
- }
- public void shiftRight(int idx, int k) {
- int indeks = (idx + k) % list.size();
- int el = list.get(idx);
- list.remove(idx);
- list.add(indeks, el);
- /*for(int i = 0; i < k; ++i) {
- int idk1 = (idx + i) % list.size();
- int idk2 = (idx + i + 1) % list.size();
- int tmp1 = list.get(idk1);
- int tmp2 = list.get(idk2);
- list.set(idk1, tmp2);
- list.set(idk2, tmp1);
- }*/
- }
- public void shiftLeft(int idx, int k) {
- int indeks =(idx - k) % list.size();
- if(indeks < 0) {
- indeks = list.size() + indeks;
- }
- int el = list.get(idx);
- list.remove(idx);
- list.add(indeks, el);
- }
- public IntegerList addValue(int value) {
- IntegerList tmp = new IntegerList();
- for(int i = 0; i < list.size(); ++i) {
- tmp.add(list.get(i) + value, i);
- }
- return tmp;
- }
- }
- public class IntegerListTest {
- public static void main(String[] args) {
- Scanner jin = new Scanner(System.in);
- int k = jin.nextInt();
- if ( k == 0 ) { //test standard methods
- int subtest = jin.nextInt();
- if ( subtest == 0 ) {
- IntegerList list = new IntegerList();
- while ( true ) {
- int num = jin.nextInt();
- if ( num == 0 ) {
- list.add(jin.nextInt(), jin.nextInt());
- }
- if ( num == 1 ) {
- list.remove(jin.nextInt());
- }
- if ( num == 2 ) {
- print(list);
- }
- if ( num == 3 ) {
- break;
- }
- }
- }
- if ( subtest == 1 ) {
- int n = jin.nextInt();
- Integer a[] = new Integer[n];
- for ( int i = 0 ; i < n ; ++i ) {
- a[i] = jin.nextInt();
- }
- IntegerList list = new IntegerList(a);
- print(list);
- }
- }
- if ( k == 1 ) { //test count,remove duplicates, addValue
- int n = jin.nextInt();
- Integer a[] = new Integer[n];
- for ( int i = 0 ; i < n ; ++i ) {
- a[i] = jin.nextInt();
- }
- IntegerList list = new IntegerList(a);
- while ( true ) {
- int num = jin.nextInt();
- if ( num == 0 ) { //count
- System.out.println(list.count(jin.nextInt()));
- }
- if ( num == 1 ) {
- list.removeDuplicates();
- }
- if ( num == 2 ) {
- print(list.addValue(jin.nextInt()));
- }
- if ( num == 3 ) {
- list.add(jin.nextInt(), jin.nextInt());
- }
- if ( num == 4 ) {
- print(list);
- }
- if ( num == 5 ) {
- break;
- }
- }
- }
- if ( k == 2 ) { //test shiftRight, shiftLeft, sumFirst , sumLast
- int n = jin.nextInt();
- Integer a[] = new Integer[n];
- for ( int i = 0 ; i < n ; ++i ) {
- a[i] = jin.nextInt();
- }
- IntegerList list = new IntegerList(a);
- while ( true ) {
- int num = jin.nextInt();
- if ( num == 0 ) { //count
- list.shiftLeft(jin.nextInt(), jin.nextInt());
- }
- if ( num == 1 ) {
- list.shiftRight(jin.nextInt(), jin.nextInt());
- }
- if ( num == 2 ) {
- System.out.println(list.sumFirst(jin.nextInt()));
- }
- if ( num == 3 ) {
- System.out.println(list.sumLast(jin.nextInt()));
- }
- if ( num == 4 ) {
- print(list);
- }
- if ( num == 5 ) {
- break;
- }
- }
- }
- }
- public static void print(IntegerList il) {
- if ( il.size() == 0 ) System.out.print("EMPTY");
- for ( int i = 0 ; i < il.size() ; ++i ) {
- if ( i > 0 ) System.out.print(" ");
- System.out.print(il.get(i));
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement