比赛链接 https://codeforces.com/contest/1253
比赛记录 https://blog.csdn.net/cheng__yu_/article/details/105395197
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+10,mod=1e9+7;
const int inf=1000;
int t,n;
int a[maxn],b[maxn];
int main()
{
cin>>t;
while(t--)
{
cin>>n;
for(int i=1;i<=n;++i) cin>>a[i];
for(int i=1;i<=n;++i) cin>>b[i];
int ok=1;
for(int i=1;i<=n;++i)
if(b[i]-a[i]<0)
ok=0;
int l=1,r=n;
while(b[l]==a[l])
l++;
while(b[r]==a[r])
r--;
for(int i=l;i<=r;++i)
if(b[i]-a[i]!=b[l]-a[l])
ok=0;
puts(ok?"YES":"NO");
}
return 0;
}
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+10,mod=1e9+7;
const int inf=1000;
int n;
int a[maxn];
int main()
{
cin>>n;
for(int i=1;i<=n;++i) cin>>a[i];
map<int,int> visit;
vector<int> ans;
int last=0,ok=1,cnt=0;
for(int i=1;i<=n;++i)
{
int x=a[i];
if(x>0)
{
if(visit[x])
{
ok=0;
break;
}
visit[x]=1;
cnt++;
}
else
{
x=-x;
if(visit[x]==0||visit[x]==2)
{
ok=0;
break;
}
visit[x]=2;
cnt--;
if(cnt==0)
ans.push_back(i-last),last=i,visit.clear();
}
}
for(auto i : visit)
if(i.second!=2)
ok=0;
if(ok==0) puts("-1");
else
{
cout<<ans.size()<<"\n";
for(auto i : ans)
cout<<i<<" ";
puts("");
}
return 0;
}
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=2e5+10,mod=1e9+7;
const int inf=1000;
int n,m;
ll a[maxn];
ll ans[maxn];
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i)
scanf("%lld",&a[i]);
sort(a+1,a+1+n);
for(int i=m;i<=n;++i)
a[i]+=a[i-m];
for(int i=1;i<=n;++i)
ans[i]=ans[i-1]+a[i];
for(int i=1;i<=n;++i)
cout<<ans[i]<<" ";
return 0;
}
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=2e5+10,mod=1e9+7;
const int inf=1000;
int n,m;
int fa[maxn];
int Find(int x)
{
return fa[x]==x?x:fa[x]=Find(fa[x]);
}
int res=0;
void Union(int x,int y)
{
x=Find(x),y=Find(y);
if(x==y) return;
if(x<y) swap(x,y);
res++;
fa[y]=x;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i) fa[i]=i;
for(int i=1;i<=m;++i)
{
int u,v;
scanf("%d%d",&u,&v);
Union(u,v);
}
int mx=Find(1),cnt=0;
for(int i=2;i<=n;++i)
{
if(i<=mx) cnt++;
mx=max(mx,Find(i));
}
cout<<cnt-res<<"\n";
return 0;
}
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=100+10,mod=1e9+7;
const int maxm=1e5+10;
int n,m;
int x[maxn],s[maxn];
int dp[maxm];
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i)
scanf("%d%d",&x[i],&s[i]);
dp[m+1]=0;
for(int i=m;i>=1;--i)
dp[i]=dp[i+1]+1;
for(int i=m;i>=1;--i)
{
for(int j=1;j<=n;++j)
{
if(i>=x[j])
dp[i]=min(dp[i],dp[min(m+1,x[j]+s[j]+1)]);
else
dp[i]=min(dp[i],max(0,x[j]-s[j]-i)+dp[min(m+1,2*x[j]-i+1)]);
}
}
printf("%d\n",dp[1]);
return 0;
}