#include<bits/stdc++.h> usingnamespacestd; constint MAX = 1e5 + 10; int a[MAX]; intmain() { int n, temp; int s[6] = { 0 }; longlong res = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &temp); s[temp]++; } a[0] = 0; temp = 1; while (s[4] != 0 && s[0] != 0) { //先对4和0进行排列 a[temp++] = 4; a[temp++] = 0; s[4]--; s[0]--; } if (s[0] != 0) { //如果还剩下0 while (s[2] != 0 && s[0] != 0) { //2和0进行排列 a[temp++] = 2; a[temp++] = 0; s[2]--; s[0]--; } while (s[2] != 0) { //还剩下2 a[temp++] = 2; s[2]--; } while (s[0] != 0) { //还剩下0 a[temp++] = 0; s[0]--; } } elseif (s[4] != 0) { //如果还剩下4 while (s[4] != 0 && s[2] != 0) { //对4和2进行排列 a[temp++] = 4; a[temp++] = 2; s[2]--; s[4]--; } while (s[4] != 0) { //还剩下4 a[temp++] = 4; s[4]--; } while (s[2] != 0) { //还剩下2 a[temp++] = 2; s[2]--; } } else { while (s[2] != 0) { //只剩下2 a[temp++] = 2; s[2]--; } } for (int i = 0; i < temp - 1; i++) res += (a[i + 1] - a[i])*(a[i + 1] - a[i]); cout<<res; return0; }
这里看标程的时候看到一个技巧,使用getchar()来统计204的数量
1 2 3 4 5 6
inline int read() { char c = getchar(); int x = 0, f = 1; while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; }