蒟蒻的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
剩下的我菜了