强大的队友构造出来的
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
const int mod=1e9+7;
const int maxn=1e5+10;
int a[maxn],cnt;
ll s[maxn];
int t,n,p;
int main()
{
cin>>t;
while(t--)
{
scanf("%d %d",&n,&p);
int k=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
s[i]=s[i-1]+a[i];
if(s[i]<p)
k=i;
}
int sum=0,m=0;
for(k=k+1;k<=n;k++)
{
for(int i=m;i<k;i++)
{
if((s[k]-s[i])%p==0)
{
sum++,m=k;
break;
}
else if(s[k]-s[i]<p)
break;
}
}
printf("%d\n",sum);
}
return 0;
}
1005.Little W and Contest
并查集瞎搞
#include<bits/stdc++.h>
using namespace std;
const int N = 100000 + 5;
const int mod = (int)1e9 + 7;
long long t,a[N],b[N],r[N],n,s,fa[N];
int fin(int x)
{
if(x==fa[x])
{
return x;
}
else
{
return fa[x]=fin(fa[x]);
}
}
void mege(int x,int y)
{
fa[fin(x)]=fin(y);
}
int main()
{
cin>>t;
while(t--)
{
cin>>n;
s=0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
if(a[i]==2)
s++;
fa[i]=i;
if(a[i]==2)
{
b[i]=1;
r[i]=0;
}
else
{
r[i]=1;
b[i]=0;
}
}
long long ans;
ans=s*(s-1)*(n-s)/2+s*(s-1)*(s-2)/6;
ans%=mod;
cout<<ans<<endl;
for(int i=1;i<=n-1;i++)
{
int u,v;
cin>>u>>v;
int p,q;
p=fin(u);
q=fin(v);
if(p!=q)
{
int c=b[p],d=b[q],e=r[p],f=r[q];
mege(u,v);
b[fin(u)]=c+d;
r[fin(u)]=e+f;
ans-=c*d*(n-s-e-f)%mod+c*f*(s-c-d)%mod+d*e*(s-c-d)%mod+c*d*(s-c-d)%mod;
ans%=mod;
ans+=mod;
ans%=mod;
}
cout<<ans<<endl;
}
}
return 0;
}
1009.Parentheses Matching
贪心
#include<bits/stdc++.h>
using namespace std;
const int N = 100000 + 5;
const int mod = (int)1e9 + 7;
#define ll long long
#define endl '\n'
long long t,n,s,k[N],sum,l;
char a[N];
int main()
{
std::ios::sync_with_stdio(false);
cin>>t;
while(t--)
{
cin>>a;
n=strlen(a);
s=0;
l=0;
sum=0;
int flag=1;
for(int i=0;i<n;i++)
{
if(a[i]=='*')
{
s++;
k[s]=i;
}
else if(a[i]=='(')
{
sum++;
}
else
{
if(sum==0)
{
l++;
if(l<=s)
a[k[l]]='(';
else
{
flag=0;
break;
}
}
else
sum--;
}
}
if(flag==0)
cout<<"No solution!"<<endl;
else if(sum>s-l)
cout<<"No solution!"<<endl;
else
{
s=0;
l=0;
sum=0;
flag=1;
for(int i=n-1;i>=0;i--)
{
if(a[i]=='*')
{
s++;
k[s]=i;
}
else if(a[i]==')')
{
sum++;
}
else
{
if(sum==0)
{
l++;
if(l<=s)
a[k[l]]=')';
else
{
flag=0;
break;
}
}
else
sum--;
}
}
if(flag==0)
cout<<"No solution!"<<endl;
else
{
for(int i=0;i<n;i++)
{
if(a[i]!='*')
cout<<a[i];
}
cout<<endl;
}
}
}
return 0;
}