2017-4-17-Train:Codeforces Beta Round #6 (Div. 2 Only)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2017-4-17-Train:Codeforces Beta Round #6 (Div. 2 Only)相关的知识,希望对你有一定的参考价值。

A. Triangle(阅读题 + next_premutation)

Johnny has a younger sister Anne, who is very clever and smart. As she came home from the kindergarten, she told his brother about the task that her kindergartener asked her to solve. The task was just to construct a triangle out of four sticks of different colours. Naturally, one of the sticks is extra. It is not allowed to break the sticks or use their partial length. Anne has perfectly solved this task, now she is asking Johnny to do the same.

The boy answered that he would cope with it without any difficulty. However, after a while he found out that different tricky things can occur. It can happen that it is impossible to construct a triangle of a positive area, but it is possible to construct a degenerate triangle. It can be so, that it is impossible to construct a degenerate triangle even. As Johnny is very lazy, he does not want to consider such a big amount of cases, he asks you to help him.

Input

The first line of the input contains four space-separated positive integer numbers not exceeding 100 — lengthes of the sticks.

Output

Output TRIANGLE if it is possible to construct a non-degenerate triangle. Output SEGMENT if the first case cannot take place and it is possible to construct a degenerate triangle. Output IMPOSSIBLE if it is impossible to construct any triangle. Remember that you are to use three sticks. It is not allowed to break the sticks or use their partial length.

Examples

input

4 2 1 3

output

TRIANGLE

input

7 2 2 4

output

SEGMENT

input

3 5 9 1

output

IMPOSSIBLE

Means:

判断四条边能不能选三条组成一个三角形,如果能输出TRIANGLE,如果不能组成三角形,判断是否有两边之和等于第三边(degenerate triangle:传说中的退化三角形,我是看了题解才知道这货的。。。神英语),如果有,输出SEGMENT,上述都不满足,输出IMPOSSIBLE

Solve:

直接全排,按优先级判断

Code:

技术分享
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 static const int MAXN = 4;
 4 static const char tri[20] = {"TRIANGLE\0"};
 5 static const char seg[20] = {"SEGMENT\0"};
 6 static const char imp[20] = {"IMPOSSIBLE\0"};
 7 char ans[20] = {\0};
 8 int data[MAXN] = {0};
 9 bool flag = 0;
10 bool st = 0;
11 int main(int argc, char const *argv[])
12 {
13     scanf("%d%d%d%d" , &data[0] , &data[1] , &data[2] , &data[3]);
14     sort(data , data + 4);
15     do
16     {
17         if(data[0] + data[1] > data[2] && data[1] + data[2] > data[0] && data[0] + data[2] > data[1])
18         {
19             printf("%s" , tri);
20             flag = 1;
21             break;
22         }
23         else if(data[0] + data[1] == data[2])
24         {
25             st = 1;
26         }
27     }while(next_permutation(data , data + 4));
28     if(!flag)
29     {
30         if(st)
31         {
32             printf("%s" , seg);
33         }
34         else
35             printf("%s" , imp);
36     }
37     return 0;
38 }
View Code

B. President‘s Office(模拟 , 枚举)

President of Berland has a very vast office-room, where, apart from him, work his subordinates. Each subordinate, as well as President himself, has his own desk of a unique colour. Each desk is rectangular, and its sides are parallel to the office walls. One day President decided to establish an assembly, of which all his deputies will be members. Unfortunately, he does not remember the exact amount of his deputies, but he remembers that the desk of each his deputy is adjacent to his own desk, that is to say, the two desks (President‘s and each deputy‘s) have a common side of a positive length.

The office-room plan can be viewed as a matrix with n rows and m columns. Each cell of this matrix is either empty, or contains a part of a desk. An uppercase Latin letter stands for each desk colour. The «period» character («.») stands for an empty cell.

Input

The first line contains two separated by a space integer numbers nm (1 ≤ n, m ≤ 100) — the length and the width of the office-room, and c character — the President‘s desk colour. The following n lines contain m characters each — the office-room description. It is guaranteed that the colour of each desk is unique, and each desk represents a continuous subrectangle of the given matrix. All colours are marked by uppercase Latin letters.

Output

Print the only number — the amount of President‘s deputies.

Examples

input

3 4 R
G.B.
.RR.
TTT.

output

2

input

3 3 Z
...
.H.
..Z

output

0

Means:

给定矩形,给你一个初始颜色值,问你与初始颜色值相邻(上下左右)的有多少种不同的颜色

Solve:

直接找到初始颜色值位置,然后上下左右枚举

Code:

技术分享
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 static const int dirx[5] = {1 , 0 , -1 , 0};
 4 static const int diry[5] = {0 , 1 , 0 , -1};
 5 static const int MAXN = 150;
 6 bool vis[200] = {0};
 7 char data[MAXN][MAXN];
 8 int n , m;
 9 char ed;
10 struct Node
11 {
12     int x , y;
13 };
14 vector <Node> in;
15 int main()
16 {
17     for(int i = 0 ; i <= 120 ; ++i)
18     {
19         for(int j = 0 ; j <= 120 ; ++j)
20             data[i][j] = .;
21     }
22     scanf("%d%d %c" , &n , &m , &ed);
23     vis[ed] = 1;
24     for(int i = 1 ; i <= n ; ++i)
25     {
26         for(int j = 1 ; j <= m ; ++j)
27         {
28             scanf(" %c" , &data[i][j]);
29             if(data[i][j] == ed)
30             {
31                 in.push_back({i , j});
32             }
33         }
34     }
35 
36     int si = in.size();
37     int ans = 0;
38     for(int i = 0 ; i < si ; ++i)
39     {
40         Node p;
41         for(int j = 0 ; j < 4 ; ++j)
42         {
43             p.x = in[i].x + dirx[j] , p.y = in[i].y + diry[j];
44             if(data[p.x][p.y] != . && !vis[data[p.x][p.y]])
45             {
46                 ++ans;
47                 vis[data[p.x][p.y]] = 1;
48             }
49         }
50     }
51     printf("%d\n" , ans);
52 }
View Code

