ACM-ICPC Asia Training League 暑假第一阶段第一场 ABEFJ

 A Choosing Ice Cream

You are standing in the supermarket in front of the freezers. You have a very tough task ahead of you: you have to choose what type of ice cream you want for after dinner that evening. After a while, you give up: they are all awesome! Instead, you take your (fair) kk-sided die out of your pocket and you decide to let fate decide.

Of course, the number of ice cream choices, nn, may not be precisely kk, in which case you could not just throw the die once, rolling ii, and take the iith ice cream choice. You therefore have to use some algorithm that involves zero or more die throws that results in an ice cream choice with every choice being exactly equally likely. Being a good computer scientist, you know about the accept-reject method, which would let you make such a fair choice.

At that point, you remember that you have a very importantcompetition to attend that same afternoon. You absolutely cannot afford to be late for that competition. Because of this, you decide you cannot use the accept-reject method, as there may be no bound on the number of die throws needed to ensure a fair result, so you may end up standing there for a long time and miss the competition! Instead, you resolve to find an algorithm that is fair and uses as few dice choices as possible in the worst case.

Given nn and kk, can you determine the minimum number ii such that there is a fair algorithm that uses at most iidie throws per execution?

Input Format

On the first line one positive number: the number of test cases, at most 100. After that per test case:

  • one line with two space-separated integers nn and kk (1 \leq n, k \leq 10^91n,k109): the number of ice cream choices and the number of sides of your die, respectively.

Output Format

Per test case:

  • one line with a single integer: the smallest number of throws after which you are guaranteed to be able to make a fair choice. If there is no such number, print “unbounded” instead.

样例输入

3
4 2
2 4
3 2

样例输出

2
1
unbounded

题目来源

BAPC 2014 Preliminary

求k^x%n==0中最小的x

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N = 110;
 5 ll n, t, k;
 6 ll fun(ll i) {
 7     ll ans = 1%n;
 8     while(i--) {
 9         ans *= k;
10         ans %= n;
11     }
12     return ans;
13 }
14 int main() {
15     scanf("%lld",&t);
16     while(t--) {
17         scanf("%lld%lld",&n,&k);
18         bool flag = false;
19         ll i = 0;
20         for(ll j = 1; j <= n; j*=2) {    
21             if(fun(i) == 0) {
22                 printf("%lld\n",i);
23                 flag = true;
24                 break;
25             }
26             i++;
27         }
28         if(!flag) printf("unbounded\n");
29     }
30     return 0;
31 }

 

 B Failing Components

As a jury member of the Best Architectural Planning Contest, you are tasked with scoring the reliability of a system. All systems entered in the contest consist of a number of components which depend on each other. The reliability of such a system depends on the damage done by a failing component. Ideally a failing component should have no consequences, but since most components depend on each other, some other components will usually fail as well.

Most components are somewhat resilient to short failures of the components they depend on. For example, a database could be unavailable for a minute before the caches expire and new data must be retrieved from the database. In this case, the caches can survive for a minute after a database failure, before failing themselves. If a component depends on multiple other components which fail, it will fail as soon as it can no longer survive the failure of at least one of the components it depends on. Furthermore no component depends on itself directly, however indirect self-dependency through other components is possible.

You want to know how many components will fail when a certain component fails, and how much time passes before all components that will eventually fail, actually fail. This is difficult to calculate by hand, so you decided to write a program to help you. Given the description of the system, and the initial component that fails, the program should report how many components will fail in total, and how much time passes before all those components have actually failed.

Input Format

On the first line one positive number: the number of test cases, at most 100. After that per test case:

  • one line with three space-separated integers nn, dd and cc (1\leq n \leq 100001n10000 and 1 \leq d \leq 1000001d100000 and 1 \leq c \leq n1cn): the total number of components in the system, the number of dependencies between components, and the initial component that fails, respectively.

  • dd lines with three space-separated integers aa, bb and ss (1 \leq a,b \leq n1a,bn and a\ != ba !=b and 0 \leq s \leq 1 0000s1000), indicating that component aa depends on component bb, and can survive for ss seconds when component bbfails.

In each test case, all dependencies (a, b)(a,b) are unique.

Output Format

Per test case:

  • one line with two space-separated integers: the total number of components that will fail, and the number of seconds before all components that will fail, have actually failed.

样例输入

2
3 2 2
2 1 5
3 2 5
3 3 1
2 1 2
3 1 8
3 2 4

样例输出

2 5
3 6

题目来源

BAPC 2014 Preliminary

