G - Good elements
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87954#problem/G
Description
You are given a sequence A consisting of N integers. We will call the i-th 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 ( - 105 ≤ Ai ≤ 105).
Output
The first and only line of output must contain the number of good elements in the sequence.
Sample Input
2
1 3
Sample Output
1
HINT
题意
给你一个序列,任意一个数只要等于在它前面的任意三个数之和(可以相同)则称为good数
题解:
有时候能用数组尽量用数组,set还是有祸害的
代码:
#include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <queue> #include <typeinfo> #include <map> #include <stack> typedef long long ll; using namespace std; inline ll read() { ll x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return x*f; } //*************************** int s[5000000]; int main() { int ans=0; int a[50004]; int n=read(); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); for(int j=1;j<i;j++) { if(s[a[i]-a[j]+500000]) { ans++; break; } } for(int j=1;j<i;j++) { s[a[i]+a[j]+500000]=1; } s[a[i]+a[i]+500000]=1; } cout<<ans<<endl; return 0; }