Description
You are given a sequence A consisting of N integers (not to be confused with the sequence from the
previous task). We will call the i
th
sequence element good if it equals the sum of some three elements
in positions strictly smaller than i (an element can be used more than once in the sum).
How many good elements does the sequence contain?
Input
The first line of input contains the positive integer N (1 ≤ N ≤ 5000), the length of the sequence A.
The second line of input contains N space-separated integers representing the sequence A (-100 000 ≤
A
i
≤ 100 000).
Output
The first and only line of output must contain the number of good elements in the sequence.
Sample Input
Sample Output
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int a[6000];
bool p[410000]; //进行的是其余两个的存储
int main()
{
int n;
while(scanf("%d",&n)!=EOF){
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
memset(p,0,sizeof(p));
int ans=0;
for(int i=0;i<n;i++){
for(int j=0;j<i;j++)if(p[ a[i]-a[j]+200000] == 1) {ans++;break;} //a[i]作为目标值 p[t]代表的是 t(有两个数相加)是否存在
for(int j=0;j<=i;j++)p[ a[i]+a[j]+200000 ]=1; //a[i]作为其中的一个元素
}
cout<<ans<<endl;
}
return 0;
}

本文介绍了一种使用动态规划解决特定序列问题的方法,通过计算序列中可以由前面三项之和构成的元素数量来解决问题。

被折叠的 条评论
为什么被折叠?