n个组件,d个依赖关系,初试坏的组件为c。依赖关系为a依赖b,在b坏了后a还能运行s秒。求在c坏了后,最后有多少个组件损坏,用的时间是多少秒。

可以转换成最短路径来做。

 1 #include <bits/stdc++.h>
 2 #define INF 0x3f3f3f3f
 3 #define ll long long
 4 using namespace std;
 5 const int N = 10010;
 6 typedef pair<int, int> P;
 7 struct Nod{
 8     int to, w;
 9 };
10 vector<Nod> vs[N];
11 int dist[N];
12 
13 void bfs(int s) {
14     priority_queue<P,vector<P>,greater<P> > que;
15     dist[s] = 0;
16     que.push(P(0, s));
17     while(!que.empty()) {
18         P p = que.top(); que.pop();
19         int v = p.second;
20         if(dist[v] < p.first) continue;
21         for(int i = 0; i < vs[v].size(); i ++) {
22             Nod e = vs[v][i];
23             if(dist[e.to] > dist[v] + e.w) {
24                 dist[e.to] = dist[v] + e.w;
25                 que.push(P(dist[e.to], e.to));
26             }
27         }
28     }
29 }
30 int main() {
31     int t, n, d, c;
32     scanf("%d", &t);
33     while(t--) {
34         memset(dist, INF, sizeof(dist));
35         scanf("%d%d%d", &n, &d, &c);
36         for(int i = 0; i < d; i ++) {
37             int a, b, s;
38             Nod e;
39             scanf("%d%d%d", &a, &b, &s);
40             e.to = a, e.w = s;
41             vs[b].push_back(e);
42         }
43         bfs(c);
44         int cnt = 0, MIN = 0;
45         for(int i = 1; i <= n; i ++) {
46             if(dist[i] != INF) {
47                 cnt++;
48                 MIN = max(MIN, dist[i]);
49             }
50         }
51         for(int i = 1; i <= n; i ++) {
52             vs[i].clear();
53         }
54         printf("%d %d\n",cnt,MIN);
55     }
56     return 0;
57 }

 

 E Pawns

 

Carl and Nathan are completely bored with the game of chess; it’s too easy! They have come up with their own game, which is surely a greater test of one’s intelligence.This game is played on a board with n by m squares. At the start, white and black pawns are placed quasi-randomly over the board, with the following constraint: in every column there is one white pawn and one black pawn, with the white pawn on some square below the black one.

Each player in turn makes a move with one of his pawns. A pawn is only allowed to move one square forward, provided that this square is empty. “Forward” means in the direction of the opponent, so white pawns move up and black pawns move down. In addition, a pawn on the first rank – that is, a white pawn on the bottom row, or a black pawn on the top row – may also move two squares forward, provided that both squares are empty. Unlike normal chess, the pawns are never taken from the board and never change column.

For example, in the position above, White (the player using the white pieces) has eight moves: one with each of the pawns on b1, d2, f5 and h2, and two with both the pawn on c1 and the pawn on g1. The pawns on a6 and e1 cannot move.

Eventually and inevitably, the pawns will meet up in every column, leaving neither player able to move. The game is then finished, and the winner is the player who made the last move.

As usual, White gets the first move. With optimal play, who would win for a given startingposition?

输入格式

On the first line one positive number: the number of test cases, at most 100. After that per test case:

  • one line with two space-separated integers nn and mm (3\leq n \leq 203n20 and 1\leq m \leq 201m20):the number of rows and columns of the board, respectively.
  • nn lines with mm characters, describing the position on the board at the start of the game:
    • ‘W’ is a white pawn.
    • ‘B’ is a black pawn.
    • ‘.’ is an empty square.Each column contains exactly one ‘W’ and one ‘B’, with the ‘W’ being below the ‘B’.

In every test case, the starting position will be such that White has at least one move.

输出格式

Per test case:

  • one line with the string “White wins” if White can win with optimal play, or “Black wins” if Black has a winning strategy.

样例输入

5
8 8
.....BB.
B.......
W......B
...B.W..
..B.....
.B......
...WB..W
.WW.W.W.
6 4
....
B..B
....
.B.W
W.B.
.WW.
5 3
...
BBB
...
WW.
..W
4 6
.BBB.B
B...B.
...W..
WWW.WW
7 7
.B.B..B
.......
..B.B..
B....B.
.......
...WW.W
WWW..W.

样例输出

Black wins
Black wins
White wins
White wins
Black wins

题目来源

