A.Tales of a Sort
题目大意
Alphen has an array of positive integers a a a of length n.
Alphen can perform the following operation:
- For all i i i from 1 to n, replace a i a_i ai with max ( 0 , a i − 1 ) \max(0,a_i−1) max(0,ai−1) .
Alphen will perform the above operation until a a a is sorted, that is a a a satisfies a 1 ≤ a 2 ≤ … ≤ a n a_1≤a_2≤…≤a_n a1≤a2≤…≤an. How many operations will Alphen perform? Under the constraints of the problem, it can be proven that Alphen will perform a finite number of operations.
思路
记录最大的逆序对的值即可
#include <bits/stdc++.h>
using namespace std;
const int N = 550;
int a[N];void solve()
{int n;cin >> n;for (int i = 1; i <= n; i++)cin >> a[i];int ans = 0;for (int i = 1; i <= n; i++)for (int j = i + 1; j <= n; j++){if (a[i] > a[j])ans = max(ans, a[i]);}cout << ans << endl;
}
int main()
{int t;cin >> t;while (t--)solve();return 0;
}
B.Good Arrays
题目大意
Let’s call an array of positive integers b of length n good if:
- a i ≠ b i a_i≠b_i ai=bi for all i from 1 t o n 1 \ to \ n 1 to n ,
- a 1 + a 2 + … + a n = b 1 + b 2 + … + b n a_1+a_2+…+a_n=b_1+b_2+…+b_n a1+a2+…+an=b1+b2+…+bn.
思路
这道题将非 1 1 1 的数把值累加到 1 1 1 上面,让自己等于 1 1 1, 然后 1 1 1 变成非 1 1 1 。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
void solve()
{int n;cin >> n;LL cnt1 = 0, sum = 0;for (int i = 1; i <= n; i++){int x;scanf("%d", &x);if (x == 1)cnt1++;elsesum += (x - 1);}if (n == 1){puts("NO");return;}if (sum >= cnt1){puts("YES");return;}else{puts("NO");return;}
}int main()
{int t;cin >> t;while (t--)solve();return 0;
}
C.To Become Max
题目描述
You are given an array of integers a a a of length n n n .
In one operation you:
- Choose an index i i i such that 1 ≤ i ≤ n − 1 1 \le i \le n - 1 1≤i≤n−1 and a i ≤ a i + 1 a_i \le a_{i + 1} ai≤ai+1 .
- Increase a i a_i ai by 1 1 1 .
Find the maximum possible value of max ( a 1 , a 2 , … a n ) \max(a_1, a_2, \ldots a_n) max(a1,a2,…an) that you can get after performing this operation at most k k k times.
输入格式
Each test contains multiple test cases. The first line of input contains a single integer t t t ( 1 ≤ t ≤ 100 1 \le t \le 100 1≤t≤100 ) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers n n n and k k k ( 2 ≤ n ≤ 1000 2 \le n \le 1000 2≤n≤1000 , 1 ≤ k ≤ 1 0 8 1 \le k \le 10^{8} 1≤k≤108 ) — the length of the array a a a and the maximum number of operations that can be performed.
The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1,a_2,\ldots,a_n a1,a2,…,an ( 1 ≤ a i ≤ 1 0 8 1 \le a_i \le 10^{8} 1≤ai≤108 ) — the elements of the array a a a .
It is guaranteed that the sum of n n n over all test cases does not exceed 1000 1000 1000 .
输出格式
For each test case output a single integer — the maximum possible maximum of the array after performing at most $ k $ operations.
样例 #1
样例输入 #1
6
3 4
1 3 3
5 6
1 3 4 5 1
4 13
1 1 3 179
5 3
4 3 2 2 2
5 6
6 5 4 1 5
2 17
3 5
样例输出 #1
4
7
179
5
7
6
提示
In the first test case, one possible optimal sequence of operations is: [ 1 , 3 , 3 ] → [ 2 , 3 , 3 ] → [ 2 , 4 , 3 ] → [ 3 , 4 , 3 ] → [ 4 , 4 , 3 ] [\color{red}{1}, 3, 3] \rightarrow [2, \color{red}{3}, 3] \rightarrow [\color{red}{2}, 4, 3] \rightarrow [\color{red}{3}, 4, 3] \rightarrow [4, 4, 3] [1,3,3]→[2,3,3]→[2,4,3]→[3,4,3]→[4,4,3] .
In the second test case, one possible optimal sequence of operations is:
[ 1 , 3 , 4 , 5 , 1 ] → [ 1 , 4 , 4 , 5 , 1 ] → [ 1 , 5 , 4 , 5 , 1 ] → [ 1 , 5 , 5 , 5 , 1 ] → [ 1 , 5 , 6 , 5 , 1 ] → [ 1 , 6 , 6 , 5 , 1 ] → [ 1 , 7 , 6 , 5 , 1 ] [1, \color{red}{3}, 4, 5, 1] \rightarrow [1, \color{red}{4}, 4, 5, 1] \rightarrow [1, 5, \color{red}{4}, 5, 1] \rightarrow [1, 5, \color{red}{5}, 5, 1] \rightarrow [1, \color{red}{5}, 6, 5, 1] \rightarrow [1, \color{red}{6}, 6, 5, 1] \rightarrow [1, 7, 6, 5, 1] [1,3,4,5,1]→[1,4,4,5,1]→[1,5,4,5,1]→[1,5,5,5,1]→[1,5,6,5,1]→[1,6,6,5,1]→[1,7,6,5,1]
思路
我们二分答案,下界设为 0 0 0,而 max ( a 1 , … a n ) + k \max(a_1,…a_n)+k max(a1,…an)+k 作为上界。
设 b b b 是 a a a 在执行 k k k 次操作后的数组。假设对于一个数 x x x,我们想要检查是否可以在 k k k 次操作中得到 max ( b 1 , … b n ) ≥ x \max(b_1,…b_n)≥x max(b1,…bn)≥x。也就是说,通过 k k k 次操作使得存在某个索引 i i i 使得 b i ≥ x b_i≥x bi≥x 。
因此,让我们从遍历一下 b b b 数组 ,看看是否可以在 k k k 次操作中使得 b i ≥ x b_i≥x bi≥x 。
设 f ( i , y ) f(i,y) f(i,y) 为使得 b i ≥ y b_i≥y bi≥y 所需的最小操作数。那么有:
f ( i , y ) = 0 , 对于所有 y ≤ a i f ( i , y ) = y − a i + f ( i + 1 , y − 1 ) ,对于所有 1 ≤ a i f ( i , y ) = + ∞ ,对于 i = n 且所有 y > a i 。 f(i,y)=0,对于所有y≤ai\\f(i,y)=y−a_i+f(i+1,y−1),对于所有1≤a_i\\f(i,y)=+∞,对于i=n且所有y>a_i。 f(i,y)=0,对于所有y≤aif(i,y)=y−ai+f(i+1,y−1),对于所有1≤aif(i,y)=+∞,对于i=n且所有y>ai。
显然计算 f ( i , x ) f(i,x) f(i,x) 最坏情况下需要 O ( n ) O(n) O(n) 的时间。因此,我们看看 ∑ i = 1 n f i , x ≤ k \sum _{i=1}^{n}f_{i,x}≤k ∑i=1nfi,x≤k
如果操作次数 ≤ k ≤k ≤k,那么在 k k k 次操作中可能存在某个 b i ≥ x b_i≥x bi≥x,并在更新当前答案后让 l = m i d l=mid l=mid。否则这个 x x x 没有办法满足,让 r = m i d r=mid r=mid。复杂度: O ( n 2 log A ) O(n^2\log A) O(n2logA),其中 A A A 是 a i a_i ai 和 k k k 的最大可能值。
我们这里再来看
一个优化空间的思路,因为我们只需要计算所有 f f f 值的和,所以 O(1) \text{O(1)} O(1) 空间复杂度就可以了。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2005;
vector<ll> a(N);
ll n, k;
bool check(ll x)
{for (int i = 1; i <= n; i++){vector<ll> minneed(n + 1);minneed[i] = x;ll ut = 0;for (int j = i; j <= n; j++){if (minneed[j] <= a[j])break;if (j == n){ut = k + 1;break;}ut += minneed[j] - a[j];minneed[j + 1] = max(0ll, minneed[j] - 1);}if (ut <= k)return true;}return false;
}void solve()
{cin >> n >> k;ll maxnum = 0;for (int i = 1; i <= n; i++){cin >> a[i];maxnum = max(maxnum, a[i]);}// cout << *max_element(a.begin(), a.end()) + k + 1 << endl;ll l = -1, r = maxnum + k + 1, ans = 0;// cout << r << endl;while (l + 1 < r){ll mid = (l + r) >> 1;if (check(mid))ans = mid, l = mid;elser = mid;}cout << l << endl;
}int main()
{int t;cin >> t;while (t--)solve();return 0;
}