Gym - 100203G Good elements 水+模拟

本文介绍了一种寻找数组中特定元素(GoodElement)的算法,此类元素的特点是其值能够通过数组中前序部分的任意三个数之和来表达。文章详细解释了使用标记数组进行快速查找的方法,并提供了一个具体的C++实现示例。

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

题意: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 }

 

转载于:https://www.cnblogs.com/macinchang/p/4733478.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值