如果一个数列S满足对于所有的合法的i,都有S[i + 1] = S[i] + d, 这里的d也可以是负数和零,我们就称数列S为等差数列。
小易现在有一个长度为n的数列x,小易想把x变为一个等差数列。小易允许在数列上做交换任意两个位置的数值的操作,并且交换操作允许交换多次。但是有些数列通过交换还是不能变成等差数列,小易需要判别一个数列是否能通过交换操作变成等差数列
输入包括两行,第一行包含整数n(2 ≤ n ≤ 50),即数列的长度。 第二行n个元素x[i](0 ≤ x[i] ≤ 1000),即数列中的每个整数。
如果可以变成等差数列输出"Possible",否则输出"Impossible"。
3 3 1 2
Possible
import java.util.Scanner;
import java.util.Arrays;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int num = in.nextInt();
int[] arr = new int[num];
for(int i = 0; i < num ;i++){
arr[i] = in.nextInt();
}
Arrays.sort(arr);
//需要定义一个布尔变量标记是否成功
boolean flag = true;
int d = arr[1] - arr[0];
for(int i = 2;i<arr.length;i++){
if(arr[i] - arr[i-1] != d){
flag = false;
}
}
if(flag){
System.out.println("Possible");
}else{
System.out.println("Impossible");
}
}
}
/* *时间复杂度O(n),空间复杂度O(n) *1、找到等差数列的差值dif *2、判断每个数对应差值dif的倍数(arr-min)/dif */ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = Integer.valueOf(sc.nextLine()); int[] arr = new int[n]; int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE; String[] str = sc.nextLine().split(" "); for (int i = 0; i < n; i++) { arr[i] = Integer.valueOf(str[i]); min = arr[i] > min ? min : arr[i]; max = arr[i] < max ? max : arr[i]; } if (min == max) { System.out.println("Possible"); return; } int dif = (max - min) / (n - 1); for (int i = 0; i < n; i++) { int tmp = (max - Math.abs(arr[i])) / dif; if (tmp < 0 || tmp >= n) { System.out.println("Impossible"); return; } arr[tmp] = -Math.abs(arr[tmp]); } for (int i : arr) { if (i > 0) { System.out.println("Impossible"); return; } } System.out.println("Possible"); } }
/*排序判断是否差值相同*/ #include <iostream> #include<algorithm> using namespace std; int num[51]; int main() { int n,i,flag=1; cin>>n; for(i=0;i<n;++i) cin>>num[i]; sort(num,num+n); if(n<=2) cout<<"Possible"; if(n>2) { for(i=0;i<n-2;++i) if(num[i]-num[i+1]!=num[i+1]-num[i+2]) {cout<<"Impossible";flag=0;break;} if(flag) cout<<"Possible"; } return 0; }
length, arr = int(input()), sorted(list(map(int, input().split())))
print("Possible" if len(set(map(lambda c: arr[c + 1] - arr[c], range(length - 1)))) == 1 else "Impossible")
import java.util.Scanner; public class Main {//两遍循环,时间复杂度0(n) AC代码 public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); String flag="Possible"; int min=2147483646; int min2=2147483647; int []arr=new int[n]; int []index=new int [n];//空间换时间 int i=0; for(i=0;i<n;i++){//第一遍循环录入数据的同时取第一第二小数 arr[i]=sc.nextInt(); if(arr[i]<min){ min2=min; min=arr[i];} else if(arr[i]<min2)min2=arr[i]; if(i>0){if(arr[i]!=arr[i-1])flag="Impossible";}//所有数全等才直接possible } int d=min2-min;//获取等差值 if(d!=0){ for(i=0;i<n;i++){ if((arr[i]-min)%d!=0)break;//不符合等差 else if((arr[i]-min)/d>=n)break;//等差值超倍 else if(index[(arr[i]-min)/d]==1)break;//数据存在重复 else {index[(arr[i]-min)/d]=1; if(i==n-1)flag="Possible"; } } } System.out.println(flag); } }
给的样例极限长度是50 所以先排序是绝对不会超时的 nlog(n)的复杂度1s内大概能处理n < 10^6-10^7的数量级
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 51;
int arr[maxn], n;
int main(){
cin>>n;
int flag = 1;
for(int i=0;i<n;i++) scanf("%d", &arr[i]);
sort(arr, arr+n);
if(n > 2){
int cha = arr[1] - arr[0];
for(int i=2;i<n;i++){
if(arr[i]-arr[i-1] != cha) {flag=0; break;}
}
}
if(flag) cout<<"Possible"<<endl;
else cout<<"Impossible"<<endl;
}
import java.util.*; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner cin=new Scanner (System.in); int n=cin.nextInt();//数组长度; int a[]=new int[n]; for(int i=0;i<n;i++) { a[i]=cin.nextInt(); } Arrays.sort(a); int left=1; int right=n-1; int temp=a[1]-a[0]; int x=0; while (left<=right) { if(a[left]-a[left-1] !=temp || a[right]-a[right-1] !=temp) { x=1; break; } left++; right--; } if(x==0)System.out.print("Possible"); else if(x==1)System.out.print("Impossible"); } }
import java.util.*; public class Main{ public static void main(String[] args){ try(Scanner in = new Scanner(System.in)){ int n = Integer.parseInt(in.nextLine()),i = 0; int[] a = new int[n]; while(in.hasNextInt()){ a[i++] = in.nextInt(); } System.out.println(helper(a)); } } public static String helper(int[] a){ Arrays.sort(a); int d = a[1] - a[0],i = 2; while(i < a.length){ if(a[i] - a[i - 1] != d) return "Impossible"; i++; } return "Possible"; } }
#!/usr/bin/env python #-*- coding:utf8 -*- def isOK(n, nums): nums = sorted(nums) Max = nums[1] - nums[0] for i in range(1, n): if nums[i] - nums[i-1] != Max: return False return True if __name__ == '__main__': n = input() nums = map(int, raw_input().split()) if isOK(n, nums): print 'Possible' else: print 'Impossible'
java语言。先排序,然后判断相邻元素的差值是否相等。其中排序可以直接使用Arrays.sort()方法, 我这里顺便复习一下快排。有错误欢迎指正,非常感谢!
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.util.Arrays; public class Main{ public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine().trim()); String[] strSeq = br.readLine().trim().split(" "); int[] seq = new int[n]; for(int i = 0; i < n; i++) seq[i] = Integer.parseInt(strSeq[i]); Arrays.sort(seq); int d = seq[1] - seq[0]; for(int i = 2; i < n; i++){ if(seq[i] - seq[i - 1] != d){ System.out.println("Impossible"); return; } } System.out.println("Possible"); } }
解法1:排序,两两比较 #include <iostream> #include <vector> #include <algorithm>2.判断max-min的差值对n-1取余是否为0 若不为0,则不是等差数列 若为0,则继续判断 3.求出数列的差值eql=(max-min)/n-1 若数列为等差数列,则每一项减去最小值对差值eql取余一定等于0 #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n,mid,max=0,min=1000; cin>>n; vector<int> vec; for(int i=0;i<n;i++) { cin>>mid; vec.push_back(mid); } for(int i=0;i<n;i++) { if(vec[i]>max) { max=vec[i]; } else if(vec[i]<min) { min=vec[i]; } } if (max==min) { cout<<"Possible"<<endl ; return 0; } int eq=(max-min)%(n-1); if(eq!=0) { cout<<"Impossible"<<endl ; return 0; } int eql=(max-min)/(n-1); bool flag=true; for(int i=1;i<n;i++) { if((vec[i]-min)%eql!=0) { cout<<"Impossible"<<endl; flag=false; break; } } if(flag) { cout<<"Possible"<<endl; } return 0; }using namespace std; int main() { int n,mid,num; vector<int> eql; cin>>n; num=n; if(n==2) { cout<<"Possible"<<endl; return 0; } while(n--) { cin>>mid; eql.push_back(mid); } sort(eql.begin(),eql.end()); bool eq=true; for(int i=0;i<num-2;i++) { if(eql[i+2]-eql[i+1]!=eql[i+1]-eql[i]) { eq=false; cout<<"Impossible"<<endl; break; } } if(eq==true) { cout<<"Possible"<<endl; } return 0; } 解法2: 思路:1.先求出数列的最大,最小值 max min
mylen = int(input()) L = list(map(int,input().split())) L.sort(reverse = True) cha = L[1]-L[0] flag = True for i in range(1,mylen): if L[i]-L[i-1]!=cha: flag = False break if flag: print("Possible") else: print("Impossible")
##Python n=int(input()) s=list(map(int,input().split())) s.sort() diff=[] for i in range(n-1): diff.append(s[i+1]-s[i]) if len(set(diff))==1: print("Possible") else: print("Impossible")
#include<iostream> #include<algorithm> using namespace std; int main(){ int n; cin>>n; int* a=new int[n]; for(int i=0;i<n;i++) cin>>a[i]; sort(a,a+n); if(n==2) cout<<"Possible"<<endl; else{ bool judge=1; for(int i=0;i<n-2;i++){ if(a[i+1]-a[i]!=a[i+2]-a[i+1]){ judge=0; break; } } if(judge) cout<<"Possible"<<endl; else cout<<"Impossible"<<endl; } }
def pd(new_m,d): for j in range(n-1): if new_m[j]-new_m[j+1]!=d: return 0 return 1 n=int(input()); m=[int(n) for n in input().split()]; new_m=sorted(m,reverse=True); d=new_m[n-2]-new_m[n-1]; c=pd(new_m,d); if c==1: print("Possible") else: print("Impossible")
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main(int argc, char **argv) { int n; cin >> n; vector<int> vec(n); for(int & c: vec) cin >> c; sort(vec.begin(), vec.end()); int diff = vec[0]-vec[1]; auto pos = adjacent_find(vec.begin(), vec.end(), [diff](int a, int b){return diff != (a-b);}); if(pos != vec.end()) cout << "Impossible" << endl; else cout << "Possible" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; while(cin>>n){ int a[n]; for(int i=0;i<n;i++) cin>>a[i]; sort(a,a+n); bool flag = true; for(int i=2,d=a[1]-a[0];i<n;i++) if(a[i]-a[i-1]!=d){ cout<<"Impossible"<<endl; flag = false; break; } if(flag) cout<<"Possible"<<endl; } return 0; }