ecjtu-summer training #9

A Robot

Description

The Robot Moving Institute is using a robot in their local store to transport different items. Of course the robot should spend only the minimum time necessary when travelling from one place in the store to another. The robot can move only along a straight line (track). All tracks form a rectangular grid. Neighbouring tracks are one meter apart. The store is a rectangle N x M meters and it is entirely covered by this grid. The distance of the track closest to the side of the store is exactly one meter. The robot has a circular shape with diameter equal to 1.6 meter. The track goes through the center of the robot. The robot always faces north, south, west or east. The tracks are in the south-north and in the west-east directions. The robot can move only in the direction it faces. The direction in which it faces can be changed at each track crossing. Initially the robot stands at a track crossing. The obstacles in the store are formed from pieces occupying 1m x 1m on the ground. Each obstacle is within a 1 x 1 square formed by the tracks. The movement of the robot is controlled by two commands. These commands are GO and TURN.
The GO command has one integer parameter n in {1,2,3}. After receiving this command the robot moves n meters in the direction it faces.

The TURN command has one parameter which is either left or right. After receiving this command the robot changes its orientation by 90o in the direction indicated by the parameter.

The execution of each command lasts one second.

Help researchers of RMI to write a program which will determine the minimal time in which the robot can move from a given starting point to a given destination.

Input

The input consists of blocks of lines. The first line of each block contains two integers M <= 50 and N <= 50 separated by one space. In each of the next M lines there are N numbers one or zero separated by one space. One represents obstacles and zero represents empty squares. (The tracks are between the squares.) The block is terminated by a line containing four positive integers B1 B2 E1 E2 each followed by one space and the word indicating the orientation of the robot at the starting point. B1, B2 are the coordinates of the square in the north-west corner of which the robot is placed (starting point). E1, E2 are the coordinates of square to the north-west corner of which the robot should move (destination point). The orientation of the robot when it has reached the destination point is not prescribed. We use (row, column)-type coordinates, i.e. the coordinates of the upper left (the most north-west) square in the store are 0,0 and the lower right (the most south-east) square are M - 1, N - 1. The orientation is given by the words north or west or south or east. The last block contains only one line with N = 0 and M = 0.

Output

The output contains one line for each block except the last block in the input. The lines are in the order corresponding to the blocks in the input. The line contains minimal number of seconds in which the robot can reach the destination point from the starting point. If there does not exist any path from the starting point to the destination point the line will contain -1.

Sample Input

9 10
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 1 0
7 2 2 7 south
0 0

Sample Output

12

一个机器人,执行每一个命令耗时1,有两个命令,1、向左向右转90度。2、向前走1、2或3步,求从起点到终点的最少耗时时间。

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <algorithm>
 5 #include <queue>
 6 #define ll long long
 7 using namespace std;
 8 const int MAX = 55;
 9 const int INF = 1<<30;
