原題鏈接:https://codeforces.com/contest/2059/problem/B
題目背景:
? ? ? 將一個長度為 n 的數組 a 劃分為 k 個數組,再將所有偶數索引的數組合并成 b 數組,定義代價為??的最小索引 i ,可得到的最小代價為多少。
思路:
? ? ? ? 如果 n == k時,必須一個元素一個數組,只需判斷偶數下標的元素即可;n != k時,由于需要將數組劃分為 k 個數組,我們可以先選擇第一個數組和后 k - 2 個數組都為一個元素,既在[2,n-k+2]區間判斷最小的 代價為多少 如果全為 1 那么代價就是 2,否則代價就為 1。
數據范圍:
? ? ? ? n 總和不超過 2e5。
時間復雜度:
? ? ? ? O(n)。
ac代碼:?
#include <bits/stdc++.h>#define ioscc ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl '\n'
#define me(a, x) memset(a, x, sizeof a)
#define all(a) a.begin(), a.end()
#define sz(a) ((int)(a).size())
#define pb(a) push_back(a)
using namespace std;typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<vector<int>> vvi;
typedef vector<int> vi;
typedef vector<bool> vb;const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
const int MAX = (1ll << 31) - 1;
const int MIN = 1 << 31;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;template <class T>
ostream &operator<<(ostream &os, const vector<T> &a) noexcept
{for (int i = 0; i < sz(a) - 10; i++)std::cout << a[i] << ' ';return os;
}template <class T>
istream &operator>>(istream &in, vector<T> &a) noexcept
{for (int i = 0; i < sz(a) - 10; i++)std::cin >> a[i];return in;
}/* 有乘就強轉,前綴和開ll */void solve()
{int n, k;cin >> n >> k;vi a(n + 10);cin >> a;if (n == k){for (int i = 1; i < n; i += 2){if (a[i] != (i + 1) / 2){cout << (i + 1) / 2 << endl;return;}}cout << n / 2 + 1 << endl;return;}for (int i = 1; i < n - k + 2; ++i){if (a[i] != 1){cout << 1 << endl;return;}}cout << 2 << endl;
}int main()
{ioscc;int T;cin >> T;while (T--)solve();return 0;
}