1. 程式人生 > >2018-BNUZ-ACM-GDCPC選拔賽 【補題】

2018-BNUZ-ACM-GDCPC選拔賽 【補題】

第一次GDCPC選拔,說不想去是不可能的,中午回來就趕緊休息,下午又早早的來到教室,可是還是自己的問題吧,又是卡題,就是一點思路都沒有,就想著暴力,結果過不去各種wa還有tle,第一次出校打比賽的機會就沒了,唉,真的不知道自己到底是怎麼了,就是感覺越學越菜,然後那種肯定能過的水題,就是死活卡在那裡,是該加強數學訓練?還是讀題方式?還是思考方式?大概還是基礎不紮實吧,感覺和協會裡的人不知不覺中差了好多題沒寫,然後就慢慢拉開差距了,導致這種題老過不去,今天以後嘗試每天或每幾天刷刷cf的題吧,多加練習,還有以前講的,真的沒了模板就不會用了。也該好好複習了。

A. Mahmoud and Longest Uncommon Subsequence

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

While Mahmoud and Ehab were practicing for IOI, they found a problem which name was Longest common subsequence. They solved it, and then Ehab challenged Mahmoud with another problem.

Given two strings a

and b, find the length of their longest uncommon subsequence, which is the longest string that is a subsequence of one of them and not a subsequence of the other.

A subsequence of some string is a sequence of characters that appears in the same order in the string, The appearances don't have to be consecutive, for example, strings "ac

", "bc", "abc" and "a" are subsequences of string "abc" while strings "abbc" and "acb" are not. The empty string is a subsequence of any string. Any string is a subsequence of itself.

Input

The first line contains string a, and the second line — string b. Both of these strings are non-empty and consist of lowercase letters of English alphabet. The length of each string is not bigger than 105 characters.

Output

If there's no uncommon subsequence, print "-1". Otherwise print the length of the longest uncommon subsequence of a and b.

ExamplesInputCopy
abcd
defgh
OutputCopy
5
InputCopy
a
a
OutputCopy
-1
Note

In the first example: you can choose "defgh" from string b as it is the longest subsequence of string b that doesn't appear as a subsequence of string a.

這個題著實把我坑了一把,讀題讀出來是找最長不同子序列,然後wa了幾發後,在讀樣例感覺並不是,結果開始各種嘗試,最後也沒過,後來才明白這個題就是如果兩個字串不同,就輸出最長的字串長度,如果相同,就輸出-1,真的想吐血,被這種水題卡真的難受的要死。

#include<bits/stdc++.h>
using namespace std;
int main() {
	int t,alen,blen,j =0,num;
	char a[100005],b[100005];
	scanf("%s",&a);
	getchar();
	scanf("%s",&b);
	alen = strlen(a);
	blen = strlen(b);
	for(int i = 0; i < alen; i++) {
		if(a[i] != b[i]) {
			j = 1;
		}
	}
	for(int i = 0; i < blen; i++) {
		if(a[i] != b[i]) {
			j = 1;
		}
	}
	if(j) {
		if(alen > blen) {
			printf("%d\n",alen);
		} else {
			printf("%d\n",blen);
		}
	} else {
		printf("%d\n",-1);
	}

}

B. Mahmoud and a Triangle

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Mahmoud has n line segments, the i-th of them has length ai. Ehab challenged him to use exactly 3 line segments to form a non-degenerate triangle. Mahmoud doesn't accept challenges unless he is sure he can win, so he asked you to tell him if he should accept the challenge. Given the lengths of the line segments, check if he can choose exactly 3 of them to form a non-degenerate triangle.

Mahmoud should use exactly 3 line segments, he can't concatenate two line segments or change any length. A non-degenerate triangle is a triangle with positive area.

Input

The first line contains single integer n (3 ≤ n ≤ 105) — the number of line segments Mahmoud has.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the lengths of line segments Mahmoud has.

Output

In the only line print "YES" if he can choose exactly three line segments and form a non-degenerate triangle with them, and "NO" otherwise.

ExamplesInputCopy
5
1 5 3 2 4
OutputCopy
YES
InputCopy
3
4 1 2
OutputCopy
NO
Note

For the first example, he can use line segments with lengths 2, 4 and 5 to form a non-degenerate triangle.

然後是這個題,給你一堆邊,從中選三個能否組成三角形,能就是yes,否則是no,這個一看就是水題,上來第一道就嘗試這個,結果腦袋就不轉了,就想著暴力暴力,三個for怎麼想都是tle,然後又想複雜了,想著先排序,然後第一個第二個相加,在二分查詢找到比他們大的值,然後再判斷是否三角形,這樣更寫不出來。。真的不知道怎麼了,反正腦子就是一片空白,除了暴力,什麼都想不到,然後優化又沒有想法,最後無奈放棄。。結束看了題解後才發現根本沒有那麼複雜,甚至都不用挨個判斷兩邊之和大於第三邊,直接排序然後遍歷用兩個小的邊相加大於最大邊就有符合條件的直接yes,否則就no,真的沒有那麼麻煩,但是自己就是死活想不到,懷疑自己需要學習小學數學了,唉,繼續努力,下次再有機會,一定不能錯過。

#include<bits/stdc++.h>
using namespace std;
int main() {
	int t,ans,n,num[100005],p,l;
	p = 0;
	l = 1;
	scanf("%d",&t);
	for(int i = 0; i < t; i++) {
		scanf("%d",&num[i]);
	}
	sort(num,num+t);
	for(int i =0; i < t-2;i++){
		if(num[i]  + num[i+1] >num[i+2]){
			p = 1;
		}
	}
	if(p){
		printf("YES\n");
	}else{
		printf("NO\n");
	}
}