BAPC 2014 Preliminary

 

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N = 22;
 5 
 6 char str[N][N];
 7 
 8 int main() {
 9     int t, n, m;
10     scanf("%d", &t);
11     while(t--) {
12         scanf("%d%d", &n, &m);
13         for(int i = 0; i < n; i ++) scanf("%s", str[i]);
14         int ans1 = 0, ans2 = 0, ans3 = 0, move = 0, w, b, j;
15         for(int i = 0; i < m; i ++) {
16             for(j = 0; str[j][i] != 'B'; j ++);
17             b = j;
18             for(j = n-1; str[j][i] != 'W'; j --);
19             w = j;
20             if(b == 0 && w == n-1 && n > 3) ans3++;
21             else {
22                 if(b == 0 && w > 2) ans1 += w-2;
23                 else if(w == n-1 && b < n-3) ans2 += n-3-b;
24                 move += w-b-1;
25             }
26         }
27         printf((n==4 && ans3%2==1) || ans2 > ans1 || (ans2 >= ans1-1 && move%2==1)?"White wins\n":"Black wins\n");
28     }
29     return 0;
30 }

 

 F Runway Planning

Most airports have multiple runways. To identify runways, they are given a number indicat- ing the direction of the runway. Such a runway number is obtained by dividing the heading of the runway in degrees by ten, rounding the result, and optionally prefixing it with a ‘0’ if the result has only a single digit. For example, a runway with a heading of 82° is indicated by the number 08.If you are paying attention, you might think “a runway can be used in both directions, and therefore has two headings, but it is only assigned one runway number.” You are correct: normally a runway is identified by two numbers, based on the direction in which the runway is used. To simplify matters, we only concern ourselves with the smallest of these two num- bers, except if it is zero; we then use 18 instead. The runway numbers thus range from 01 to 18.

Now, you might think, “what if two runways have the same heading?” In that case, the characters ‘L’ and ‘R’ are appended to the number of the left and right runway, respectively. But what if three runways have the same heading? Then, the character ‘C’ is appended to the center runway. “But”, I can hear you ask, “what if four runways have the same heading?” If you really want to know, look up how Dallas/Fort Worth International Airport solved this problem after the contest. At any rate, we do not concern ourselves with multiple runways having the same heading in this problem, so you can forget all you read in this paragraph.

The runway in use is mostly determined by the current direction of the wind. It is pre- ferred to take off and land with headwind. If it is not possible to have the wind coming from straight ahead, its direction should be as close to that as possible. For example, if an airport has the runways 05 and 15, and the wind has a heading of 70°, taking off and landing using runway 05 is preferred, since the heading of that runway is closest to the heading of the wind.

Now, consider an airport already having one or more runways, and planning the con- struction of a new runway. Obviously, this runway should have a unique runway number: not only would we otherwise have a problem outside the boundaries of our restricted runway numbering outlined above, but, most importantly, this increases the probability of being able to take off or land with headwind.

The engineers at the airport under consideration have already determined the heading of the new runway, but still need you to determine the runway number. Note that the engineers are not very considerate with their input to your problem. They give you one heading of the runway, but it can be either the lowest or the highest heading of the runway. Be sure to give the lowest of the two runway numbers, as discussed in the second paragraph of this problem statement, even if you are given the highest of the two headings from the engineers.

输入格式

On the first line one positive number: the number of test cases, at most 100. After that per test case:

  • one line with a single integer hh (1 \leq h \leq 3601h360): the heading of the new runway in degrees.

输出格式

Per test case:

  • one line with the runway number of the newly constructed runway.

样例输入

4
82
115
316
4

样例输出

08
12
14
18

题目来源

BAPC 2014 Preliminary

 

签到题。

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N = 110;
 5 
 6 int main() {
 7     int t, n;
 8     cin >> t;
 9     while(t--) {
10         cin >> n;
11         if(n > 180) n -= 180;
12         n = int(1.0*n/10+0.5);
13         if(n == 0) n = 18;
14         printf("%02d\n",n);
15     }
16     return 0;
17 }

  J Word Search

A word search puzzle is a puzzle that involves a rectangular grid of letters and a list of words. The objective is to find and mark all those words, which are hidden inside the grid. The words may be placed horizontally, vertically, or diagonally, in either direction. When you have found a word, you mark all the letters in the grid that are involved. A letter may be part of multiple words. At the end, all the unmarked letters, from top to bottom and from left to right, form a message; this is the solution.

A certain magazine has a bunch of word search puzzles in it. They would like you to check, for each puzzle, that all words are actually in the grid. You should also be on the lookout for words that can be found in two (or more) different places – even if it does not influence the final solution. If all is well, just give the solution.

