Codeforces Round #546 (Div. 2) D. Nastya Is Buying Lunch

Posted hyfer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #546 (Div. 2) D. Nastya Is Buying Lunch相关的知识,希望对你有一定的参考价值。

题意:

长度为n的数组{pi},m对关系(a,b),如果a正好在数组中位于b的前一个位置,则可以交换a和b,问最多可以让pn的位置往前移动多少

题解:

如果pn可以往前走k步,他肯定可以和pk交换。如果pk后面的数都可与他交换,则最后可以使pn和pk互换,使pn移动到pk的位置

#include <bits/stdc++.h>
//#pragma comment(linker, ”/STACK:36777216“)
           
using namespace std;
           
typedef long long ll;
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define all(a) a.begin(), a.end()
#define db long double

int n, m;
vector<int> a, was;
vector<vector<int> > g;

int main(){
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n >> m;
    a.resize(n);
    g.resize(n);
    was.resize(n);
    for (int i = 0; i < n; i++) cin >> a[i], a[i]--;
    for (int i = 0; i < m; i++){
        int w1, w2;
        cin >> w1 >> w2;
        w1--; w2--;
        g[w1].pb(w2);
    }

    reverse(all(a));
    int ans = 0;

    for (int i = 0; i < n; i++) was[i] = 0;
    was[a[0]] = 1;
    int cnt = 1;
    for (int i = 1; i < n; i++){
        int cnt2 = 0;
        for (int to : g[a[i]]){
            if (was[to]) cnt2++;
        }
        if (cnt == cnt2){
            ans++;
        } else {
            was[a[i]] = 1;
            cnt++;
        }
    }

    cout << ans;
}

 

以上是关于Codeforces Round #546 (Div. 2) D. Nastya Is Buying Lunch的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #546 (Div. 2) D 贪心 + 思维

Codeforces Round #546 (Div. 2)-D - Nastya Is Buying Lunch

Codeforces Round #546 (Div. 2) D. Nastya Is Buying Lunch

Codeforces-Round#546(Div.2)-D-Nastya Is Buying Lunch

Nastya Hasn't Written a Legend(Codeforces Round #546 (Div. 2)E+线段树)

Codeforces Round #436 E. Fire(背包dp+输出路径)