2024.12.4 週三
Q1. 1000
給定01串,操作:選擇l,r,將s[r]放到s[l]前:s[l]s[l+1]...s[r-1]s[r]->s[r]s[l]s[l+1]...s[r-1],代價為r-l+1/區間長度。
問最小代價將01串由小到大排序。
Q2. 1300
給定2行'<''>'組成的字串,起點[1,1],可選4個方向走一步,然後必須根據所在字元走一步。
問是否可到達[2,m]。
Q3. 1300
給定n,m。n個人在排隊,你在n+1的位置。操作:你在i,與前面的j交換位置,代價a[j]+b[j+1]+..+b[i-1]。
問最小代價使你排到前m。
Q4. 1400
給定n,k,a陣列。i:1~n卡牌數字i的數量為a[i]。你可以購買k張任意數字的卡牌。
問排列後可構成1~n的排列的連續子陣列的最大數量。
------------------------獨自思考分割線------------------------
-
這次的4道題算是比較順,Q2 Q3都是20分鐘左右,Q4近一個小時,主要是在對2種答案的討論上費了時間,最後發現殊途同歸。
A1.
兩點:1.發現最終必須000011111,必要操作就是將後面的0提到1之前。
2.發現最小代價就是對最近的0操作,操作次數就是前面1的個數。即對每個0:res+=pre_1+1。
A2.
這裡發現bfs可做就直接bfs了,當然也有其他巧妙的做法。
A3.
兩點:1.發現本質就是在1~m選一個a[i],中間的話選min(a[i],b[i])最優。
2.操作就是m+1n:選min(a[i],b[i]),m1列舉作為終點的位置,倒序列舉可簡化當其不為終點時取min(a[i],b[i])。
A4.
4點:1.貪心構造出最多排列的方式:1234123412 且數量取決於最少數量牌的個數
2.二分找出貪心購買後最少數量,列舉一次找到最少數量牌的個數。
3.設最少數量為k,其個數為cnt,,發現答案就是n*(k-1)+1+n-cnt;
4.二分的時候可能爆long long,開int128。(重測了一次,l開1e12確實有問題,r需要開到1e13,long long也沒問題)
------------------------程式碼分割線------------------------
A1.
#include <bits/stdc++.h>
#define int long long //
#define endl '\n' // 互動/除錯 關
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
void _();
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cout << fixed << setprecision(6);
int T = 1;
cin >> T;
while (T--)
_();
return 0;
}
void _()
{
string s;
cin >> s;
int pre = 0, res = 0;
for (auto v : s)
if (v == '0')
{
if (pre)
res += pre + 1;
}
else
pre++;
cout << res << endl;
}
A2.
#include <bits/stdc++.h>
#define int long long //
// #define endl '\n' // 互動/除錯 關
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
void _();
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cout << fixed << setprecision(6);
int T = 1;
cin >> T;
while (T--)
_();
return 0;
}
void _()
{
int n = 2, m;
cin >> m;
vector<string> a(n + 1);
for (int i = 1; i <= n; i++)
{
cin >> a[i];
a[i] = " " + a[i];
}
vector<vector<int>> vis(n + 1, vector<int>(m + 1));
auto bfs = [&]()
{
queue<pair<int, int>> q;
vis[1][1] = 1;
q.push({1, 1});
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
while (q.size())
{
auto [x, y] = q.front();
q.pop();
for (int i = 0; i < 4; i++)
{
int nx = x + dx[i], ny = y + dy[i];
if (nx < 1 || nx > 2 || ny < 1 || ny > m || vis[nx][ny])
continue;
vis[nx][ny] = 1;
// bug2(nx, ny);
if (a[nx][ny] == '<')
ny--;
else
ny++;
if (nx < 1 || nx > 2 || ny < 1 || ny > m || vis[nx][ny])
continue;
// bug2(nx, ny);
vis[nx][ny] = 1;
q.push({nx, ny});
}
}
};
bfs();
cout << (vis[n][m] ? "YES" : "NO") << endl;
}
A3.
#include <bits/stdc++.h>
#define int long long //
#define endl '\n' // 互動/除錯 關
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
void _();
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cout << fixed << setprecision(6);
int T = 1;
cin >> T;
while (T--)
_();
return 0;
}
void _()
{
int n, m;
cin >> n >> m;
vector<int> a(n + 1), b(n + 1), pre(n + 1);
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
{
cin >> b[i];
pre[i] = pre[i - 1] + b[i];
}
int s = 0, cnt = 0;
for (int i = m + 1; i <= n; i++)
s += min(a[i], b[i]);
int res = 1e18;
int has = 0;
for (int i = m; i; i--)
{
res = min(res, s + a[i]);
s += min(a[i], b[i]);
}
cout << res << endl;
}
A4.
#include <bits/stdc++.h>
// #define int long long //
#define int __int128
#define endl '\n' // 互動/除錯 關
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
void _();
int re()
{
int s = 0, f = 1;
char ch = getchar();
while (ch > '9' || ch < '0')
{
if (ch == '-')
f = -1;
ch = getchar();
}
while ('0' <= ch && ch <= '9')
s = s * 10 + ch - 48, ch = getchar();
return s * f;
}
void wr(int s)
{
if (s < 0)
putchar('-'), s = -s;
if (s > 9)
wr(s / 10);
putchar(s % 10 + 48);
}
void wr(int s, char ch) { wr(s), putchar(ch); }
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cout << fixed << setprecision(6);
int T = 1;
T = re();
while (T--)
_();
return 0;
}
void _()
{
int n, k;
n = re(), k = re();
vector<int> a(n);
for (int &x : a)
x = re();
auto ok = [&](int x)
{
int res = 0;
for (auto v : a)
if (x > v)
res += x - v;
return res <= k;
};
int l = 0, r = 1e13;
while (r - l - 1)
{
int mid = l + r >> 1;
if (ok(mid))
l = mid;
else
r = mid;
}
int kk = k;
int cnt = 0;
for (auto v : a)
if (l >= v)
kk -= l - v, cnt++;
cnt -= kk;
// bug2(l, cnt);
int res = n * (l - 1) + 1 + n - cnt;
// if (l * n == n + k)
// bug(1);
// res = n * (l - 1) + 1;
// if (!k)
// res = n * l - n - 1;
wr(res, '\n');
}