输入格式

On the first line one positive number: the number of test cases, at most 100. After that per test case:

  • one line with three integers nn, hh and ww (1 \leq n \leq 2561n256 and 1 \leq h,w \leq 321h,w32):the number of words and the height and width of the grid, respectively.

  • hh lines with ww uppercase letters: the grid.

  • nn lines, each with a single string ss (1 \leq length(s) \leq 321length(s)32), consisting of uppercase letters only: the words to be found in the grid.

输出格式

Per test case:

  • one line with a single string of uppercase letters: the solution. If there is a word that is not present in the grid, print “no solution” instead. If all words are present, but there is a word for which there are two (or more) different sets of letters that could be marked, print “ambiguous” instead. If all words are present and can be found uniquely in the grid, yet there are no unmarked letters remaining, print“empty solution”instead.

样例输入

4
10 7 8
ELIPMOCN
TACODEOL
IMELBORP
MGOALRRM
BIPLEIEA
UCATZUCE
SBHEMSTT
BAPC
TUE
TEAM
PROBLEM
CODE
COMPILE
SUBMIT
CORRECT
BALLOON
PRIZE
2 4 3
BCB
AOA
PDP
CEC
BAPC
CODE
3 4 3
BCB
AOA
PDP
CEC
BAPC
CODE
TEAM
2 2 10
DELEVELEDB
ATESTSETPC
DELEVELED
TESTSET

样例输出

ALGORITHMS
ambiguous
no solution
BAPC

暴力搜索,注意回文和长度为1的、
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N = 35;
 4 char str[N][N];
 5 int vis[N][N];
 6 char s[N];
 7 int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1}, 
 8     dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
 9 int n, h, w, t;
10 int ok() {
11     int len = strlen(s);
12     if(len == 1) return 1;
13     for(int i = 0; i <= len/2; i ++) {
14         if(s[i] != s[len-1-i]) return 2;
15     }
16     return 3;
17 }
18 
19 int main() {
20     scanf("%d", &t);
21     while(t--) {
22         memset(vis, 0, sizeof(vis));
23         scanf("%d%d%d", &n, &h, &w);
24         for(int i = 1; i <= h; i ++) scanf("%s", str[i]+1);
25         bool flag = false, flag1 = false;
26         bool flag2 = false;
27         while(n--) {
28             scanf("%s", s);
29             int ans = 0, len;
30             int ok1 = ok();
31             if(ok1 == 1) len = 1;
32             else if(ok1 == 2) len = 8;
33             else len = 4;
34             for(int i = 1; i <= h; i ++) {
35                 for(int j = 1; j <= w; j ++) {
36                     for(int k = 0; k < len; k ++) {
37                         int nx = i, ny = j;
38                         bool flag1 = true;
39                         // if(i==3&&j==5&&l==5) printf("%d\n",k);
40                         for(int l = 0; s[l]; l ++) {
41                             // if(i==7&&j==2&&k==2) printf("%c %c\n",str[nx][ny],s[l]);
42                             if(str[nx][ny] != s[l] || nx > h || ny > w) {
43                                 flag1 = false;
44                                 break;
45                             }
46                             nx = nx + dx[k], ny = ny + dy[k];
47                         }
48                         if(flag1) {
49                             ans++;
50                             nx = i, ny = j;
51                             for(int l = 0; s[l]; l ++) {
52                                 vis[nx][ny]++;
53                                 nx = nx + dx[k], ny = ny + dy[k];
54                             }
55                         }
56                     }
57                 }
58             }
59             // printf("ans:%d\n",ans);
60             if(ans > 1) {
61                 flag = true;
62             } else if(ans == 0) {
63                 flag2 = true;
64             }
65         }
66         if(flag2) {
67             printf("no solution\n");
68             continue;
69         }
70         for(int i = 1; i <= h; i ++) {
71             for(int j = 1; j <= w; j ++) {
72                 if(vis[i][j] == 0) {
73                     flag1 = true;
74                 }
75             }
76         }
77         if(flag) {
78             printf("ambiguous\n");
79         } else if(!flag1){
80              printf("empty solution\n");
81         } else {
82             for(int i = 1; i <= h; i ++) {
83                 for(int j = 1; j <= w; j ++) {
84                     if(vis[i][j] == 0) printf("%c",str[i][j]);
85                 }
86             }
87             printf("\n");
88         }
89     }
90     return 0;
91 }

 

posted @ 2018-07-08 13:22  starry_sky  阅读(726)  评论(0编辑  收藏  举报