10 const int dir[4][2]{{-1,0},{0,1},{1,0},{0,-1}};
11 
12 struct Nod{
13     int x;
14     int y;
15     int t;
16     int d;
17     bool operator < (const Nod &a) const{
18         return t > a.t;
19     }
20 };
21 int g[MAX][MAX], n, m, mark[MAX][MAX][4];
22 int bfs(int bx, int by, int ex, int ey, int f) {
23     priority_queue<Nod> que;
24     Nod nod;
25     mark[bx][by][f] = 0;
26     que.push((Nod){bx,by,0,f});
27     while(!que.empty()) {
28         nod = que.top();
29         que.pop();
30         int x = nod.x, y = nod.y;
31         if(x == ex && y == ey) return nod.t;
32         int d = nod.d;
33         if(mark[x][y][(d+1)%4] == -1 || mark[x][y][(d+1)%4] > nod.t + 1){
34             mark[x][y][(d+1)%4] = nod.t+1;
35             que.push((Nod){x,y,nod.t+1,(d+1)%4});
36         }
37         if(mark[x][y][(d-1+4)%4] == -1 || mark[x][y][(d-1+4)%4] > nod.t + 1){
38             mark[x][y][(d-1+4)%4] = nod.t + 1;
39             que.push((Nod){x,y,nod.t+1,(d-1+4)%4});
40         }
41         int nx = x, ny = y;
42         for(int i = 1; i <= 3; i ++) {
43             nx += dir[d][0];
44             ny += dir[d][1];
45             if(nx <= 0 || nx >= m || ny <= 0 || ny >= n)break;
46             if(g[nx][ny] == 1 || g[nx-1][ny] == 1 || g[nx][ny-1] == 1 || g[nx-1][ny-1] == 1) break;
47             ///上行必须要用四个判断,因为机器人是在线上的,而障碍物是块,比如在(7,2)这个点,判断这个点
48             //是否可以走,就要判断(7,2),(7,1),(6,1),(6,2)这个四个点有没有块
49             if(mark[nx][ny][d] == -1 || mark[nx][ny][d] > nod.t+1){
50                 mark[nx][ny][d] = nod.t+1;
51                 que.push((Nod){nx,ny,nod.t+1,d});
52             }
53         }
54     }
55     return -1;
56 }
57 int main() {
58     int bx, by, ex, ey, d, ans;
59     char str[20];
60     while(scanf("%d%d",&m,&n)&&(n+m)) {
61         for(int i = 0; i < m; i ++) {
62             for(int j = 0; j < n; j ++) {
63                 scanf("%d",&g[i][j]);
64             }
65         }
66         scanf("%d %d %d %d %s",&bx,&by,&ex,&ey,str);
67         if(str[0] == 'n') d = 0;
68         else if(str[0] == 'e') d = 1;
69         else if(str[0] == 's') d = 2;
70         else if(str[0] == 'w') d = 3;
71         memset(mark,-1,sizeof(mark));
72         ans = bfs(bx,by,ex,ey,d);
73         printf("%d\n",ans);
74     }
75     return 0;
76 }

 

 

B 湫湫系列故事——消灭兔子

 

Problem Description
  湫湫减肥
  越减越肥!
  
  最近,减肥失败的湫湫为发泄心中郁闷,在玩一个消灭免子的游戏。
  游戏规则很简单,用箭杀死免子即可。
  箭是一种消耗品,已知有M种不同类型的箭可以选择,并且每种箭都会对兔子造成伤害,对应的伤害值分别为Di(1 <= i <= M),每种箭需要一定的QQ币购买。
  假设每种箭只能使用一次,每只免子也只能被射一次,请计算要消灭地图上的所有兔子最少需要的QQ币。
 

 

Input
输入数据有多组,每组数据有四行;
第一行有两个整数N,M(1 <= N, M <= 100000),分别表示兔子的个数和箭的种类;
第二行有N个正整数,分别表示兔子的血量Bi(1 <= i <= N);
第三行有M个正整数,表示每把箭所能造成的伤害值Di(1 <= i <= M);
第四行有M个正整数,表示每把箭需要花费的QQ币Pi(1 <= i <= M)。

特别说明:
1、当箭的伤害值大于等于兔子的血量时,就能将兔子杀死;
2、血量Bi,箭的伤害值Di,箭的价格Pi,均小于等于100000。
 

 

Output
如果不能杀死所有兔子,请输出”No”,否则,请输出最少的QQ币数,每组输出一行。
 

 

Sample Input
3 3
1 2 3
2 3 4
1 2 3
3 4
1 2 3
1 2 3 4
1 2 3 1
 

 

Sample Output
6
4
 
优先队列+贪心。一开始贪心思路出错,然后有想了个办法,可以用线段树做,每个箭的伤害当成一个区间坐标,QB当成它的值,
兔子的血从高往低排序,每次射一个兔子时选它的血当 l,区间的最大值当r,去找[l,r]这个区间的最小值然后把它标记为INF就行了。
可惜没有写出来,而这种思路正好可以用优先队列做。糊涂了,绕了这么多弯。
 
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <vector>
 4 #include <queue>
 5 #include <algorithm>
 6 using namespace std;
 7 #define ll long long
 8 const int MAX = 100010;
 9 int b[MAX];
