1495B - Let's Go Hiking - CodeForces Solution


games greedy *1900

Please click on ads to support us..

C++ Code:

#include<unordered_map>
#include<functional>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<set>
#include<map>

#define fi first
#define se second
#define int long long
#define pii pair<int,int>
#define debug printf("++ ++\n");
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f3f3f3f3f;
const int N = 1e5;
int n;
int a[N + 5];

signed main() {//首先一定是 波峰 其次波峰两边的下降长度相等 否则另一人可应变
	std::ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	int ri = 0, de = 0, timri = 0, timde = 0;
	for (int i = 1; i <= n; i++) {
		int l = i, r = i + 1;
		if (r <= n && a[r] > a[r - 1]) {
			while (r <= n && a[r] > a[r - 1]) r++;
			r--;
			if (r - l + 1 > ri) ri = r - l + 1, timri = 1;
			else if (r - l + 1 == ri) timri++;
		} else if (r <= n && a[r] < a[r - 1]) {
			while (r <= n && a[r] < a[r - 1]) r++;
			r--;
			if (r - l + 1 > de) de = r - l + 1, timde = 1;
			else if (r - l + 1 == de) timde++;
		}
		i = r - 1;
	}
	int tim = 0;
	for (int i = 2; i <= n - 1; i++) {
		if ((a[i] > a[i - 1] && a[i] > a[i + 1])) {
			int pl = i - 1, pr = i + 1;
			while (pl >= 1 && a[pl] < a[pl + 1]) pl--;
			pl++;
			while (pr <= n && a[pr] < a[pr - 1]) pr++;
			pr--;
			if (pr - i + 1 == i - pl + 1 && i - pl + 1 == ri && ri == de) tim++;
		}
	}
	if (tim == 1 && (ri & 1) && timri == timde && timri == 1) cout << "1\n";
	else cout << "0\n";
	return 0;
}
  		 	   						 		      	     	


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST