DOBRI 簡單的dp

來源:互聯網
上載者:User
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 Input21 361 2 3 5 7 103-1 2 0Sample Output141
簡單動態規劃思想
#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;    } 




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.