1136D - Nastya Is Buying Lunch - CodeForces Solution


greedy *1800

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf (ll)1e18
#define pi (3.141592653589)
ll mod=1000000007;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define min3(a, b, c) min(c, min(a, b))
#define min4(a, b, c, d) min(d, min(c, min(a, b)))
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
vector<ll>adj[300002];
vector<ll>vis(200002,0);
vector<ll>dis(200002,0);
#define countbit(a) __builtin_popcount(a)
void extendgcd(ll a, ll b, ll*v) {if (b == 0) {v[0] = 1; v[1] = 0; v[2] = a; return ;} extendgcd(b, a % b, v); ll x = v[1]; v[1] = v[0] - v[1] * (a / b); v[0] = x; return;} //pass an arry of size1 3
ll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}
ll mminv(ll a, ll b) {ll arr[3]; extendgcd(a, b, arr); return arr[0];} //for non prime b
ll mminvprime(ll a, ll b) {return expo(a, b - 2, b);}  //for prime b
//void google(int t) {cout << Case # << t << : ;}
#define all(v) v.begin(),v.end()
/*returns nCr*/ll combination(ll n, ll r, ll m, ll *fact, ll *ifact) {ll val1 = fact[n]; ll val2 = ifact[n - r]; ll val3 = ifact[r]; return (((val1 * val2) % m) * val3) % m;}
#define vll vector<ll>
#define pll pair<ll,ll>
#define dec(n) cout<<fixed; cout<<setprecision(n);
ll dp1[1030][1020];
ll C(int n, int r){ if (r == 0 || r == n) return 1; if (dp1[n][r]) return dp1[n][r];return dp1[n][r] = (C(n - 1, r - 1) + C(n - 1, r)) % mod;}
/*ll up[200002][20];
ll depth[200002];
ll LCA(ll x,ll y){if(depth[x]>depth[y]) swap(x,y); ll d= depth[y]-depth[x];  for(int i=19;i>=0;i--){ if(d&(1<<i)) {y=up[y][i];}}if(x==y) return x; for(int i=19;i>=0;i--) {if(up[x][i]!=up[y][i]){x=up[x][i];y=up[y][i];}}return up[x][0];}
void LCAprec(ll node){vis[node]=1;for(auto x:adj[node]){if(vis[x]==0){depth[x]=depth[node]+1;up[x][0]=node;for(int i=1;i<20;i++){up[x][i]=up[up[x][i-1]][i-1];}LCAprec(x);}}}
*/

int main()
{
fast
int t=1;
//cin>>t;
for(int tc=1;tc<=t;tc++)
{
   ll n,m;
   cin>>n>>m;
   vll v(n);
   for(int i=0;i<n;i++)
   {
       cin>>v[i];
   }
   for(int i=0;i<m;i++)
   {
      ll x,y;
      cin>>x>>y;
      adj[x].pb(y);
   }
   set<ll>s;
   s.insert(v.back());
   ll cnt=0;
   for(int i=n-2;i>=0;i--)
   {
    ll cnt1=0;
       for(auto x:adj[v[i]])
       {
          if(s.count(x)) cnt1++;
       }
       if(cnt1==s.size()) cnt++;
       else 
       {
           s.insert(v[i]);
       }
   }
   cout<<cnt<<'\n';
}


}


Comments

Submit
0 Comments
More Questions

686A - Free Ice Cream
1358D - The Best Vacation
1620B - Triangles on a Rectangle
999C - Alphabetic Removals
1634C - OKEA
1368C - Even Picture
1505F - Math
1473A - Replacing Elements
959A - Mahmoud and Ehab and the even-odd game
78B - Easter Eggs
1455B - Jumps
1225C - p-binary
1525D - Armchairs
1257A - Two Rival Students
1415A - Prison Break
1271A - Suits
259B - Little Elephant and Magic Square
1389A - LCM Problem
778A - String Game
1382A - Common Subsequence
1512D - Corrupted Array
667B - Coat of Anticubism
284B - Cows and Poker Game
1666D - Deletive Editing
1433D - Districts Connection
2B - The least round way
1324A - Yet Another Tetris Problem
246B - Increase and Decrease
22E - Scheme
1566A - Median Maximization