其他分享
首页 > 其他分享> > Codeforces Round #805 (Div. 3)

Codeforces Round #805 (Div. 3)

作者:互联网

C. Train and Queries

思路:

开一个map记录一个数字出现的最小坐标和最大坐标

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> PII;

const int N = 200010;

int n, k;
PII a[N];

int main()
{
    int T;
    cin >> T;
    while(T -- )
    {
        int n, k;
        cin >> n >> k;
        map<int, PII> idx;
        for (int i = 1; i <= n; i ++ )
        {
            int x;
            cin >> x;
            if(idx[x].first == 0)
                idx[x].first = idx[x].second = i;
            idx[x].first = min(idx[x].first, i);
            idx[x].second = max(idx[x].second, i);
        }

        while(k -- )
        {
            int a, b;
            cin >> a >> b;
            if(idx[a].first == 0 || idx[b].first == 0)
                cout << "no" << endl;
            else if(idx[b].second < idx[a].first)
                cout << "no" << endl;
            else
                cout << "yes" << endl;
        }
    }
    return 0;
}

D. Not a Cheap String

思路:

贪心,开一个map记录各字母出现的次数,然后从z到a开始删,删的是map里这些字符的个数,直到总代价小于等于p。最后输出剩余的字符

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> PII;

const int N = 200010;

int n, k;
PII a[N];

int main()
{
    int T;
    cin >> T;
    while(T -- )
    {
        string str;
        cin >> str;
        int p;
        cin >> p;
        map<char, int> cnt;
        int sum = 0;
        for(int i = 0; i < str.size(); i ++ )
        {
            sum += str[i] - 'a' + 1;
            cnt[str[i]]++;
        }
        for (int i = 'z'; i >= 'a'; i -- )
        {
            if(sum <= p)
                break;
            while(sum > p && cnt[i])
            {
                sum -= (i - 'a' + 1);
                cnt[i]--;
            }
        }

        for (int i = 0; i < str.size(); i ++ )
            if(cnt[str[i]])
            {
                cnt[str[i]]--;
                cout << str[i];
            }

        cout << endl;
    }
    return 0;
}

 

标签:cnt,idx,int,Codeforces,cin,--,str,Div,805
来源: https://www.cnblogs.com/yctql/p/16528488.html