NightEpiphany @ 2024-03-29 22:55:40
package exercise.example;
import java.io.*;
import java.util.*;
public class P8939 {
public static void main(String[] args) throws IOException {
Scanner inp=new Scanner(System.in);
int n=inp.nextInt(),q=inp.nextInt();
ArrayList<Integer> a=new ArrayList<>();
for (int i=0;i<n;i++)
a.add(inp.nextInt());
System.out.println();
for(int i=0;i<q;i++){
int operation = inp.nextInt();
if(operation == 1)
function1(inp.nextInt(), a);
else if(operation == 2)
function2(inp.nextInt(), a);
}
System.exit(0);
}
public static void function1(int m,ArrayList<Integer> a){
if(!a.contains(m))
System.out.println(-1);
else {
a.remove((Integer) m);
ArrayList<Integer> b=new ArrayList<>();
ArrayList<Integer> c=new ArrayList<>();
b=permutationFunc(a,c,0);
b.sort(Comparator.naturalOrder());
System.out.println(b.get(0));
}
}
public static void function2(int m,ArrayList<Integer> a){
a.add(m);
ArrayList<Integer> b=new ArrayList<>();
ArrayList<Integer> c=new ArrayList<>();
b=permutationFunc(a,c,0);
b.sort(Comparator.naturalOrder());
System.out.println(b.get(0));
}
public static int sigmaResult(ArrayList<Integer> a){
int result=Math.abs(a.get(0)-a.get(a.size()-1));
for(int i=a.size()-1;i>=1;i--)
result+=Math.abs(a.get(i)-a.get(i-1));
return result;
}
public static ArrayList<Integer> permutationFunc(ArrayList<Integer> list,ArrayList<Integer> b,int start)
{
if(start==list.size()-1) {
b.add(sigmaResult(list));
}
for(int i=start;i<=list.size()-1;i++)
{
Swap(list,i,start);
permutationFunc(list,b,start+1);
Swap(list,i,start);
}
return b;
}
public static void Swap(ArrayList<Integer> list,int i,int j) {
int temp;
temp = list.get(i);
list.set(i, list.get(j));
list.set(j, temp);
}
}
by aochiao @ 2024-05-02 17:35:36
@SixSeven 你又不是复读机,干嘛重复这么多遍?
by Never_Hate @ 2024-05-05 08:21:05
Java是啥?