HDU 5496 - Beauty of Sequence (序列 + 统计贡献)
来源:互联网 发布:javascript异步编程 编辑:程序博客网 时间:2024/06/05 10:52
题意
统计所有子序列的和,子序列中连续的重复元素消去只剩一个。
思路
看了snowy_smile巨巨在比赛时写的代码,每行代码都有注释,仰慕。
考虑每个元素的贡献。
当前元素后面当然可以随便选,有
如果当前元素是第一个不重复的元素,那么显然前面
如果不是,我们要从前面所有的情况里减去重复算的情况。
重复算的情况就是same[上一个]。
下面考虑如何维护same数组。
考虑累计到当前位置,包含当前元素的唯一情况数same[i]。也就是说,在same[i]包含的情况中,再加上当前的元素是没用的。same[i]由两部分组成
- 不包含当前位置的元素u,就是same[上一个u]。
- 包含当前位置的元素u,就是
2i−1
把两部分相加就是最终的结果。
代码
#include <stack>#include <cstdio>#include <list>#include <cassert>#include <set>#include <fstream>#include <iostream>#include <string>#include <sstream>#include <vector>#include <queue>#include <functional>#include <cstring>#include <algorithm>#include <cctype>#pragma comment(linker, "/STACK:102400000,102400000")#include <string>#include <map>#include <cmath>//#include <ext/pb_ds/assoc_container.hpp>//#include <ext/pb_ds/hash_policy.hpp>using namespace std;//using namespace __gnu_pbds;#define LL long long#define ULL unsigned long long#define SZ(x) (int)x.size()#define lowbit(x) ((x) & (-x))#define MP(a, b) make_pair(a, b)#define MS(p, num) memset(p, num, sizeof(p))#define PB push_back#define X first#define Y second#define ROP freopen("input.txt", "r", stdin);#define MID(a, b) (a + ((b - a) >> 1))#define LC rt << 1, l, mid#define RC rt << 1|1, mid+1, r#define LRT rt << 1#define RRT rt << 1|1#define FOR(i, a, b) for (int i=(a); (i) < (b); (i)++)#define FOOR(i, a, b) for (int i = (a); (i)<=(b); (i)++)const double PI = acos(-1.0);const int INF = 0x3f3f3f3f;const double eps = 1e-8;const int MAXN = 1e5+10;const int MOD = 1e9+7;const int dir[][2] = { {1, 0}, {0, -1}, {-1, 0}, {0, 1} };const int seed = 131;int cases = 0;typedef pair<int, int> pii;LL b[MAXN], same[MAXN];map<int, int> mp;int main(){ //ROP; b[0] = 1; for (int i = 1; i < MAXN; i++) b[i] = b[i-1] * 2 % MOD; int T; scanf("%d", &T); while (T--) { int n; mp.clear(); scanf("%d", &n); LL ans = 0; for (int i = 1; i <= n; i++) { int tmp; scanf("%d", &tmp); if (!mp.count(tmp)) { same[i] = 0; ans = (ans + b[i-1]*b[n-i]%MOD*tmp) % MOD; } else { ans = (ans + (b[i-1]-same[mp[tmp]])*b[n-i]%MOD*tmp) % MOD; same[i] = same[mp[tmp]]; } same[i] = (same[i] + b[i-1]) % MOD; mp[tmp] = i; } printf("%lld\n", (ans+MOD) % MOD); } return 0;}
0 0
- HDU 5496 - Beauty of Sequence (序列 + 统计贡献)
- hdu 5496 Beauty of Sequence
- HDU 5496 Beauty of Sequence
- HDU 5496 Beauty of Sequence
- hdu 5496 Beauty of Sequence 枚举
- hdu 5496 Beauty of Sequence (map维护)
- hdu 5496 Beauty of Sequence 组合数学
- HDU 5496(Beauty of Sequence-DP)
- hdu 5496 Beauty of Sequence(高效)
- HDU 5496 Beauty of Sequence 线性DP
- hdu 5496 Beauty of Sequence bc#58 组合 离散化
- hdoj 5496 Beauty of Sequence
- hdoj 5496 Beauty of Sequence 【求序列所有子序列(去重后)的和】【好题】
- hdu5496 Beauty of Sequence
- hdu Beauty of Sequence (好题_集合问题)
- HDU5496--Beauty of Sequence(水题)
- HDOJ 题目5496 Beauty of Sequence(数学)
- 哈希+前缀和 hdu5496 Beauty of Sequence
- Hive笔记七:Hive的数据导出
- session缓存时间的两种设置方式
- 海量数据挖掘MMDS week2: LSH的距离度量方法
- Head First设计模式—策略模式
- 解决 IDEA 中src下xml等资源文件无法读取的问题
- HDU 5496 - Beauty of Sequence (序列 + 统计贡献)
- Java对象在JVM中的生命周期
- 133 Clone Graph [Leetcode]
- C语言——接口设计原则
- DOM 元素节点几何量与滚动几何量
- 【C++】c++写时拷贝Copy On Write
- [经济学原理|政治部分]社会资本再生产理论
- Hive笔记八:Hive体系结构
- 检查网络连接状态