Codeforces Round #613 (Div. 2)

A. Mezo Playing Zoma
题目大意:给你两个操作L和R,分别表示当前坐标-1和+1,当然,这些操作可以视为无效,即不移动它的坐标点。现在给你一个操作的字符串,然后进行一系列操作,让你算出有多少种可能的坐标。
思路:计算出它的左右边界,这个区间内的所有点即他可能到达的点。

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin >> n;
	string str;
	cin >> str;
	int l = 0, r = 0;
	for (int i = 0; i < str.length(); i++) {
		if (str[i] == 'L')
			l++;
		else if (str[i] == 'R')
			r++;
	}
	cout << l + r + 1 << endl;
	return 0;
}

B. Just Eat It!
题目:Today, Yasser and Adel are at the shop buying cupcakes. There are n cupcake types, arranged from 1 to n on the shelf, and there are infinitely many of each type. The tastiness of a cupcake of type i is an integer ai. There are both tasty and nasty cupcakes, so the tastiness can be positive, zero or negative.

Yasser, of course, wants to try them all, so he will buy exactly one cupcake of each type.

On the other hand, Adel will choose some segment [l,r] (1≤l≤r≤n) that does not include all of cupcakes (he can’t choose [l,r]=[1,n]) and buy exactly one cupcake of each of types l,l+1,…,r.

After that they will compare the total tastiness of the cupcakes each of them have bought. Yasser will be happy if the total tastiness of cupcakes he buys is strictly greater than the total tastiness of cupcakes Adel buys regardless of Adel’s choice.

For example, let the tastinesses of the cupcakes be [7,4,−1]. Yasser will buy all of them, the total tastiness will be 7+4−1=10. Adel can choose segments [7],[4],[−1],[7,4] or [4,−1], their total tastinesses are 7,4,−1,11 and 3, respectively. Adel can choose segment with tastiness 11, and as 10 is not strictly greater than 11, Yasser won’t be happy 😦

Find out if Yasser will be happy after visiting the shop.

Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.

The first line of each test case contains n (2≤n≤105).

The second line of each test case contains n integers a1,a2,…,an (−109≤ai≤109), where ai represents the tastiness of the i-th type of cupcake.

It is guaranteed that the sum of n over all test cases doesn’t exceed 105.

Output
For each test case, print “YES”, if the total tastiness of cupcakes Yasser buys will always be strictly greater than the total tastiness of cupcakes Adel buys regardless of Adel’s choice. Otherwise, print “NO”.
样例:

输入:
3
4
1 2 3 4
3
7 4 -1
3
5 -5 5
输出:
YES
NO
NO

思路:求最大的区间和,然后比较

#include<iostream>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
	int t; cin >> t;
	while (t--) {
		int n; cin >> n;
		ll sum = 0, dp = 0;
		ll maxn = -inf;
		int l, r;
		int L, R;
		for (int i = 0; i < n; i++) {
			ll x; cin >> x;
			sum += x;
			if (dp <= 0) {
				dp = x;
				l = r = i;
			}
			else {
				dp += x;
				r = i;
			}
			if (maxn < dp) {
				maxn = dp;
				L = l; R = r;
			}
		}
		if (sum > maxn || (sum == maxn && L == 0 && R == n - 1))
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
	return 0;
}

|ू・ω・` )啊啊啊,cf的判题姬挂了,后续的几题随后补上。

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务