Codeforces Round #835 (Div. 4)

Problem A   Medium Number

思路:要求根据3个输入找到中位数。先将最大的数找出来,然后输出剩下的两个数中的较大数即可。

#include <iostream>

using namespace std;

int main(){
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int N;
    cin >> N;
    while (N--){
        int a, b, c;
        cin >> a >> b >> c;
        if (a < b) swap(a, b);
        if (a < c) swap(a, c);
        if (c < b)swap(b, c);
        cout << c << '\n';
    }
}

Problem B   Atilla's Favorite Problem

思路:一次遍历,找到最大字母即可。

先介绍一下max_element(elementtype.begin(), elementtype.end())函数,包含于头文件algorithm中。返回最大元素所在的第一个位置的迭代器,如果要访问元素,需要加个星号。编译器里试了vector,string都是可以直接调用这个函数。

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main(){
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int N;
    cin >> N;
    while (N--){
        int _;
        string s;
        cin >> _ >> s;
        cout << (*(max_element(s.begin(), s.end()))) - 'a' + 1 << '\n';
    }
    return 0;
}

Problem C Advantage

思路:很恶心的一个题。找到最大值,并将所有元素减去最大值(除最大值元素),但是如果最大值元素出现的次数超过一次,那么对所有的数都减去最大值。

其中count记录最大值出现的次数,secmaxx用来记录第二大值。

#include <iostream>
#include <algorithm>
#include <vector>
typedef long long ll;
using namespace std;

int main(){
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int N;
    cin >> N;
    while (N--){
        int n, count = 0;
        cin >> n;
        vector<ll> nums(n);
        ll maxx = 0, secmaxx = 0;
        for (int i = 0; i < n; ++i){
            cin >> nums[i];
            if (maxx < nums[i]){secmaxx = maxx; maxx = nums[i];count = 0;}
            else if (secmaxx < nums[i] && nums[i] != maxx){secmaxx = nums[i];}
            if (maxx == nums[i]){++count;}
        }
        if (secmaxx == 0 || count > 1){secmaxx = maxx;}
        for (int i = 0; i < n; ++i){
            if (i > 0){cout << ' ';}
            if (nums[i] != maxx){cout << nums[i] - maxx;}
            else {cout << nums[i] - secmaxx;}
        }
        cout << '\n';
    }
    return 0;
}

Problem D  Challenging Valleys

这个题也要多读一会,不然很容易误解题意。题意的话就是找出一个连续区间,这个区间的长度可以是1~n,而且要求区间左端点下标为0或者左端点的值小于左边一个数的值,也要求右端点的下标为n-1或者右端点的值小于更右边一位的值。问,输入数组里满足这样条件的subarray是否只有一个,是的话输出Yes,不是输出No。

思路:因为题目要求区间内数字连续,而且没有限制区间的最小长度。所以在缓冲区读取输入时,如果当前读取的输入上一个push进来的输入一样,那么可以直接不存储当前输入,这样做的好处是在后面遍历的时候不需要增加额外的判定条件去判断当前区间内的元素是否是连续的,因为题目规定单个数字等于他自己也算连续。然后对输入好的数组从头到尾一次遍历进行判断,并记录满足区间的子数组数目即可。

#include <iostream>
#include <algorithm>
#include <vector>
typedef long long ll;
using namespace std;

int main(){
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int N;
    cin >> N;
    while (N--){
        int n, length = 1, count = 0;
        cin >> n;
        vector<ll> nums(n);
        cin >> nums[0];
        for (int i = 1; i < n; ++i){
            cin >> nums[length];
            if (nums[length] != nums[length - 1]){++length;}
        }
        if (length == 1){cout << "Yes\n";}
        else{
            for (int i = 0; i < length && count < 2; ++i){

                if ((i == 0 || nums[i] < nums[i - 1]) && (i == length - 1 || nums[i] < nums[i + 1])){++count;}
            }
            if (count == 1){cout << "Yes\n";}
            else {cout << "No\n";}
        }

    }
    return 0;
}

Problem E    Binary inversions 

思路:额外建立两个数组,统计当前位置右边出现0的次数和左边出现1的次数,并根据当前位置是0还是1,判断一次operation后是否会使情况更好。比如当前位置是1,一次operation会变0.那么如果他左边1的数目大于右边0的数目,那么这个数就是值得operation的。0的话同理。一共三次遍历,第一次存储0数目数目,第二次存储1数目数组,第三次扫描最优operation带来的增幅,并将增幅添加到结果中输出。


#include <iostream>
#include <vector>
using namespace std;

typedef vector<int> vi;

int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int N;
    cin >> N;
    while(N){
        N -= 1;
        int n, count = 0, maxx = 0;
        long long rst = 0;
        cin >> n;
        vi nums(n), zeros(n), ones(n);
        for (int i = 0; i < n; ++i){cin >> nums[i];}
        for (int i = n - 1; i > -1; --i){
            zeros[i] = count;
            if (nums[i] == 0) count += 1;
            else{rst += count;}
        }
        for (int i = 0, count = 0; i < n; ++i){
            ones[i] = count;
            if (nums[i] == 1){count += 1;}
        }
        for (int i = 0; i < n; ++i){
            if (nums[i] == 0 && zeros[i] > ones[i]){maxx = max(zeros[i] - ones[i], maxx);}
            else if(nums[i] == 1 && ones[i] > zeros[i]){maxx = max(ones[i] - zeros[i], maxx);}
        }
        rst += maxx;
        cout << rst << endl;
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值