Codeforces-Round#546(Div.2)-D-Nastya Is Buying Lunch
阿新 • • 發佈:2019-03-21
ret size iomanip i++ eof ORC \n space 一個
這個題官方tag是貪心,其實我覺得有點牽強,還是算思維題吧,但是自己的菜是沒得說的QAQ
做的時候能想到從後向前看,如果n-1能交換就交換,不能就看n-2,依次往前。
其實再深究就會發現,如果pi和pn的距離等於pi到pn之間(包括pn)與pi可交換的個數,那麽pn一定可以前進。
例如:
5 2
3 1 5 4 2
5 2
5 4
這組中5的位置和2差距是2,且可與2,4兩個交換,那麽就可以使pn前進。
然後有一個地方需要考慮下,這個題會輸入2 5這種交換,也就是說不能在輸入的時候直接處理出來pi後面的可交換的個數
那麽如果確定了pi沒有成為那個被換到pn後面的數(比如例子中的4),那麽4前面的pi在計算可交換個數時要考慮4。
#include <iostream> #include <cmath> #include <cstdio> #include <cstring> #include <vector> #include <algorithm> #include <queue> #include <set> #include <map> #include <stack> #include <iomanip> #include <string> using namespace std; const int maxn=300000+5; int n,m,u,v; vector<int> edge[maxn]; int arr[maxn],num[maxn]; int main(){ scanf("%d%d",&n,&m); memset(num,0,sizeof(num)); for(int i=1;i<=n;i++) scanf("%d",&arr[i]); for(int i=1;i<=m;i++){ scanf("%d%d",&u,&v); edge[v].push_back(u); } for(int i=0;i<edge[arr[n]].size();i++) num[edge[arr[n]][i]]++; int pos=n; for(int i=n-1;i>=1;i--){ if(pos-i==num[arr[i]]) pos--; else for(int j=0;j<edge[arr[i]].size();j++) num[edge[arr[i]][j]]++; } printf("%d\n",n-pos); return 0; }
Codeforces-Round#546(Div.2)-D-Nastya Is Buying Lunch