D. Extreme Subtraction(思维/差分) Codeforces Round #681

Codeforces Round #681 (Div. 2, based on VK Cup 2019-2020 - Final)
在这里插入图片描述
题意:给定一个长度为n的数组,可以使前k个或后k个元素-1,次数没有限制,问是否可以使所以元素同时为0。

第一次尝试思路:
如果对于中间任何数两侧最小值的和大于等于该数字,输出YES,否则输出NO. 样例过了,结果WA on test 2.
以上结论为可以做到的前提条件之一:某个数字x时,两侧的最小值和必须大于等于x,否则该数为0时会出现负数
思路二:判断该数组是否可以分成 一个n长度不下降数组和一个n长度不上升数组的和。

11月9日更新,用思路二ac了 代码比较麻烦,调了好久。
网上的其他题解都是差分思路。
以下为差分ac代码:

#include<iostream>
#include<algorithm>
#include<iomanip>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<string.h>
#include<string>
#include<math.h>
#define INF 0x7fffffff
#define IOS; {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
#define debug(x) {cout<<"<<<<"<<(x)<<endl;}
#define print(x) {cout<<(x)<<endl;} 
const int N = 1e5+5;
typedef long long ll;
using namespace std;
ll a[N];
ll ans[N];
int main() {
	IOS;
	int t;cin>>t;
	while(t--)
	{
		int n;cin>>n;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
		}
		for(int i=1;i<=n;i++)
		ans[i]=a[i]-a[i-1];
		int res=0;
		for(int i=2;i<=n;i++)
		{
			if(ans[i]<0)res+=(-1)*ans[i];
		}
		if(res<=a[1])cout<<"YES"<<endl;
		else
		cout<<"NO"<<endl;
	} 
	
	return 0;
}
	
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
定义一个结构体类型,用于描述复数结构数据。分别编写函数实现复数的加法和减法运算,在主函数中调用这些函数进行计算并输出计算结果。目前已完成main函数的编写,请编程实现add函数和minus函数。具体要求如下所示。 /* @Filename: ex702.c @Author: Ju Chengdong @Version: 1.0 @Date: 2021-03-18 @Description: Complex Addition and Subtraction */ #include <stdio.h> struct complex{ int re; // real part(实部) int im; // imaginary part(虚部) }; int main(){ struct complex add(struct complex a, struct complex b); struct complex minus(struct complex a, struct complex b); struct complex x, y, z; // 输入实数x的实部和虚部 scanf("%d,%d", &x.re, &x.im); // 输入实数y的实部和虚部 scanf("%d,%d", &y.re, &y.im); // 计算并输出复数加法和 z = add(x, y); printf("%d+%di\n",z.re, z.im); // 计算并输出复数减法差 z = minus(x, y); printf("%d+%di",z.re, z.im); return 0; } /* * 函数名称:add * 函数功能:实现复数加法运算 * 形式参数:struct complex a,操作数 * 形式参数:struct complex b,操作数 * 返 回 值:struct complex型,返回形参a和形参b的加法结果 */ struct complex add(struct complex a, struct complex b){ // 请编程实现本函数 } /* * 函数名称:minus * 函数功能:实现复数减法运算 * 形式参数:struct complex a,操作数 * 形式参数:struct complex b,操作数 * 返 回 值:struct complex型,返回形参a和形参b的减法结果 */ struct complex minus(struct complex a, struct complex b){ // 请编程实现本函数 } 其它说明:无。 【源文件名】ex702.c 【输入形式】输入有两行。 第1行输入两个整数,用逗号分隔,分别表示复数x的实部和虚部 第2行输入两个整数,用逗号分隔,分别表示复数y的实部和虚部
05-24

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值