CoreForce Round #890 div.2 Part.A

SortA. Tales of a Sort

time limit per test 1 second
memory limit per test 256 megabytes
inputstandard input
outputstandard output
Alphen has an array of positive integers a
 of length n
.

Alphen can perform the following operation:For all i from 1 to n, replace ai with max(0,ai−1).
Alphen will perform the above operation until a is sorted, that is a satisfies 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.

Input
Each test contains multiple test cases. The first line of input contains a single integer t
 (1≤t≤500
) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer n
 (2≤n≤50
) — the length of the array a
.

The second line of each test case contains n
 integers a1,a2,…,an
 (1≤ai≤109
) — the elements of the array a
.

Output
For each test case, output a single integer — the number of operations that Alphen will perform.

Example
input
7
3
1 2 3
5
2 1 2 1 2
4
3 1 5 4
2
7 7
5
4 1 3 2 5
5
2 3 1 4 5
3
1000000000 1 2


output
0
2
5
0
4
3
1000000000


Note
In the first test case, we have a=[1,2,3]. Since a is already sorted, Alphen will not need to perform any operations. So, the answer is 0.

In the second test case, we have a=[2,1,2,1,2]. Since a is not initially sorted, Alphen will perform one operation to make a=[1,0,1,0,1]. After performing one operation, a is still not sorted, so Alphen will perform another operation to make a=[0,0,0,0,0]. Since a
 is sorted, Alphen will not perform any other operations. Since Alphen has performed two operations in total, the answer is 2.each operation is to turn ai into max(0,ai-1)operation is to make sure to change each element

1,how to identify the array is sorted?
2,how to make sure that 

first idea
what we need to know is only that the element is alive or not.
剪枝:初始数组是否sorted?
else
整体-1,直至出现0
剪枝:寻找不合相邻元素
时间复杂度分析:500*50*2*1e9 必然超时
手动模拟数据:0 0 1 2 5 5 6


最终解法:逆序对


原先的思路有什么问题:
判断函数不好写,或者说时间复杂度一定很高,O(n)的判断成本虽然不大,但是也够呛,每组测试数据的时间复杂度为O(n^2)(200*50*2*1e9)
 

模拟超时算法代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>

using namespace std;

typedef long long ll;
typedef pair<int,int> PII;
const int N = 1e5+10;
int a[N];

bool sort(int n) {
    for(int i = 1;i<n;i++) {
        if(a[i-1]>a[i]) return false;
    }
    return true;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--) {
        int n;
        cin>>n;
        for(int i = 0;i<n;i++) cin>>a[i];
        
        int ans = 0;
        while(!sort(n)) {
            for(int i = 0;i<n;i++) {
                a[i] = max(a[i]-1,0);
            }
            ans++;
        }
        cout<<ans<<endl;
    } 
    return 0;
}


Right solution

求最大逆序对

cpp

#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>

using namespace std;

typedef long long ll;
typedef pair<int,int> PII;
const int N = 1e5+10;
int a[N];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--) {
        int n;
        cin>>n;
        int ans = 0;
        for(int i = 0;i<n;i++) cin>>a[i];
        for(int i = 0;i<n-1;i++) {
            for(int j = i+1;j<n;j++) {
                if(a[i]>a[j]) {
                    ans = max(ans,a[i]);
                }
            }
        }
        cout<<ans<<endl;
    } 
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值