题解:P12860 [NERC 2020 Online] Kate' s 2021 Celebration

· · 题解

蒟蒻第一篇题解

题目

大体思路

我们先想一下思路:题目需要找出符合条件的最便宜的方案,也可以理解为是找出最便宜且符合条件的方案。 判断符合条件很简单.

判断是否符合条件

如果字符串里有两个2,一个0,一个1。那么就是符合条件的方式。

bool Chars(string s) {
    bool  has0 = false,has1 = false;
    int has2 =0;
    for (char c : s) {
        if (c == '2') has2 ++;
        else if (c == '0') has0 = true;
        else if (c == '1') has1 = true;
    }
    return (has2>=2)&& has0 && has1; 
}

求最小?

我们把他排个序,然后依次判断直接输出第一个符合条件的序号。

排序后顺序就变了,编号和字符串就跟价格不对应了,那就打包成结构体。

ACcode

#include<bits/stdc++.h>
using namespace std;
int n;
struct sh{
    int id;
    int m;
    string s;
}a[10005];
bool cmp(sh x,sh y){
    return x.m<y.m;
}
bool Chars(string s) {
    bool  has0 = false,has1 = false;
    int has2 =0;
    for (char c : s) {
        if (c == '2') has2 ++;
        else if (c == '0') has0 = true;
        else if (c == '1') has1 = true;
    }
    return (has2>=2)&& has0 && has1; 
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        a[i].id=i;
        cin>>a[i].m>>a[i].s;
    }
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=n;i++){
        if(Chars(a[i].s)){
            cout<<a[i].id<<'\n';
            return 0;
        }
    }
    cout<<0<<'\n';
    return 0;
}