10 struct Nod{
11     int d,q;
12 }nod[MAX];
13 bool cmp(Nod a, Nod b) {
14     return a.d < b.d;
15 }
16 int main() {
17     int n,m;
18     while(scanf("%d%d",&n,&m)!=EOF){
19         for(int i = 0; i < n; i ++) scanf("%d",&b[i]);
20         for(int i = 0; i < m; i ++) scanf("%d",&nod[i].d);
21         for(int i = 0; i < m; i ++) scanf("%d",&nod[i].q);
22         if(n > m) {
23             printf("No\n");
24             continue;
25         }
26         sort(b,b+n);
27         sort(nod,nod+m,cmp);
28         priority_queue<int, vector<int>, greater<int> >que;
29         int k = m-1;
30         bool flag = true;
31         ll ans = 0;
32         for(int i = n-1; i >= 0; i --) {
33             while(k >= 0 && nod[k].d >= b[i]){
34                 que.push(nod[k].q);
35                 k--;
36             }
37             if(que.empty()){
38                 flag = false;
39                 break;
40             }
41             ans += que.top();
42             que.pop();
43         }
44         if(flag)printf("%lld\n",ans);
45         else printf("No\n");
46     }
47     return 0;
48 }

 

C. Timofey and a tree

昨天的原题。

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 using namespace std;
 5 const int N = 1e5+10;
 6 int x[N], y[N], h[N], c[N];
 7 int main() {
 8     int n;
 9     scanf("%d",&n);
10     for(int i = 1; i < n; i ++) scanf("%d %d",&x[i],&y[i]);
11     for(int i = 1; i <= n; i ++) scanf("%d",&c[i]);
12     int m = 0;
13     for(int i = 1; i < n; i ++) {
14         if(c[x[i]] != c[y[i]]){
15             m++;
16             h[x[i]]++;h[y[i]]++;
17         }
18     }
19     for(int i = 1; i <= n; i ++){
20         if(h[i] == m){
21             return 0*printf("YES\n%d\n",i);
22         }
23     }
24     printf("NO\n");
25     return 0;
26 }

 

D Collecting Bugs

