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

A. Three Indices

Educational Codeforces Round 91 (Rated for Div. 2) 题解_第1张图片
input:

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

output:

YES
2 3 4
YES
3 5 6
NO

题意: 给一串从1到n序列,要求找到i,j,k使得在这里插入图片描述
若能满足则输出YES,并输出i,j,k,若找不到满足条件的三个数则输出NO。
思路: 对于每个序列,只需满足a[i]>a[i-1]&&a[i]>a[i+1]就一定有解,所以只需判断序列中是否有一个三元组满足这个条件即可。
代码:

#include
using namespace std;

int a[1005];

int main() {
    int t;
    cin>>t;
    while(t--){
        int n,flag=0;
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        for(int i=1;i<n-1;i++){
            if(a[i]>a[i-1]&&a[i]>a[i+1]){
                flag=1;
                cout<<"YES"<<endl;
                cout<<i<<" "<<i+1<<" "<<i+2<<endl;
                break;//只需找出一组即可
            }
        }
        if(!flag)
            cout<<"NO"<<endl;
    }
}

B. Universal Solution

Educational Codeforces Round 91 (Rated for Div. 2) 题解_第2张图片
input:

3
RRRR
RSP
S

output:

PPPP
RSP
R

题意: 石头剪刀布游戏中,假设已经知道对手出手的顺序,但是不知道会从哪个位置开始出,求怎么出能使平均胜利场次最多,如果有多种解,输出其中任意一种。
思路: 本题为简单的贪心,只需算出对手最多出的是什么,然后全出能胜它的,这样的出法能保证平均胜利场次最多。
代码:

#include
using namespace std;

int main() {
    int t;
    cin>>t;
    while(t--) {
        string s;
        cin>>s;
        int len=s.size(),c1=0,c2=0,c3=0;
        for(int i=0; i<len; i++) {
            switch(s[i]) {
            case 'R':
                c1++;
                break;
            case 'S':
                c2++;
                break;
            case 'P':
                c3++;
                break;
            }
        }
        char ch;
        if(c1>=c2&&c1>=c3)//注意比较的时候要加等号,否则c1=c2>c3的情况会出错
            ch='P';
        else if(c2>=c1&&c2>=c3)
            ch='R';
        else
            ch='S';
        for(int i=0; i<len; i++)
            cout<<ch;
        cout<<endl;
    }
}

C. Create The Teams

Educational Codeforces Round 91 (Rated for Div. 2) 题解_第3张图片
input

3
5 10
7 11 2 9 5
4 8
2 4 2 3
4 11
1 3 3 7

output

2
1
0

题意: 给出n个程序员及其他们的技能值,要将他们划分成团队,每个团队都有一个限制:团队中程序员的数量乘以团队中所有程序员的最低技能必须至少为x,要将他们划分成尽可能多个团队,注意,每个人最多进一个团队,也可以有人不进团队。
思路: 将数据从大到小排列出来,然后从大到小取满足条件的即可。
代码:

#include
using namespace std;

long long a[100005];

bool cmp(long long a,long long b) {
    return a>b;
}

int main() {
    int t;
    cin>>t;
    while(t--) {
        int n;
        long long x,ans=0,cnt=1;
        cin>>n>>x;
        for(int i=0; i<n; i++)
            cin>>a[i];
        sort(a,a+n,cmp);//从大到小进行排序
        for(int i=0; i<n; i++) {
            if(a[i]*cnt>=x) {
                ans++;
                cnt=1;//满足条件划分成一个团队后,一定要重置cnt
            } else
                cnt++;
        }
        cout<<ans<<endl;
    }
}

D. Berserk And Fireball

未完待续…

你可能感兴趣的:(ACM题解,算法,编程语言,c++,c语言)