视频链接: Educational Codeforces Round 165 (Rated for Div. 2) B题_哔哩哔哩_bilibili
讲解都在视频里哦
#include<bits/stdc++.h>
using namespace std;
int main()
{
int T;
cin >> T;
while (T--)
{
int n;
cin >> n;
bool flag = false;
vector<int> a(n);
for (auto &i : a)
{
cin >> i;
i--;
}
for (int i = 0; i < n; i++)
if (a[a[i]] == i)
flag = true;
if (flag)
cout << 2 << endl;
else
cout << 3 << endl;
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main()
{
int T;
cin >> T;
while (T--)
{
string ss;
cin >> ss;
long long ans = 0;
int l = 0, r = 0;
bool f = false;
while (r < ss.size())
{
if (ss[r] == '1')
{
if (f == false)
{
f = true;
l = r;
}
}
else
{
if (f == true)
{
ans += r - l + 1;
l++;
ss[r] = '1';
}
}
r++;
}
cout << ans << endl;
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf LLONG_MAX
main()
{
ll T;
cin >> T;
while (T--)
{
ll n, k;
cin >> n >> k;
vector<ll> a(n);
for (auto &i : a)
cin >> i;
vector<vector<ll>> dp(n + 1, vector<ll>(k + 1, inf));
for (int i = 0; i <= k; i++)
dp[0][i] = 0;
for (int i = 1; i <= n; i++)
for (int j = 0; j <= k; j++)
{
ll minNum = a[i - 1];
for (int c = 0; c <= j && i - c > 0; c++)
{
minNum = min(minNum, a[i - 1 - c]);
dp[i][j] = min(dp[i][j], dp[i - c - 1][j - c] + (c + 1) * minNum);
}
}
cout << dp[n][k] << endl;
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int T;
cin >> T;
while (T--)
{
ll n, k, ans = 0;
cin >> n >> k;
vector<pair<ll, ll>> a(n);
vector<ll> prefix(n);
for (auto &i : a)
cin >> i.second;
for (auto &i : a)
cin >> i.first;
sort(a.begin(), a.end());
for (int i = 0; i < n; i++)
{
prefix[i] = a[i].first - a[i].second;
if (prefix[i] < 0)
prefix[i] = 0;
if (i != 0)
prefix[i] += prefix[i - 1];
}
multiset<ll> ms;
ll sum = 0;
for (int i = n - 1; i >= 0; i--)
{
ms.insert(a[i].second);
sum += a[i].second;
if (ms.size() > k)
if (a[i].second < *ms.rbegin())
sum -= *ms.rbegin(), ms.erase(--ms.end());
else
sum -= a[i].second, ms.erase(ms.find(a[i].second));
if (ms.size() == k)
ans = max((i == 0 ? 0 : prefix[i - 1]) - sum, ans);
}
if (k == 0)
ans = max(prefix[n - 1], ans);
cout << ans << endl;
}
return 0;
}