Codeforces Round #695 (Div. 2) B. Hills And Valleys (思维)

链接

题意:

定义山峰(两边低中间高)和山谷(两边高中间低)。问最少这两种的和是多少?需要可以操作一个数。

分析:

首先我们需要先计算出不改变数会有多少种,和为多少?这个东西是好计算出来的,(就从头跑一遍)

然后我们看每个数改变会改变多少,当下标为x的数改变时他只影响中间点为(x)和(x-1),(x+1).那我们我们就需要看改变这个数会变成什么,为了减少数量有两种选择,一种是变成前一个数,一种是变成后面的那个数,我们只需要判断出来即可。然后枚举一下改变的数。

但是做的时候也不知道在想神马?

// Problem: B. Hills And Valleys
// Contest: Codeforces - Codeforces Round #695 (Div. 2)
// URL: https://codeforces.com/contest/1467/problem/B
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

/// 欲戴皇冠,必承其重。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;

#define x first
#define y second
#define sf scanf
#define pf printf
#define PI acos(-1)
#define inf 0x3f3f3f3f
#define lowbit(x) ((-x)&x)
#define mem(a,x) memset(a,x,sizeof(a))
#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<=(b);++i)
#define repr(i,b,a) for(int i=int(b);i>=(a);--i)
#define debug(x) cout << #x << ": " << x << endl;

const int MOD = 998244353;
const int mod = 998244353;
const int N = 3e5 + 10;
const int dx[] = {0, 1, -1, 0, 0};
const int dy[] = {0, 0, 0, 1, -1};
const int dz[] = {1, -1, 0, 0, 0, 0 };
int day[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

ll n, m;
string str;
ll a[N];
ll ans,tmp;
bool check1(ll x){
	if(x>1&&a[x]>a[x-1]&&x<n&&a[x]>a[x+1]) return 1;
	return 0;
}
bool check2(ll x){
	if(x>1&&a[x]<a[x-1]&&x<n&&a[x]<a[x+1]) return 1;
	return 0;
}
ll juedg(ll x,ll y){
	ll tx=a[x];
	ll l,r;
	l=r=0;
	l+=check1(x)||check2(x);
	l+=check1(x-1)||check2(x-1);
	l+=check1(x+1)||check2(x+1);
	a[x]=a[x+y];
	r+=check1(x)||check2(x);
	r+=check1(x-1)||check2(x-1);
	r+=check1(x+1)||check2(x+1);
	a[x]=tx;
	return l-r;
}
void solve()
{      
	cin>>n;
	ans=0,tmp=0;
	for(int i = 1;i<=n;i++)  scanf("%lld",&a[i]);
	for(int i=2;i<n;i++) ans+=check1(i)||check2(i),tmp=max(tmp,max(juedg(i,-1),juedg(i,1)));
	cout<<ans-tmp<<endl;
    
}
int main()
{
    ll t = 1;
	scanf("%lld", &t);
    while(t--)
    {
        solve();
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值