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 ...
随机推荐
- [LeetCode]题解(python):075-Sort Colors
题目来源: https://leetcode.com/problems/sort-colors/ 题意分析: 给定n个颜色,红色,白色和蓝色.分别用0,1,2代替,将这些颜色排序,0在1前,1在2前. ...
- wordpress 首页模板变量对应表
最近开始学习wp,这里做一些笔记. 首页模板,第一行为前台显示的html代码.第二行为 wp-content/themes/模板目录 下 head.php文件中. 其中我将变量名 html用绿色标记 ...
- HDU 2104 hide handkerchief
题解:由题目可以知道,如果n和m的最大公约数不为1,那么总有箱子是无法遍历的,所以求一遍GCD就可以判断了. 注意点:一定要记住判断是==,在做题时又忘了. #include <cstdio&g ...
- [转]WIBKIT技术资料
WebKit结构和流程分析 http://inedx.blog.hexun.com/28830354_d.html webkit架构 http://inedx.blog.hexun.com/28795 ...
- C# - String与StringBuilder
A String object is called immutable (read-only), because its value cannot be modified after it has b ...
- WinRT Toolkit 介绍--Control篇
WinRT toolkit是组针对Windows Runtime XAML开发的一系列Control,extension和helper类.它和Windows Phone Toolkit一样,也是由很多 ...
- CSS中 清除浮动解决“包含问题”
今天看到大神对浮动定位作了个详细的分析 刚好就分析了当时任务三遇到的问题 在一个div中定义了三个div,让横向排列,结果父元素div的背景色显示不出来了 ,这是个经典问题----包含浮动 < ...
- the convertion between string and BlobColumn
It's hard to find some samples about the convertion between string and BlobColumn.AddBlobData. It's ...
- Oracle游标-循环查询表中数据(表名),并执行
Oralce 表中存有一个字段,该字段存储表名,要把该表中的所有表名查询出来(即表名结果集),且执行结果集from 表名结果集: declare v_ccount ); --定义一个游标变量 curs ...
- Ubuntu 15.04 设置thin1.6.4作为Rails4.2.5的默认服务器
0. gem install thin //安装1.6.4 1. 进入到Raisl项目的根目录. 2. 在Gemfile中加入一行 gem 'thin' 3. 运行 bundle install 4. ...