算法 【BISHI12】元素方碑 求解代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 public static void main(String[] args)throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer in = new StringTokenizer(br.readLine()); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); int t = Integer.parseInt(in.nextToken()); for(int i=0;i<t;i++){ in = new StringTokenizer(br.readLine()); int n = Integer.parseInt(in.nextToken()); int[] nums = new int[n]; int count = 0; in = new StringTokenizer(br.readLine()); for(int j=0;j<n;j++){ nums[j]=Integer.parseInt(in.nextToken()); count+=nums[j]; } if(count%n!=0){ out.println("NO"); }else{ /** * 问题转化为:能否通过题目允许的操作,将所有偏差值变为0。 */ for(int j=0;j<n;j++){ nums[j]-=(count/n); } /** * 将j-1位置的所有偏差值,全部累加到j+1位置,然后将j-1置为 0; * 说人话就是模拟能量从左向右转移,清空左侧方碑的偏差。 */ for(int j =1;j<n-1;j++){ nums[j+1]+=nums[j-1]; nums[j-1]=0; } boolean flag = true; for(int j=0;j<n;j++){ if(nums[j]!=0){ flag=false; break; } } out.println(flag?"YES":"NO"); } } out.flush(); out.close(); br.close(); }