首页 > 代码库 > The Week Code - Issue #1

The Week Code - Issue #1

D - Train Problem I(栈的应用)

As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes a problem, there is only one railway where all the trains stop. So all the trains come in from one side and get out from the other side. For this problem, if train A gets into the railway first, and then train B gets into the railway before train A leaves, train A can‘t leave until train B leaves. The pictures below figure out the problem. Now the problem for you is, there are at most 9 trains in the station, all the trains has an ID(numbered from 1 to n), the trains get into the railway in an order O1, your task is to determine whether the trains can get out in an order O2. 
  技术分享技术分享技术分享

 

Input

The input contains several test cases. Each test case consists of an integer, the number of trains, and two strings, the order of the trains come in:O1, and the order of the trains leave:O2. The input is terminated by the end of file. More details in the Sample Input. 

Output

The output contains a string "No." if you can‘t exchange O2 to O1, or you should output a line contains "Yes.", and then output your way in exchanging the order(you should output "in" for a train getting into the railway, and "out" for a train getting out of the railway). Print a line contains "FINISH" after each test case. More details in the Sample Output. 

Sample Input

3 123 321

3 123 312

Sample Output

Yes.

in

in

in

out

out

out

FINISH

No.

FINISH

For the first Sample Input, we let train 1 get in, then train 2 and train 3.

So now train 3 is at the top of the railway, so train 3 can leave first, then train 2 and train 1.

In the second Sample input, we should let train 3 leave first, so we have to let train 1 get in, then train 2 and train 3.

Now we can let train 3 leave.

But after that we can‘t let train 1 leave before train 2, because train 2 is at the top of the railway at the moment.

So we output "No.".

Means:

就是问你进站的火车能不能形成后面给出的出站的火车的形式

Solve:

很明显,他说的火车进出站的形式就是个栈的形式,所以这是栈的模拟应用,然后他要求输出进出站过程,直接再设置一个bool数组来记录进出站就可以了

Code:

技术分享
 1 #include <bits/stdc++.h> 2 using namespace std; 3 static const int MAXN = 10010; 4 #define CLR(x , v)    memset(x , v , sizeof(x)) 5 char in[MAXN] , out[MAXN]; 6 int n , vis[MAXN]; 7 int main() 8 { 9     while(~scanf("%d" , &n))10     {11         CLR(in , \0) , CLR(out , \0) , CLR(vis , 0);12         scanf(" %s %s" , in , out);13         stack<char> st;14         int pos = 0;15         int tag = 0;16         for(int i = 0 ; i < n ; ++i)17         {18             st.push(in[i]);19             vis[tag++] = 1;//in20             while(!st.empty() && pos < n)21             {22                 if(st.top() == out[pos])23                 {24                     st.pop();25                     vis[tag++] = 0;//out26                     ++pos;27                 }28                 else29                     break;30             }31         }32         if(st.empty())33         {34             printf("Yes.\n");35             for(int i = 0 ; i < tag ; ++i)36             {37                 if(vis[i])38                     printf("in\n");39                 else40                     printf("out\n");41             }42         }43         else44         {45             printf("No.\n");46         }47         printf("FINISH\n");48     }49 50 }
View Code

E - 敌兵布阵(单点更新,区间查询)

Problem Description

C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了。A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况。由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视。
中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人,例如Derek问:“Tidy,马上汇报第3个营地到第10个营地共有多少人!”Tidy就要马上开始计算这一段的总人数并汇报。但敌兵营地的人数经常变动,而Derek每次询问的段都不一样,所以Tidy不得不每次都一个一个营地的去数,很快就精疲力尽了,Derek对Tidy的计算速度越来越不满:"你个死肥仔,算得这么慢,我炒你鱿鱼!”Tidy想:“你自己来算算看,这可真是一项累人的工作!我恨不得你炒我鱿鱼呢!”无奈之下,Tidy只好打电话向计算机专家Windbreaker求救,Windbreaker说:“死肥仔,叫你平时做多点acm题和看多点算法书,现在尝到苦果了吧!”Tidy说:"我知错了。。。"但Windbreaker已经挂掉电话了。Tidy很苦恼,这么算他真的会崩溃的,聪明的读者,你能写个程序帮他完成这项工作吗?不过如果你的程序效率不够高的话,Tidy还是会受到Derek的责骂的.

Input

第一行一个整数T,表示有T组数据。
每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表第i个工兵营地里开始时有ai个人(1<=ai<=50)。
接下来每行有一条命令,命令有4种形式:
(1) Add i j,i和j为正整数,表示第i个营地增加j个人(j不超过30)
(2)Sub i j ,i和j为正整数,表示第i个营地减少j个人(j不超过30);
(3)Query i j ,i和j为正整数,i<=j,表示询问第i到第j个营地的总人数;
(4)End 表示结束,这条命令在每组数据最后出现;
每组数据最多有40000条命令

Output

对第i组数据,首先输出“Case i:”和回车,
对于每个Query询问,输出一个整数并回车,表示询问的段中的总人数,这个数保持在int以内。

Sample Input

1

10

1 2 3 4 5 6 7 8 9 10

Query 1 3

Add 3 6

Query 2 7

Sub 10 2

Add 6 3

Query 3 10

End

Sample Output

Case 1:

6

33

59

Solve:

裸BIT问题

Code:

技术分享
 1 #include <bits/stdc++.h> 2 using namespace std; 3 static const int maxn = 50010; 4 int t , n; 5 int tree[maxn] = {0}; 6 inline int Lowbit(int x) 7 { 8     return x & (-x); 9 }10 void Update(int i , int x)11 {12 13     while(i <= n)14     {15         tree[i] = tree[i] + x;16         i += Lowbit(i);17     }18 }19 int Query(int x)20 {21     int sum = 0;22     while(x > 0)23     {24         sum += tree[x];25         x -= Lowbit(x);26     }27     return sum;28 }29 int main()30 {31     scanf("%d" , &t);32     for(int w = 1 ; w <= t ; ++w)33     {34         memset(tree , 0 , sizeof(tree));35         int x;36         scanf("%d" , &n);37         for(int i = 1 ; i <= n ; ++i)38         {39             scanf("%d" , &x);40             Update(i , x);41         }42         printf("Case %d:\n" , w);43         char cmd[10] = {\0};44         while(~scanf(" %s" , cmd))45         {46             if(cmd[0] == E)   break;47             int a , b;48             scanf("%d%d" , &a , &b);49             if(cmd[0] == A)   Update(a , b);50             if(cmd[0] == S)   Update(a , -b);51             if(cmd[0] == Q)   printf("%d\n" , Query(b) - Query(a - 1));52             memset(cmd , \0 , sizeof(cmd));53         }54     }55 }
View Code

The Week Code - Issue #1