ZOJ 3872 Beauty of Array DP 15年浙江省赛D题
也是一道比赛时候没有写出来的题目,队友想到了解法不过最后匆匆忙忙没有 A 掉
What a pity...
题意:定义Beauty数是一个序列里所有不相同的数的和,求一个序列所有字序列的Beauty和
1 <= N <= 100000
解题思路:由于数据比较大,常规方法求字序列和肯定是行不通的,我们不妨这样想:因为要区别于不同的数
,可以看成序列里的数是一个一个加进去的,每次加入一个数,统计前面序列里第一次出现新加入的这个数的位置,表达的不好,
举个例子:
1 2 3
定义dp(当前元素前面(包括自己)所有包含自己的字序列的和)
定义sum(当前元素前面所有字序列的和,包括此元素)
//输入 1 2 3
//dp 1 5 14
//sum 1 6 20
//a[i] 1 2 3
理解了思路,代码很容易实现,也是比较短,精髓都在for循环里,因为只用了一个for循环,每次新加入一个元素,就可以求出当前所有字序列的Beauty和,所以复杂度为O(n),弱弱把代码贴上,对于没有过掉的可以看一下思路,秒过掉的大神们可以直接无视~~
Source Code:
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0) using namespace std; typedef long long ll ;
typedef unsigned long long ull ;
typedef unsigned int uint ;
typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e- ;
const int N = ;
const int M = * ;
const ll P = 10000000097ll ;
const int MAXN = ; ll a[]; int main () {
int i, j, k, n, m, x, u, v;
cin >> n;
while (n--) {
cin >> m;
memset (a, , sizeof(a));
ll sum = , dp = ;
for (i = ; i <= m; ++i) {
cin >> x;
dp = (i - a[x]) * x + dp;
sum += dp;
a[x] = i;
}
cout << sum << endl;
}
return ;
}
ZOJ 3872 Beauty of Array DP 15年浙江省赛D题的更多相关文章
- DP ZOJ 3872 Beauty of Array
题目传送门 /* DP:dp 表示当前输入的x前的包含x的子序列的和, 求和方法是找到之前出现x的位置(a[x])的区间内的子序列: sum 表示当前输入x前的所有和: a[x] 表示id: 详细解释 ...
- ZOJ 3872 Beauty of Array
/** Author: Oliver ProblemId: ZOJ 3872 Beauty of Array */ /* 需求: 求beauty sum,所谓的beauty要求如下: 1·给你一个集合 ...
- ZOJ 3872 Beauty of Array【无重复连续子序列的贡献和/规律/DP】
Edward has an array A with N integers. He defines the beauty of an array as the summation of all dis ...
- ZOJ 3872 Beauty of Array 连续子序列求和
Edward has an array A with N integers. He defines the beauty of an array as the summation of all dis ...
- ZOJ 3872 Beauty of Array (The 12th Zhejiang Provincial Collegiate Programming Contest )
对于没有题目积累和clever mind的我来说,想解这道题还是非常困难的,也根本没有想到用dp. from: http://blog.csdn.net/u013050857/article/deta ...
- ZOJ 3879 Capture the Flag 15年浙江省赛K题
每年省赛必有的一道模拟题,描述都是非常的长,题目都是蛮好写的... sigh... 比赛的时候没有写出这道题目 :( 题意:首先输入4个数,n,q,p,c代表有n个队伍,q个服务器,每支队伍的初始分数 ...
- Zoj 3842 Beauty of Array
Problem地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5520 根据题目的要求,需要算出所有连续子数组的the be ...
- ZOJ 3777 - Problem Arrangement - [状压DP][第11届浙江省赛B题]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 Time Limit: 2 Seconds Me ...
- 第15届浙江省赛 D Sequence Swapping(dp)
Sequence Swapping Time Limit: 1 Second Memory Limit: 65536 KB BaoBao has just found a strange s ...
随机推荐
- 制作一个vagrant的win7 box
准备: 1.win7镜像文件 2.vagrant安装文件 3.virtual box安装文件 步骤: 1.先在本机上安装virtualbox和vagrant,本机为win7,安装虚机也为win7 2. ...
- Spring 基于注解的装配
xml头文件 xmlns:context="http://www.springframework.org/schema/context" 扫描包:<context:compo ...
- poj 2503 Babelfish(Map、Hash、字典树)
题目链接:http://poj.org/bbs?problem_id=2503 思路分析: 题目数据数据量为10^5, 为查找问题,使用Hash或Map等查找树可以解决,也可以使用字典树查找. 代码( ...
- [置顶] JDK-CountDownLatch-实例、源码和模拟实现
Conception A synchronization aid that allows one or more threads to wait until a set of operations b ...
- Android 建造者(Builder)模式
关于 Builder 模式 详述:http://blog.csdn.net/jjwwmlp456/article/details/39890699 先来张图 看到 Android 中 使用了 Bui ...
- junit学习笔记(二):hamcrest和TestSuit
1. hamcrest hamcrest可以有效增加junit的测试能力,用一些对通俗语言来进行测试. Hamcrest 是一个测试的框架,它提供了一套通用的匹配符 Matcher,灵活使用这些匹配符 ...
- python第三方模块
python相关:1.zeromq网络库:2.twisted框架:twisted:一个基于事件驱动,异步的python高性能网络开发框架:注:什么是基于事件驱动:当(鼠标点击事件)事件注册器注入事件, ...
- c#简单的调试信息、日志信息输出
public static void ErrorLog(string mssg) { string FilePath = "D:/logs/ErrorLog.txt"; try { ...
- C# inherit
Case:class A has a construct. class B is inherit from class A and B also has a construct. What's the ...
- 个人自建网店(WordPress WooCommerce on SAE)集成支付宝支付
插件: Alipay For WooCommerce 到支付宝账户的商家服务提出申请: https://b.alipay.com/order/serviceIndex.htm 在"在线签约- ...