[比赛链接](https://codeforces.com/contest/1923)
A
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int,int>
void solve()
{
int n;cin >> n;
vector<int>a(n + 1);
for (int i = 1;i <= n;i++)cin >> a[i];
int p1 = 0;
for (int i = 1;i <= n;i++)if (a[i] == 1) {
p1 = i;
break;
}
int p2;
for (int i = n;i >= 1;i--)if (a[i] == 1) {
p2 = i;
break;
}
int cnt = 0;
for (int i = p1;i <= p2;i++) {
if (a[i] == 0)cnt++;
}
cout << cnt << '\n';
}
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t;cin >> t;
while (t--) {
solve();
}
return 0;
}
B
把负位置的合并到正位置上
前缀和累加血量,避免计算射击单个单位伤害溢出问题
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int,int>
void solve()
{
int n, k;cin >> n >> k;
vector<int>a(n + 1), b(n + 1);
for (int i = 1;i <= n;i++)cin >> a[i];
for (int i = 1;i <= n;i++)cin >> b[i];
vector<int>mv(n + 1, 0);
for (int i = 1;i <= n;i++)mv[abs(b[i])] += a[i];
for (int i = 1;i <= n;i++)mv[i] += mv[i - 1];
for (int i = 1;i <= n;i++) {
if (i * k < mv[i]) {
cout << "NO" << '\n';
return;
}
}
cout << "YES" << '\n';
}
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t;cin >> t;
while (t--) {
solve();
}
return 0;
}
C
思路
没要求输出过程,只是判断就行
令所有的
a
[
i
]
=
1
a[i]=1
a[i]=1,
b
[
i
]
=
a
[
i
]
+
1
b[i]=a[i]+1
b[i]=a[i]+1,其余
b
[
i
]
b[i]
b[i]为
1
1
1
出来两个和,若
a
a
a大,随便找一个
b
[
i
]
=
a
[
i
]
+
1
b[i]=a[i]+1
b[i]=a[i]+1的加上
s
u
m
a
−
s
u
m
b
suma-sumb
suma−sumb就行,若
b
b
b大,不合法
ACcode
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int,int>
void solve()
{
int n, q;cin >> n >> q;
vector<int>a(n + 1, 0);
for (int i = 1;i <= n;i++)cin >> a[i], a[i]--;
vector<int>zero(n + 1, 0);
for (int i = 1;i <= n;i++)zero[i] += zero[i - 1] + (a[i] == 0);
vector<int>sum(n + 1, 0);
for (int i = 1;i <= n;i++)sum[i] += sum[i - 1] + a[i];
int le, ri;
while (q--) {
cin >> le >> ri;
if (le == ri) {
cout << "NO" << '\n';
continue;
}
int p = sum[ri] - sum[le - 1];//a的总数
if (p < zero[ri] - zero[le - 1]) cout << "NO" << '\n';
else cout << "YES" << '\n';
}
}
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t;cin >> t;
while (t--) {
solve();
}
return 0;
}