2024.12.26 周四
Q1. 1100
There is a ribbon divided into $n$ cells, numbered from $1$ to $n$ from left to right. Initially, an integer $0$ is written in each cell.
Monocarp plays a game with a chip. The game consists of several turns. During the first turn, Monocarp places the chip in the $1$-st cell of the ribbon. During each turn except for the first turn, Monocarp does exactly one of the two following actions:
- move the chip to the next cell (i. e. if the chip is in the cell $i$, it is moved to the cell $i+1$). This action is impossible if the chip is in the last cell;
- choose any cell $x$ and teleport the chip into that cell. It is possible to choose the cell where the chip is currently located.
At the end of each turn, the integer written in the cell with the chip is increased by $1$.
Monocarp's goal is to make some turns so that the $1$-st cell contains the integer $c_1$, the $2$-nd cell contains the integer $c_2$, ..., the $n$-th cell contains the integer $c_n$. He wants to teleport the chip as few times as possible.
Help Monocarp calculate the minimum number of times he has to teleport the chip.
------------------------独自思考分割线------------------------
-
思维与观察,递推思想,是有趣的题..
A1.
- 本质就是在求在一个全 $0$ 数组中不断对其连续子串加 $1$至少加多少次才能变成指定数组。
- 观察/递推思维:在全局寻找答案比较难时考虑从开始递推寻找答案。如果画出以下图定会助力思考。
- 出了2个假思路,第三次基本正确比正解复杂点且不知道wa哪里了。
------------------------代码分割线------------------------
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 _()
{int n;cin >> n;vector<int> a(n + 1);for (int i = 1; i <= n; i++)cin >> a[i];int res = -1;for (int i = 1; i <= n; i++)res += a[i] > a[i - 1] ? a[i] - a[i - 1] : 0;cout << res << endl;
}
// void _()
// {
// int n;
// cin >> n;
// vector<int> a(n + 2);
// int cnt = 0;
// for (int i = 1; i <= n; i++)
// {
// int x;
// cin >> x;
// if (i == 1 || x - a[i - 1])
// a[++cnt] = x;
// }
// n = cnt;// map<int, int> has;
// for (int i = 1; i <= n; i++)
// if (a[i] > a[i - 1] && a[i] > a[i + 1])
// has[a[i]]--;
// else if (a[i] < a[i - 1] && a[i] < a[i + 1])
// has[a[i]]++;// cnt = 1;
// int last = 0, res = 0;
// for (auto [x, val] : has)
// {
// res += (x - last) * cnt;
// last = x;
// cnt += val;
// }
// res--;
// cout << res << endl;
// }
// void _()
// {
// int n;
// cin >> n;
// vector<vector<int>> vec;
// vector<int> a(n + 1);
// for (int i = 1; i <= n; i++)
// cin >> a[i];// for (int i = 1; i <= n; i++)
// if (a[i])
// {
// vector<int> t;
// int j = i;
// for (; j <= n && a[j]; j++)
// t.push_back(a[j]);
// i = j - 1;
// vec.push_back(t);
// }
// // bug(vec.size());
// int res = vec.size() - 1;
// for (auto a : vec)
// {
// sort(a.rbegin(), a.rend());
// res += a[0] - 1;
// }
// cout << res << endl;
// }
// void _()
// {
// int n;
// cin >> n;
// int res = 0;
// int cnt = 0, maxw = 0;
// for (int i = 0; i < n; i++)
// {
// int x;
// cin >> x;
// if (x)
// cnt++, maxw = max(maxw, x);
// else
// {
// if (cnt)
// {
// res++;
// res += maxw - 1;
// cnt = 0;
// maxw = 0;
// }
// }
// }
// if (cnt)
// {
// res += maxw - 1;
// cnt = 0;
// maxw = 0;
// }
// cout << res << endl;
// }