Problem I. Instruction

题目连接:

http://codeforces.com/gym/100531/attachments

Description

Ingrid is a head of a big railway station and, among other duties, is responsible for routing trains to the

right platforms. The station has one entrance, and there are many switches that direct trains to other

switches and platforms.

Each switch has one inbound track and two outbound tracks, platforms have one inbound track, and

station entrance has one outbound track. Each outbound track is connected to one inbound track and

vice versa. Every switch and platform is reachable from station entrance.

Platforms have a rail dead ends and you may assume that trains disappear from the platform immediately

after arriving to it.

Each morning Ingrid looks at the timetable and writes switch toggling instruction: when and which

switch to toggle. She would like to automate this process to save a lot of time.

Input

The first line of the input file contains a single integer n — the total number of switches and platforms

on the station (3 ≤ n ≤ 51).

The i-th of the following n lines describes a switch or a platform with an index i. Description starts with

a character ‘p’ for a platform or ‘s’ for a switch. Next number qi

indicates the number of the switch the

inbound track is connected to or 0 if it is connected to station entrance (0 ≤ qi < i). Description of the

platform also contains a unique lowercase English letter — the platform identifier.

Trains spend exactly one minute to move between two connected switches or a switch and a platform.

In the morning, each switch is toggled in a way that a train would pass to the one of the two outbound

tracks connected to the switch/platform with the lower number.

Next line of the input file contains a single integer m (1 ≤ m ≤ 1000) — the number of trains in timetable.

Each of the following m lines contains integer ai (0 ≤ ai ≤ 10 000; ai > ai−1) — the time in minutes

when a train arrives to the station entrance, and the letter pi — identifier of the destination platform for

this train.

Output

In the first line output integer c — the number of commands in the switch toggling instruction. For each

command, output two integers si and ti (1 ≤ si ≤ n; 0 ≤ ti ≤ 109

) — the number of the switch and the

time to toggle it. Assume that the switch is toggled between minutes ti − 1 and ti

.

Output commands in order of non-decreasing time. The number of commands should not exceed 100 000

Sample Input

7

s 0

s 1

s 1

p 2 a

p 2 b

p 3 c

p 3 d

5

0 a

1 c

3 b

4 a

5 d

Sample Output

6

1 2

1 4

2 4

2 6

1 6

3 7

Hint

题意

给你一个像二叉树的火车站,一开始,所有点都指向最小的节点,你按动开关就会使得一个点指向另外一个儿子

然后现在有很多个火车会从起点走到他想去的叶子节点,然后请你输出一个可行解

题解:

我们一个火车一个火车分析就好了

当火车去序号小的,就不动开关,如果去序号大的,就动开关,当他过了之后,就再动一下开关,使得又指向小的就好了

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int n;
  4. map<char,int> H;
  5. vector<int> E[3000];
  6. vector<int>ans1[30000];
  7. vector<int>temp1,temp2;
  8. void solve(int x,int time,int fin)
  9. {
  10. //cout<<x<<" "<<time<<" "<<fin<<endl;
  11. if(x==fin)
  12. {
  13. for(int i=0;i<temp1.size();i++)
  14. ans1[temp2[i]].push_back(temp1[i]);
  15. return;
  16. }
  17. for(int i=0;i<E[x].size();i++)
  18. {
  19. if(i==1)
  20. {
  21. temp1.push_back(x);
  22. temp1.push_back(x);
  23. temp2.push_back(time);
  24. temp2.push_back(time+1);
  25. }
  26. solve(E[x][i],time+1,fin);
  27. if(i==1)
  28. {
  29. temp1.pop_back();
  30. temp1.pop_back();
  31. temp2.pop_back();
  32. temp2.pop_back();
  33. }
  34. }
  35. }
  36. int main()
  37. {
  38. freopen("instruction.in","r",stdin);
  39. freopen("instruction.out","w",stdout);
  40. scanf("%d",&n);
  41. for(int i=1;i<=n;i++)
  42. {
  43. string s;
  44. cin>>s;
  45. if(s=="s")
  46. {
  47. int x;
  48. scanf("%d",&x);
  49. E[x].push_back(i);
  50. }
  51. if(s=="p")
  52. {
  53. int x;
  54. scanf("%d",&x);
  55. char c;cin>>c;
  56. H[c]=i;
  57. E[x].push_back(i);
  58. }
  59. }
  60. int q;scanf("%d",&q);
  61. for(int i=0;i<q;i++)
  62. {
  63. int t;scanf("%d",&t);
  64. char c;cin>>c;
  65. solve(0,t,H[c]);
  66. }
  67. int sum = 0;
  68. for(int i=0;i<30000;i++)
  69. sum+=ans1[i].size();
  70. cout<<sum<<endl;
  71. for(int i=0;i<30000;i++)
  72. for(int j=0;j<ans1[i].size();j++)
  73. cout<<ans1[i][j]<<" "<<i<<endl;
  74. }

Codeforces Gym 100531I Instruction 构造的更多相关文章

  1. Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】

     2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...

  2. Codeforces GYM 100876 J - Buying roads 题解

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

  3. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

  4. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  5. Codeforces Gym 101623A - 动态规划

    题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...

  6. 【Codeforces Gym 100725K】Key Insertion

    Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...

  7. codeforces gym 100553I

    codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...

  8. CodeForces Gym 100213F Counterfeit Money

    CodeForces Gym题目页面传送门 有\(1\)个\(n1\times m1\)的字符矩阵\(a\)和\(1\)个\(n2\times m2\)的字符矩阵\(b\),求\(a,b\)的最大公共 ...

  9. Codeforces Gym 100187K K. Perpetuum Mobile 构造

    K. Perpetuum Mobile Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...

随机推荐

  1. PreferenceActivity使用方法

              public class MainActivity extends Activity { @Override protected void onCreate(Bundle save ...

  2. javascript面向对象实例

    非私有属性 function Student(name, gender, age, grade, teacher){ this.name = name; this.gender = gender; t ...

  3. java PO、BO

    PO(persistent object) 持久对象 在o/r映射的时候出现的概念,如果没有o/r映射,那么这个概念也就不存在了.通常对应数据模型(数据库),本身还有部分业务逻辑的处理.可以看成是与数 ...

  4. LeetCode Database: Rank Scores

    Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...

  5. mybatis系列-16-spring和mybatis整合

    16.1     整合思路 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSess ...

  6. Bluebird-Core API(二)

    .error .error([function(any error) rejectedHandler]) -> Promise 和catch一样,但是catch捕获了所有错误类型的异常,而err ...

  7. LeetCode(3) - Longest Substring Without Repeating Characters

    这题的题意大概就是给你一个字符串"abcdecde",找到最长的子字符串长度,里面所有的子母都不重复.本例子中最长的满足条件的子字符串就是"abcde",所以应 ...

  8. ado无法访问数据库问题

    现象:以ADO方式访问数据库的C++程序,在一台计算机上能访问成功,在另一台计算机上却访问不成功,报告不能连接错误,并且这两台计算机都装有ado. 原因:ado版本不对 解决方案:下载KB983246 ...

  9. perf

  10. django admin site配置(二)

    1. ModelAdmin.inlines 将有外键的子类包含进视图 ,实例: class Author(models.Model): name = models.CharField(max_leng ...