Gym - 100203G Good elements 水+模拟
题意:good element的定义是a[i]在1~i-1中任取三个数(可以重复)的和能等于a[i]
思路:vis[x]标记一下任两个数的和,处理a[i]时枚举1~i-1判断vis[a[i] - a[j]]是否被标记
1 #include <iostream> 2 #include <cstdio> 3 #include <fstream> 4 #include <algorithm> 5 #include <cmath> 6 #include <deque> 7 #include <vector> 8 #include <queue> 9 #include <string> 10 #include <cstring> 11 #include <map> 12 #include <stack> 13 #include <set> 14 #define LL long long 15 #define eps 1e-8 16 #define INF 0x3f3f3f3f 17 #define MAXN 200005 18 using namespace std; 19 int a[5005]; 20 bool vis[400005]; 21 int main() 22 { 23 #ifndef ONLINE_JUDGE 24 freopen("in.txt", "r", stdin); 25 //freopen("out.txt", "w", stdout); 26 #endif // OPEN_FILE 27 int n; 28 scanf("%d", &n); 29 for(int i = 1; i <= n; i++){ 30 scanf("%d", &a[i]); 31 } 32 int ans = 0; 33 for(int i = 1; i <= n; i++){ 34 for(int j = 1; j <= i - 1; j++){ 35 if(!vis[a[i] - a[j] + MAXN]) continue; 36 ans++; 37 break; 38 } 39 for(int j = 1; j <= i; j++){ 40 vis[a[i] + a[j] + MAXN] = true; 41 } 42 } 43 printf("%d\n", ans); 44 }