蒟蒻打cf#688 div2

蒟蒻的cf从0开始
A题
water
I am water
大家同时出发,如果要相撞,必有出发横坐标==纵坐标,比较输入的两个数组有多少个元素相同即为答案。

#include<bits/stdc++.h>
using namespace std;
#define rei register int
typedef  long long LL;
inline void read(int &x)
{
	int f=1;char s=getchar();
	while(s>'9'||s<'0'){f=-1;s=getchar();}
	while(s>='0'&&s<='9'){x=x*10+s-48;s=getchar();}
	x*=f;
}
bool find(int *s,int x,int n)
{
	for(int i=1;i<=n;i++) if(x==s[i]) return true;
	return false;
}
int main()
{
	int t;
	int n,m;
	int ans;
	int l[110];
	int x;
	cin>>t;
	while(t--)
	{
		ans=0;
		cin>>n>>m;
		for(int i=1;i<=n;i++)
		cin>>l[i];
		for(int i=1;i<=m;i++) 
		{
			cin>>x;
			if(find(l,x,n)) ans++;
		}
		printf("%d\n",ans);
	} 
	return 0;
}

B题
先想想没有这句话怎么做
you have the option of changing one of the integers in the array to any other integer.
由题是通过后缀来改变数组,可以直接跑一遍数组的数组的差分和sum( abs ( s[ i ]-s[ i-1 ] ) )就可了。
Then?
枚举每个位置think think把它修改成与它相邻的数后的答案。即找一下abs(st[i]-st[i-1])+abs(st[i-1]-st[i-2])-abs(st[i]-st[i-2])的max值,用差分和减一下即可。
Why?

#include<bits/stdc++.h>
using namespace std;
typedef  long long LL;
int main()
{
	int t;
	int n;
	LL ans;
	LL r;
	LL st[201000];
	cin>>t;
	while(t--)
	{
		ans=0;
		r=-0x7ff;
		cin>>n;
		for(int i=1;i<=n;i++)
		cin>>st[i];
		if(n==2) printf("0\n");
		else 
		{
			for(int i=2;i<=n;i++)
			ans+=abs(st[i]-st[i-1]);
			r=max(r,abs(st[1]-st[2]));
			for(int i=3;i<=n;i++)
			r=max(r,abs(st[i]-st[i-1])+abs(st[i-1]-st[i-2])-abs(st[i]-st[i-2]));
			r=max(r,abs(st[n]-st[n-1]));
			printf("%lld\n",ans-r);
		}
	}
	return 0;
}

注意边界特判
CDEF
剩下的我菜了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值