题意:

给一个序列和一组交换序列(a,b),当且仅当a在b的前面(不允许有间隔),这两个数才能交换,问最后一个数最多能移动多少个位置。

分析: 这题是思路是十分的巧妙呀 , 用一个数组num[x]  表示在x的后面有什么是可以于他交换的数 , 注意核心来了: 如果这个数目等于x的位置到目标位置id , 则目标可以向前进行移动 ,秒呀

  1. #include<bits/stdc++.h>
  2. using namespace std ;
  3. int a[];
  4. vector<int>G[];
  5. int num[];
  6. int main()
  7. {
  8. int n,m;
  9. scanf("%d%d",&n,&m);
  10. for(int i= ; i<=n ; i++)
  11. scanf("%d",&a[i]);
  12.  
  13. for(int i= ; i<=m ; i++)
  14. {
  15. int u,v;
  16. scanf("%d%d",&u,&v);
  17. G[v].push_back(u);
  18. }
  19.  
  20. for(int i= ; i<G[a[n]].size() ; i++)
  21. num[G[a[n]][i]]++;
  22. int ans=;
  23. int id=n;
  24. for(int i=n- ; i>= ; i--)
  25. {
  26. if(num[a[i]]==id-i) {ans++; id--;}
  27. else
  28. {
  29. for(int j= ; j<G[a[i]].size() ; j++)
  30. num[G[a[i]][j]]++;
  31. }
  32. }
  33. printf("%d\n",ans);
  34. return ;
  35. }

Codeforces 1136D Nastya Is Buying Lunch (贪心)的更多相关文章

  1. Codeforces 1136D - Nastya Is Buying Lunch - [贪心+链表+map]

    题目链接:https://codeforces.com/problemset/problem/1136/D 题意: 给出 $1 \sim n$ 的某个排列 $p$,再给出若干 $(x,y)$ 表示当序 ...

  2. cf1136D. Nastya Is Buying Lunch(贪心)

    题意 题目链接 给出一个排列,以及\(m\)个形如\((x, y)\)的限制,表示若\(x\)在\(y\)之前则可以交换\(x, y\). 问\(n\)位置上的数最多能前进几步 \(n \leqsla ...

  3. Nastya Is Buying Lunch CodeForces - 1136D (排列)

    大意: 给定n排列, m个pair, 每个pair(u,v), 若u,v相邻, 且u在v左侧, 则可以交换u和v, 求a[n]最多向左移动多少 经过观察可以发现, 尽量先用右侧的人与a[n]交换, 这 ...

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

    这道题,神仙贪心题... 题意就是我给出数的顺序,并给出多个交换,每个只能用于相邻交换,问最后一个元素,最多能往前交换多少步. 我们考虑这样一个问题,如果一个这数和a[n]发生交换,那么这个数作为后面 ...

  5. D. Nastya Is Buying Lunch

    链接 [https://codeforces.com/contest/1136/problem/D] 题意 有N个人,a[i]表示第i个人的编号,m个二元组. 当前一个在后一个的前面一个位置时二者可以 ...

  6. Nastya Is Buying Lunch

    At the big break Nastya came to the school dining room. There are nn pupils in the school, numbered ...

  7. CF1136D Nastya Is Buying Lunch

    思路: 1. 最终答案不超过能与Nastya“直接交换”的人数. 2. 对于排在j前面的i,如果i和i-j之间(包括j)的每个人都能“直接交换”,j才能前进一步. 实现: #include <b ...

  8. Codeforces GYM 100876 J - Buying roads 题解

    Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...

  9. Codeforces 437C The Child and Toy(贪心)

    题目连接:Codeforces 437C  The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...

随机推荐

  1. ubuntu 12.04下搭建web服务器(MySQL+PHP+Apache) 教程

    最近,经理让我搭建一个服务器,我从网上找了一篇比较好的教程,按教程一步步执行感觉挺顺利,所以准备整理一下这个教程.尊重原版注明出处:http://news.2ky.cn/Linux/ubuntu-12 ...

  2. spring框架 事务 注解配置方式

    user=LF password=LF jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl driverClass=oracle.jdbc.driver.Ora ...

  3. PostgreSQL的索引选型

    PostgreSQL里面给全文检索或者模糊查询加索引提速的时候,一般会有两个选项,一个是GIST类型,一个是GIN类型,官网给出的参考如下: There are substantial perform ...

  4. CSS--抽屉(dig.chouti.com)页面

    一.设置整体页面宽度 一般写个样式命名为.d{}设置整体页面指定宽度和居中,京东命名为.w{},bootstrap里命名为.container{} 1 2 3 4 5 6 7 8 9 10 11 12 ...

  5. vue.js的一些模板指令简述

    1.模板指令都是写在<template></template>html里面   v-text : value是什么就显示什么,如果value里面有html的标签,也会直接显示出 ...

  6. python核心编程第3章课后题答案(第二版55页)

    3-4Statements Ues ; 3-5Statements Use\(unless part of a comma-separated sequence in which case \ is ...

  7. dssfsfsfs

    一直都好使啊..好使 好使 MainActivity://overover package com.example.sockettest; import java.io.BufferedReader; ...

  8. HTML 5+CSS 3网页设计经典范例 (李俊民,黄盛奎) 随书光盘​

    <html 5+css 3网页设计经典范例(附cd光盘1张)>共分为18章,涵盖了html 5和css3中各方面的技术知识.主要内容包括html 5概述.html 5与html 4的区别. ...

  9. 关于CS0016: Could not write to output file ‘c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary AS

    1.添加用户"Network Service” 和 “IIS_IUSERS” 读下面目录的读写权限 a) C:\Windows\Temp b) C:\Windows\Microsoft.NE ...

  10. angular 父组件调用子组件

    import { Component, OnInit, ViewChild } from '@angular/core'; @Component({ selector: 'app-child', te ...