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 }

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 }

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 }

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 }

时间: 2024-08-25 03:46:22

2017-4-17-Train:Codeforces Beta Round #6 (Div. 2 Only)的相关文章

Codeforces Beta Round #91 (Div. 1 Only) E. Lucky Array

E. Lucky Array Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467are not. Petya has an arra

暴力/DP Codeforces Beta Round #22 (Div. 2 Only) B. Bargaining Table

题目传送门 1 /* 2 题意:求最大矩形(全0)的面积 3 暴力/dp:每对一个0查看它左下的最大矩形面积,更新ans 4 注意:是字符串,没用空格,好事多磨,WA了多少次才发现:( 5 详细解释:http://www.cnblogs.com/cszlg/p/3217478.html 6 */ 7 #include <cstdio> 8 #include <algorithm> 9 #include <cstring> 10 #include <cmath>

图论/暴力 Codeforces Beta Round #94 (Div. 2 Only) B. Students and Shoelaces

题目传送门 1 /* 2 图论/暴力:这是个连通的问题,每一次把所有度数为1的砍掉,把连接的点再砍掉,总之很神奇,不懂:) 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 #include <cmath> 8 using namespace std; 9 10 const int MAXN = 1e2 + 10; 11 const int INF = 0x3f3f3

BFS Codeforces Beta Round #94 (Div. 2 Only) C. Statues

题目传送门 1 /* 2 BFS:三维BFS,坐标再加上步数,能走一个点当这个地方在步数内不能落到.因为雕像最多8步就会全部下落, 3 只要撑过这个时间就能win,否则lose 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <queue> 8 #include <vector> 9 #include <cstring> 10 using namespace std; 11 1

水题 Codeforces Beta Round #70 (Div. 2) A. Haiku

题目传送门 1 /* 2 水题:三个字符串判断每个是否有相应的元音字母,YES/NO 3 下午网速巨慢:( 4 */ 5 #include <cstdio> 6 #include <cstring> 7 #include <string> 8 #include <iostream> 9 #include <algorithm> 10 #include <cmath> 11 using namespace std; 12 13 cons

Codeforces Beta Round #6 (Div. 2 Only)

Codeforces Beta Round #6 (Div. 2 Only) A 水题 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 1000010 7 typedef long long ll; 8 /*#ifndef

Codeforces Beta Round #9 (Div. 2 Only)

Codeforces Beta Round #9 (Div. 2 Only) http://codeforces.com/contest/9 A gcd水题 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 1000010 7

Codeforces Beta Round #12 (Div 2 Only)

Codeforces Beta Round #12 (Div 2 Only) http://codeforces.com/contest/12 A 水题 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 1000010 7 t

Codeforces Beta Round #14 (Div. 2)

Codeforces Beta Round #14 (Div. 2) http://codeforces.com/contest/14 A 找最大最小的行列值即可 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 500005

Codeforces Beta Round #22 (Div. 2 Only)

Codeforces Beta Round #22 (Div. 2 Only) http://codeforces.com/contest/22 A 水题 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 500005 7 t