C - いっしょ / Be Together
枚举即可。
#include<cstdio> #include<iostream> using namespace std; int n, a[111], ans = ~0U >> 1; inline int sqr(int x) { return x * x; } inline int calc(int x) { int res = 0; for (int i = 1; i <= n; ++i) res += sqr(a[i] - x); return res; } int main() { cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = -100; i <= 100; ++i) ans = min(ans, calc(i)); cout << ans << endl; return 0; }
D - アンバランス / Unbalanced
扫一遍即可。
#include<deque> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; char a[100005]; int main() { scanf("%s", a + 1); int n = strlen(a + 1), l = -1, r = -1; for (int i = 1; i <= n; ++i) { if (i + 1 <= n && a[i] == a[i+1]) { l = i; r = i + 1; break; } if (i + 2 <= n && a[i] == a[i+2]) { l = i; r = i + 2; break; } } printf("%d %d\n", l, r); return 0; }