Description

Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other material stuff, he collects software bugs. When Ivan gets a new program, he classifies all possible bugs into n categories. Each day he discovers exactly one bug in the program and adds information about it and its category into a spreadsheet. When he finds bugs in all bug categories, he calls the program disgusting, publishes this spreadsheet on his home page, and forgets completely about the program.
Two companies, Macrosoft and Microhard are in tight competition. Microhard wants to decrease sales of one Macrosoft program. They hire Ivan to prove that the program in question is disgusting. However, Ivan has a complicated problem. This new program has s subcomponents, and finding bugs of all types in each subcomponent would take too long before the target could be reached. So Ivan and Microhard agreed to use a simpler criteria --- Ivan should find at least one bug in each subsystem and at least one bug of each category.
Macrosoft knows about these plans and it wants to estimate the time that is required for Ivan to call its program disgusting. It's important because the company releases a new version soon, so it can correct its plans and release it quicker. Nobody would be interested in Ivan's opinion about the reliability of the obsolete version.
A bug found in the program can be of any category with equal probability. Similarly, the bug can be found in any given subsystem with equal probability. Any particular bug cannot belong to two different categories or happen simultaneously in two different subsystems. The number of bugs in the program is almost infinite, so the probability of finding a new bug of some category in some subsystem does not reduce after finding any number of bugs of that category in that subsystem.
Find an average time (in days of Ivan's work) required to name the program disgusting.

Input

Input file contains two integer numbers, n and s (0 < n, s <= 1 000).

Output

Output the expectation of the Ivan's working days needed to call the program disgusting, accurate to 4 digits after the decimal point.

Sample Input

1 2

Sample Output

3.0000


题意:有n种bug和s种子系统,bug的数量是无限的,一个程序员每天都可以发现一个bug,现在求的是发现n中bug并存在
于s个子系统中的平均天数(期望)。题意有点难懂,读了好几遍了。
假设dp[i][j]表示已经发现了i种bug并存在于j个子系统中(注意是已经发现了),那么dp[n][s] 就是 0了。
从dp[i][j]经过一天会出现4中情况
1、dp[i+1][j+1],出现一个新的bug并存在一个新的子系统中,概率是(n-i)*(s-j)/(n*s)
2、dp[i][j+1],在一个子系统中出现一个bug,不过这个bug种类是之前出现过的,概率是(s-j)*i/(n*s)
3、dp[i+1][j],出现一个新的bug,但是在子系统的已经出现过了,概率是(n-i)*j/(n*s)
4、dp[i][j],在已经发现过的bug中发现一个bug,概率是i*j/(n*s)
然后根据E(aA+bB+cC+dD+...)=aEA+bEB+....;//a,b,c,d...表示概率,A,B,C...表示状态
所以dp[i][j]=p1*dp[i+1][j+1]+p2*dp[i+1][j]+p3*dp[i][j+1]+p4*dp[i][j]+1;//dp[i][j]表示的就是到达状态i,j的期望
=>dp[i][j]=(p1*dp[i+1][j+1]+p2*dp[i+1][j]+p3*dp[i][j+1]+1)/(1-p4);

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <algorithm>
 5 #define ll long long
 6 using namespace std;
 7 const int MAX = 1010;
 8 double dp[MAX][MAX];
 9 int main() {
10     int n,s;
11     while(scanf("%d%d",&n,&s)!=EOF){
12         memset(dp,0,sizeof(dp));
13         for(int i = n; i >= 0; i --) {
14             for(int j = s; j >= 0; j --) {
15                 if(i == n && j == s)continue;
16                 dp[i][j] = (i*(s-j)*dp[i][j+1]+(n-i)*j*dp[i+1][j]+(n-i)*(s-j)*dp[i+1][j+1]+n*s)/(n*s-i*j);
17             }
18         }
19         //printf("+++\n");
20         printf("%.4f\n",dp[0][0]);
21     }
22     return 0;
23 }

 

 

E An old Stone Game


Description


There is an old stone game.At the beginning of the game the player picks n(1<=n<=50000) piles of stones in a line. The goal is to merge the stones in one pile observing the following rules:
At each step of the game,the player can merge two adjoining piles to a new pile.The score is the number of stones in the new pile.
You are to write a program to determine the minimum of the total score.

Input


The input contains several test cases. The first line of each test case contains an integer n, denoting the number of piles. The following n integers describe the number of stones in each pile at the beginning of the game.
The last test case is followed by one zero.

Output


For each test case output the answer on a single line.You may assume the answer will not exceed 1000000000.

Sample Input


1
100
3
3 4 3
4
1 1 1 1
0

Sample Output


0
17
8

一条线上有n堆石头,每堆石头都有一个数量,可以合并相邻的两堆石头,每次合并会产生一个值,是两堆石头的总数,求合并成一堆石头时,总值是多少。
GarsiaWachs算法。做法就是,比如序列是a[n],从前往后寻找,找到k满足a[k]<a[k+2],然后合并a[k]和a[k+1],替换为新值s,然后从k往前寻找,
找到第一个大于s的数,并把s插到那个数的后面。
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #define ll long long
 5 using namespace std;
 6 const int MAX = 5e4+5;
 7 int num, n, a[MAX];
 8 ll ans;
 9 void dfs(int now) {
10     int j;
11     int temp = a[now-1] + a[now];
12     ans += (ll)temp;
13     for(int i = now; i < num -1; i ++) a[i] = a[i+1];
14     num--;
15     for(j = now-1; j > 0&& a[j-1] < temp; j --) a[j] = a[j-1];
16     a[j] = temp;
17     while(j >= 2 && a[j-2] <= a[j]){
18         int d = num - j;
19         dfs(j-1);
20         j = num-d;
21     }
22 }
23 int main() {
24     while(scanf("%d",&n)&&n){
25         for(int i = 0; i < n; i ++) scanf("%d",&a[i]);
26         num = 1;
27         ans = 0;
28         for(int i = 1; i < n; i ++) {
29             a[num++] = a[i];
30             while(num >= 3 && a[num-3] <= a[num-1]) dfs(num-2);
31         }
32         while(num > 1) dfs(num-1);
33         printf("%lld\n",ans);    
34     }
35     return 0;
36 }

 




posted @ 2017-07-26 21:03  starry_sky  阅读(254)  评论(0编辑  收藏  举报