Codeforces Global Round 19

本弱弱只能A B C,大佬求带

A. Sorting Parts

input

3
3
2 2 1
4
3 1 2 1
5
1 2 2 4 4

output

YES
YES
NO

题意:给你一个数组,问你是否可以按照题目要求操作一次,这个数组不是递增的一个序列

操作要求: 选择一个位置x,把[1,x],[x + 1,n],进行排序

思路:只要找到一对前大后小的数,就能满足要求,或者用已经排好序的数组与原数组对比,不一样的超过两个就是可以的

AC代码

#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<cstdio>
#include<cstring>
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define PII pair<int,int> 
#define PDI pair<double,int> 
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
const int maxn = 1e4+5;
int a[maxn],b[maxn],t,n;
void solve()
{
	sort(b + 1,b + 1 + n);
	int cnt = 0;
	for(int i = 1 ; i <= n ; i++){
		if(a[i] != b[i]) cnt++;
		if(cnt >= 2){
			cout << "YES" << endl;
			return ; 
		}
	}
	cout << "NO" << endl; 
}
int main(){
	IOS;
	cin >> t;
	while(t--){
		cin >> n;
		for(int i = 1 ; i <= n ; i++) cin >> a[i],b[i] = a[i];
		solve();
	}
    return 0;
}

假如知道函数也可直接调用函数(参考官方题解)

#include <bits/stdc++.h>
using namespace std;
 
int main() {
    int t;
    cin >> t;
    for (int i = 0; i < t; i++) {
        int n;
        cin >> n;
        vector<int> a(n);
        for (auto& u : a)
            cin >> u;
        if (!is_sorted(a.begin(), a.end()))
            cout << "YES\n";
        else
            cout << "NO\n";
    }
}

B. MEX and Array

本弱弱没读懂题意,望有大佬指点

官方题解

 官方代码

#include <bits/stdc++.h>
using namespace std;
 
int main() {
    int t;
    cin >> t;
    for (int i = 0; i < t; i++) {
        int n;
        cin >> n;
        vector<int> a(n);
        for (auto& u : a)
            cin >> u;
 
        int ans = 0;
        for (int i = 0; i < n; i++) {
            ans += (i + 1) * (n - i);
            if (a[i] == 0)
                ans += (i + 1) * (n - i);
        }
        cout << ans << '\n';
    }
}

C. Andrew and Stones

 题意: 给你 n 堆石头,给你一种操作规则,把 1 ~ n之间的石头放到 第1堆和第n堆,问最小的操作次数

 

操作规则: 可以选择三堆石头,中间的一堆假如石头的数量大于等于2,就可以向另外的两堆石头分别放置一块

思路:什么情况下是-1(解题关键),可以看看题目给的样例,很明显就是石头个数为1或者为奇数,但是偶数堆的石头可以让1或者奇数堆的石头变成偶数堆,然后循环下去,所以-1的情况只有中间石头个数全为1,或者就是特殊样例,中间只有一堆石头为奇数

AC代码:

#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<cstdio>
#include<cstring>
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define PII pair<int,int> 
#define PDI pair<double,int> 
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
const int maxn = 1e5+5;
int a[maxn],n,t;
void solve()
{
	ll ans = 0;
	int MAX = 0;
	for(int i = 2 ; i < n ; i++) MAX = max(a[i],MAX); 
	if(MAX == 1 || (n == 3 && a[2] % 2 == 1)){
		cout << -1 << endl;
		return;
	}
	for(int i = 2 ; i < n ; i++){
		if(a[i] & 1) ans = ans +  (a[i] + 1) / 2;
		else ans = ans + a[i] / 2;
	}
	cout << ans << endl;
}
int main(){
	IOS;
	cin >> t;
	while(t--){
		cin >> n;
		for(int i = 1 ; i <= n ; i++) cin >> a[i];
		solve();
	}
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值