DOBRI 简单的dp

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

21 361 2 3 5 7 103-1 2 0

Sample Output

141

简单动态规划思想
#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;    
} 






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值