C. Alice, Bob and Chocolate(Two Points + 模拟)

Alice and Bob like games. And now they are ready to start a new game. They have placed n chocolate bars in a line. Alice starts to eat chocolate bars one by one from left to right, and Bob — from right to left. For each chocololate bar the time, needed for the player to consume it, is known (Alice and Bob eat them with equal speed). When the player consumes a chocolate bar, he immediately starts with another. It is not allowed to eat two chocolate bars at the same time, to leave the bar unfinished and to make pauses. If both players start to eat the same bar simultaneously, Bob leaves it to Alice as a true gentleman.

How many bars each of the players will consume?

Input

The first line contains one integer n (1 ≤ n ≤ 105) — the amount of bars on the table. The second line contains a sequence t1, t2, ..., tn(1 ≤ ti ≤ 1000), where ti is the time (in seconds) needed to consume the i-th bar (in the order from left to right).

Output

Print two numbers a and b, where a is the amount of bars consumed by Alice, and b is the amount of bars consumed by Bob.

Examples

input

5
2 9 8 2 7

output

2 3

Means:

给你一些巧克力,以及吃每块巧克力的时间,Alice从左边开始,Bob从右边开始吃(两个人吃到同一块巧克力时Bob会让给Alice),现在问你他们俩没人吃了几块

Solve:

Two Points两边扫,也是模拟

Code:

技术分享
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 static const int MAXN = 1e5 + 10;
 4 int n;
 5 int data[MAXN];
 6 int alice , bob;
 7 int main(int argc, char const *argv[])
 8 {
 9     scanf("%d" , &n);
10     for(int i = 1 ; i <= n ; ++i)
11     {
12         scanf("%d" , &data[i]);
13     }
14     int l = 1 , r = n;
15     while(l <= r)
16     {
17         if(alice <= bob)
18         {
19             alice += data[l++];
20         }
21         else
22             bob += data[r--];
23     }
24     printf("%d %d" , l - 1 , n - r);
25     return 0;
26 }
View Code

 

E. Exposition(线段树 || 单调队列)

There are several days left before the fiftieth birthday of a famous Berland‘s writer Berlbury. In this connection the local library decided to make an exposition of the works of this famous science-fiction writer. It was decided as well that it is necessary to include into the exposition only those books that were published during a particular time period. It is obvious that if the books differ much in size, the visitors will not like it. That was why the organizers came to the opinion, that the difference between the highest and the lowest books in the exposition should be not more than k millimeters.

The library has n volumes of books by Berlbury, arranged in chronological order of their appearance. The height of each book in millimeters is know, it is hi. As Berlbury is highly respected in the city, the organizers want to include into the exposition as many books as possible, and to find out what periods of his creative work they will manage to cover. You are asked to help the organizers cope with this hard task.

Input

The first line of the input data contains two integer numbers separated by a space n (1 ≤ n ≤ 105) and k (0 ≤ k ≤ 106) — the amount of books by Berlbury in the library, and the maximum allowed height difference between the lowest and the highest books. The second line contains n integer numbers separated by a space. Each number hi (1 ≤ hi ≤ 106) is the height of the i-th book in millimeters.

Output

In the first line of the output data print two numbers a and b (separate them by a space), where a is the maximum amount of books the organizers can include into the exposition, and b — the amount of the time periods, during which Berlbury published a books, and the height difference between the lowest and the highest among these books is not more than k milllimeters.

In each of the following b lines print two integer numbers separated by a space — indexes of the first and the last volumes from each of the required time periods of Berlbury‘s creative work.

Examples

input

3 3
14 12 10

output

2 2
1 2
2 3

input

2 0
10 10

output

2 1
1 2

input

4 5
8 19 10 13

output

2 1
3 4

Means:

给出数列,要求找到一个区间,使得这个区间的最大值与最小值的差不能超过K,问你这个区间内最多有几个数,以及有这么多数的区间有几个,输出这些区间的起点和终点

Solve:

好多人写线段树,我这种菜鸡写不动线段树QWQ,所以这好像是滑动窗口?单调队列?STL搞一搞。(PS:rbegin():集合内最后一个数,以及集合内是有序的,以及multiset是允许有重复元素的。)

Code:

技术分享
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 static const int MAXN = 1e5;
 4 int data[MAXN];
 5 vector< pair<int , int> > ans;
 6 multiset <int> mul;
 7 int n , k;
 8 int main()
 9 {
10     scanf("%d%d" , &n , &k);
11     for(int i = 0 ; i < n ; ++i)
12         scanf("%d" , data + i);
13     int j = 0 , mx = -1;
14     for(int i = 0 ; i < n ; ++i)
15     {
16         mul.insert(data[i]);
17         while(*mul.rbegin() - *mul.begin() > k)
18             mul.erase(mul.find(data[j++]));
19         if(i - j + 1 > mx)
20         {
21             mx = i - j + 1;
22             ans.clear();
23         }
24         if(i - j + 1 == mx)
25             ans.push_back(make_pair(j + 1 , i + 1));
26     }
27     int si = ans.size();
28     printf("%d %d\n" , mx , si);
29     for(int i = 0 ;  i < si ; ++i)
30         printf("%d %d\n" , ans[i].first , ans[i].second);
31 }
View Code

 

以上是关于2017-4-17-Train:Codeforces Beta Round #6 (Div. 2 Only)的主要内容,如果未能解决你的问题,请参考以下文章