Educational Codeforces Round 91 (Rated for Div. 2)题解

A. Three Indices

题目链接

https://codeforces.com/contest/1380/problem/A

思路

直接遍历数组找到一个值比它左右的值都大的即可
原因:如果该值存在,则该点必定为最大值,左右的值至少有一个比它小

代码

#include<cstdio>
#include<iostream>
#include<math.h>
using namespace std;
#define INF 0x3f3f3f3f
int main(){
    int t;
    cin>>t;
    while(t--){
        int n, a[1009]={0}, ind=-1;
        cin>>n;
        for(int i=1; i<=n; i++)
        {
            cin>>a[i];
        }
        for(int i=2; i<n; i++){
            if(a[i]>a[i-1]&&a[i]>a[i+1])
                ind=i;
        }
        if(ind==-1)
            cout<<"NO"<<endl;
        else
        {
            cout<<"YES"<<endl;
            printf("%d %d %d\n", ind-1, ind, ind+1);
        }
    }
    return 0;
}

B. Universal Solution

题目链接

https://codeforces.com/contest/1380/problem/B

思路

从pos的位置将字符串遍历一圈,下次从pos+1开始,一共有len(s)种情况
很明显每个位置的情况正好是s,所以直接找出s中最多的那个,找出赢它的情况,并且输出len(s)次即可
在这里插入图片描述

代码

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<string>
#include<math.h>
#include<algorithm>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        string s;
        cin>>s;
        int ans1=0, ans2=0, ans3=0;
        for(int i=0; i<s.length(); i++){
            if(s[i]=='R')
                ans1++;
            else if (s[i]=='S')
                ans2++;
            else if (s[i]=='P')
                ans3++;
        }
        int mx=max(ans1, max(ans2, ans3));
        if(mx==ans1)
            for(int i=0; i<s.length(); i++)
                cout<<"P";
        else if (mx==ans2)
            for(int i=0; i<s.length(); i++)
                cout<<"R";
        else if (mx==ans3)
            for(int i=0; i<s.length(); i++)
                cout<<"S";
        cout<<endl;
    }
    return 0;
}

C. Create The Teams

题目链接

https://codeforces.com/contest/1380/problem/C

思路

将数组按照从大到小的顺序排序,然后遍历数组,如果该数字自己就可以组成团队,ans++,
如果不行,则安排进下一个团队

代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include<algorithm>
using namespace std;
bool cmp(int a, int b){
    return a>b;
}
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, x, a[100009]={0};
        cin>>n>>x;
        for(int i=0; i<n; i++)
            cin>>a[i];
        sort(a,a+n,cmp);
        int ans=0, sum=0;
        for(int i=0; i<n; i++){
            if(a[i]>x)
                ans++;
            else{
                sum++;
                if(a[i]*sum>=x){
                    sum=0;
                    ans++;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

D. Berserk And Fireball

题目链接

https://codeforces.com/contest/1380/problem/D

思路

分别对需要删除的每个区间进行操作
求得删除区间的最大值,判断其能否被删除
1、区间长度大于等于k,
2、最大值相对于删除区间的左右值其中一个较小
再分别讨论每组情况下的最优解

代码

#include <bits/stdc++.h>
using namespace std;
long long n, m, x, k, y, i, j, mx, len, edge, c;
long long a[200009], b[200009];
int main()
{
    cin >> n >> m;
    cin >> x >> k >> y;
    for (i = 1; i < n + 1; i++)
        cin >> a[i];
    for (i = 0; i < m; i++)
        cin >> b[i];
    a[0] = 0;
    a[n + 1] = 0;                                             //防止数组越界
    for (i = 1; i < n + 2; i++)
    {
        mx = max(mx, a[i]);                                   //mx--区间最大值
        if (a[i] == b[j])
        {
            len = i - edge - 1;                               //len--区间长度, egde--区间左边的值(不是被删除的值
            if (mx <= a[i] || mx <= a[edge])                  //区间最大值可以通过方法2消除,并选择耗能最小
            {
                if (x <= y * k)
                    c = c + (len / k) * x + (len % k) * y;
                else
                    c = c + len * y;
            }
            else if (len < k)                                 //区间最大值不能被消除
            {
                cout << -1;
                return 0;
            }
            else                                              //区间最大值只能通过方法1消除,至少执行一次1消除最大值
            {
                if (x <= y * k)
                    c = c + (len / k) * x + (len % k) * y;
                else
                    c = c + x + (len - k) * y;
            }
            j++;
            mx = a[i];
            edge = i;
        }
    }
    if (j != m+1)
    {
        cout << "-1";
        return 0;
    }
    cout << c;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xmyrzb/article/details/107336111
今日推荐