为啥全是求方案总数的题(晕)
T1
我们可以用暴力来找规律,会发现:
n | ans |
---|---|
1 | 1+2=3 |
2 | 1+2+3=6 |
3 | 1+2+3+4=10 |
4 | 1+2+3+4+5=15 |
… | … |
总结起来可得出公式:
a
n
s
=
(
n
+
1
)
∗
(
n
+
2
)
/
2
ans=(n+1)*(n+2)/2
ans=(n+1)∗(n+2)/2
A C C o d e AC~Code AC Code
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
long long n;
int main()
{
freopen("h2o.in","r",stdin);
freopen("h2o.out","w",stdout);
cin>>n;
cout<<(n+1)*(n+2)/2; //公式
return 0;
}
T2
A C C o d e AC~Code AC Code
未做出
T3
A C C o d e AC~Code AC Code
未做出
T4
这道题以前考试做过(<_<)
前缀和+离散化+树状数组
A C C o d e AC~Code AC Code
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
long long a[100010],f[100010];
long long n,m,c,ans,w;
struct node
{
long long sum,place;
}b[100010];
bool cmp(node a,node b)
{
return a.sum<b.sum;
}
bool cmp2(node a,node b)
{
return a.place<b.place;
}
int main()
{
freopen("sequence.in","r",stdin);
freopen("sequence.out","w",stdout);
cin>>n>>m;
for(int i=1; i<=n; i++)
{
scanf("%lld",&a[i]);
a[i]-=m; //首先统计一轮单个
b[i].sum=b[i-1].sum+a[i]; //前缀和
b[i].place=i; //离散化
if(b[i].sum>=1) //单个比较
ans++;
}
sort(b+1,b+1+n,cmp);
for(int i=1; i<=n; i++)
{
if(b[i].sum!=w||i==1)
c++;
w=b[i].sum;
b[i].sum=c; //算次数
}
sort(b+1,b+1+n,cmp2);
for(int i=1; i<=n; i++)
{
for(int j=b[i].sum-1; j>=1; j-=j&(-j)) //树状数组
ans+=f[j];
for(int j=b[i].sum; j<=c; j+=j&(-j))
f[j]++;
}
cout<<ans;
return 0;
}