Codeforces Beta Round #6

A - Triangle

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

判斷能否組成正面積三角形或退化三角形。
組成正面積三角形的條件:任意兩邊之和大於第三邊。
組成退化面積三角形的條件:存在兩邊之和等於第三邊。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

int a, b, c, d;
bool flag1, flag2;

int main(void)
{
	scanf("%d%d%d%d", &a, &b, &c, &d);
	if (a + b > c && a + c > b && b + c > a)
		flag1 = 1;
	else if (a + b > d && a + d > b && b + d > a)
		flag1 = 1;
	else if (a + c > d && a + d > c && c + d > a)
		flag1 = 1;
	else if (c + b > d && c + d > b && b + d > c)
		flag1 = 1;
	if (a + b == c || a + c == b || b + c == a)
		flag2 = 1;
	else if (a + b == d || a + d == b || b + d == a)
		flag2 = 1;
	else if (a + c == d || a + d == c || c + d == a)
		flag2 = 1;
	else if (c + b == d || c + d == b || b + d == c)
		flag2 = 1;
	if (flag1)
		printf("TRIANGLE\n");
	else if (flag2)
		printf("SEGMENT\n");
	else
		printf("IMPOSSIBLE\n");
	return 0;
}

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 n, m (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

這道題的題意可能有些難理解,每個非空的子矩陣代表桌子,每個大寫字母代表桌子的一部分,尋找與總統桌子相鄰的有多少種顏色。
直接暴力搜就可以。
一開始做的時候用的是scanf,在讀取字符上面出鍋了,後來改成了cin就可以了。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

char c;
int n, m, ans;
char ma[105][105];
bool arr[30];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

void dfs(int x, int y)
{
	for (int h = 0; h < 4; h++)
	{
		int nx = x + dx[h];
		int ny = y + dy[h];
		if (nx >=0 && nx < n && ny >=0 && ny < m && ma[nx][ny] != c && ma[nx][ny] != '.' && arr[ma[nx][ny] - 'A'] == 0)
		{
			ans++;
			arr[ma[nx][ny] - 'A'] = 1;
		}
	}
}

int main(void)
{
	cin >> n >> m >> c;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			cin >> ma[i][j];
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
		{
			if (ma[i][j] == c)
				dfs(i, j);
		}
	cout << ans << endl;
	return 0;
}

C - Alice, Bob and Chocolate

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

直接模擬即可,注意特判n = 1、2的情況。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5 + 5;

int arr[N];

int main(void)
{
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%d", &arr[i]);
	int i = 1, j = n;
	int a = arr[1];
	int b = arr[n];
	if (n == 1)
	{
		printf("1 0\n");
		return 0;
	}
	else if (n == 2)
	{
		printf("1 1\n");
		return 0;
	}
	while (j - i > 1)
	{
		if (a < b)
		{
			i++;
			a += arr[i];
		}
		else if (a > b)
		{
			j--;
			b += arr[j];
		}
		else if (a == b)
		{
			i++; j--;
			if (i == j)
			{
				j++;
				a += arr[i];
			}
			else
			{
				a += arr[i];
				b += arr[j];
			}
		}
	}
	printf("%d %d\n", i, n - j + 1);
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章