【NOIP2018pj】题解
\(T1\)
题面
题解
好像并没有什么好说的。。。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
using namespace std;
string s;
int main () {
getline(cin, s);
int ans = 0;
for (int i = 0, l = s.size(); i < l; i++) {
if (isdigit(s[i]) || ('a' <= s[i] && s[i] <= 'z') || ('A' <= s[i] && s[i] <= 'Z')) ans++;
}
printf("%d\n", ans);
return 0;
}
\(T2\)
题面
题解
先把每一边贡献算出来再扫一遍即可
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
inline int gi() {
register int data = 0, w = 1;
register char ch = 0;
while (ch != '-' && !isdigit(ch)) ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar();
return data * w;
}
#define MAX_N 100005
int N, a[MAX_N];
int m, p1, s1, s2;
typedef long long ll;
ll Left = 0, Right = 0, dlt = 0;
int ans = 0;
int main () {
N = gi();
for (int i = 1; i <= N; i++) a[i] = gi();
m = gi(), p1 = gi(), s1 = gi(), s2 = gi();
a[p1] += s1;
for (int i = 1; i < m; i++) Left += 1ll * a[i] * (m - i);
for (int i= m + 1; i <= N; i++) Right += 1ll * a[i] * (i - m);
ans = m; dlt = abs(Left - Right);
for (int i = 1; i < m; i++) {
ll res = abs(Left + 1ll * s2 * (m - i) - Right);
if (res < dlt) ans = i, dlt = res;
else if (res == dlt) ans = min(i, ans);
}
for (int i = m + 1; i <= N; i++) {
ll res = abs(Right + 1ll * s2 * (i - m) - Left);
if (res < dlt) ans = i, dlt = res;
else if (res == dlt) ans = min(i, ans);
}
printf("%d\n", ans);
return 0;
}