Divide the Sequence
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 702 Accepted Submission(s): 368
Total Submission(s): 702 Accepted Submission(s): 368
题意:把所给数列分成m段,使m尽可能的大,要求是每段的前缀和为非负,
思路:从后往前,遇到>=0的和 计数加加;
Problem Description
思路:从后往前,遇到>=0的和 计数加加;
Problem Description
Alice has a sequence A, She wants to split A into as much as possible continuous subsequences, satisfying that for each subsequence, every its prefix sum is not small than 0.
Input
The input consists of multiple test cases.
Each test case begin with an integer n in a single line.
The next line contains n integers A1,A2⋯An .
1≤n≤1e6
−10000≤A[i]≤10000
You can assume that there is at least one solution.
Each test case begin with an integer n in a single line.
The next line contains n integers A1,A2⋯An .
1≤n≤1e6
−10000≤A[i]≤10000
You can assume that there is at least one solution.
Output
For each test case, output an integer indicates the maximum number of sequence division.
Sample Input
6 1 2 3 4 5 6 4 1 2 -3 0 5 0 0 0 0 0
Sample Output
6 2 5
Author
ZSTU
Source
Recommend
Statistic | Submit | Discuss | Note
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cctype>
#define LL long long
using namespace std;
const int N=1e6+10;
LL A[N];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
scanf("%lld",&A[i]);
int ans=0;
int i=n;
LL tmp;
while(i>0)
{
tmp=A[i--];
while(tmp<0)
tmp+=A[i--];
ans++;
}
printf("%d\n",ans);
}
return 0;
}
Two
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1161 Accepted Submission(s): 536
Problem Description
Alice gets two sequences A and B. A easy problem comes. How many pair of sequence A' and sequence B' are same. For example, {1,2} and {1,2} are same. {1,2,4} and {1,4,2} are not same. A' is a subsequence of A. B' is a subsequence of B. The subsequnce can be not continuous. For example, {1,1,2} has 7 subsequences {1},{1},{2},{1,1},{1,2},{1,2},{1,1,2}. The answer can be very large. Output the answer mod 1000000007.
Input
The input contains multiple test cases.
For each test case, the first line cantains two integers N,M(1≤N,M≤1000) . The next line contains N integers. The next line followed M integers. All integers are between 1 and 1000.
For each test case, the first line cantains two integers N,M(1≤N,M≤1000) . The next line contains N integers. The next line followed M integers. All integers are between 1 and 1000.
Output
For each test case, output the answer mod 1000000007.
Sample Input
3 2 1 2 3 2 1 3 2 1 2 3 1 2
Sample Output
2 3
Author
ZSTU
Source
Recommend
#include <iostream>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#define LL long long
using namespace std;
const int N=1e3+10;
int A[N];
int B[N];
const LL MOD=1e9+7;
LL dp[N][N];
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
for(int i=1;i<=n;i++)
scanf("%d",&A[i]);
for(int i=1;i<=m;i++)
scanf("%d",&B[i]);
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(A[i]==B[j])
{
(dp[i][j]=(dp[i-1][j]+dp[i][j-1])+1)%=MOD;
}
else
(dp[i][j]=(dp[i][j-1]+dp[i-1][j]-dp[i-1][j-1])%MOD+MOD)%=MOD;
}
}
//deque<>
printf("%I64d\n",dp[n][m]);
}
return 0;
}
World is Exploding
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 666 Accepted Submission(s): 315
容斥原理;
Problem Description
Given a sequence A with length n,count how many quadruple (a,b,c,d) satisfies:
a≠b≠c≠d,1≤a<b≤n,1≤c<d≤n,Aa<Ab,Ac>Ad
.
Input
The input consists of multiple test cases.
Each test case begin with an integer n in a single line.
The next line contains n integers A1,A2⋯An .
1≤n≤50000
0≤Ai≤1e9
Each test case begin with an integer n in a single line.
The next line contains n integers A1,A2⋯An .
1≤n≤50000
0≤Ai≤1e9
Output
For each test case,output a line contains an integer.
Sample Input
4 2 4 1 3 4 1 2 3 4
Sample Output
1 0
Author
ZSTU
Source
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#define LL long long
using namespace std;
const int N=1e5+10;
int A[N];
int B[N];
int rg[N]; ///rg(x)=∣{y∣x<y<=n,Ax<Ay}∣
int lg[N];/// {y∣1<=y<x,Ax<Ay}
int rs[N];
int ls[N];
int allg[N];
int sum[N];
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int num,int n)
{
while(x<=n)
{
sum[x]+=num;
x+=lowbit(x);
}
}
int getsum(int x)
{
int s=0;
while(x>0)
{
s+=sum[x];
x-=lowbit(x);
}
return s;
}
int main()
{
int n;
int k;
LL sum1;
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
{
scanf("%d",&A[i]);
B[i]=A[i];
}
sort(B,B+n);
k=unique(B,B+n)-B;
for(int i=0;i<n;i++)
A[i]=lower_bound(B,B+k,A[i])-B+1;
memset(ls,0,sizeof(ls));
memset(lg,0,sizeof(ls));
memset(rs,0,sizeof(ls));
memset(rg,0,sizeof(ls));
memset(sum,0,sizeof(sum));
LL a=0,b=0;
for(int i=0;i<n;i++)
{
ls[i+1]=getsum(A[i]-1);
lg[i+1]=getsum(k)-getsum(A[i]);
update(A[i],1,k);
a+=ls[i+1];
}
sum1=0;memset(sum,0,sizeof(sum));
for(int i=n-1;i>=0;i--)
{
rs[i+1]=getsum(A[i]-1);
rg[i+1]=getsum(k)-getsum(A[i]);
update(A[i],1,k);
sum1+=rs[i];
b+=rs[i+1];
}
LL ans=0;
for(int i=1;i<=n;i++)
{
ans-=(ls[i]*rs[i]+rg[i]*rs[i]+rg[i]*lg[i]+ls[i]*lg[i]);
}
ans+=a*b;
printf("%I64d\n",ans);
}
return 0;
}