传送门ZOJ 3872

Beauty of Array


Time Limit: 2 Seconds      Memory Limit: 65536 KB

Edward has an array A with N integers. He defines the beauty of an array as the summation of all distinct integers in the array. Now Edward wants to know the summation of the beauty of all contiguous subarray of the array A.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains an integer N (1 <= N <= 100000), which indicates the size of the array. The next line contains N positive integers separated by spaces. Every integer is no larger than 1000000.

Output

For each case, print the answer in one line.

Sample Input

3
5
1 2 3 4 5
3
2 3 3
4
2 3 3 2

Sample Output

105
21
38

Author: LIN, Xi
Source: The 12th Zhejiang Provincial Collegiate Programming Contest

Time Limit Exceeded  版

暴力

#include "cstdio"
#include "cstring"
#include "iostream"
#include "map"
using namespace std;
#define N 100005
int ans[N];
map<int,int> m;
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
m.clear();
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d",&ans[i]);
}
int sum=;
int temp;
for(int i=;i<n;i++)
{
m[ans[i]]++;
temp=ans[i];
sum+=temp;
for(int j=i+;j<n;j++)
{
if(m.find(ans[j])==m.end())
{
m[ans[j]]++;
temp+=ans[j];
}
sum+=temp;
}
m.clear();
}
printf("%d\n",sum);
}
}

AC版

分析:

找规律:

数列如 1 2 3

前1个数  1,和为1

前2个数  1,2,加入之后有新子序列 2  1,2  即子序列之和 较次态多加 2*2(一个为值,一个为个数)

前3个数  1,2,3  加入之后有新子序列  3  2,3  1,2,3  即子序列之和 较次态多加 3*3(一个为值,一个为个数) 

数列如 2 3 3

前1个数  2,和为2

前2个数  2,3,加入之后有新子序列 3  2,3  即子序列之和 较次态多加 3*2(一个为值,一个为个数)

前3个数  2,3,3  加入之后有新子序列  3  3,3  2,3,3 !!! 即子序列之和 较次态多加 3*(新加3的位置-前面出现3的最大位置),因为前面最后一个三之前的所有组合 再跟此时3组合,与新加数重复,不再加

2

3

2 3

3

3 3

2 3 3

得21

这样次态与现态的递推关系,就是DP问题了

而得出这种递推关系,就需要发掘规律

发掘规律,需要良好的数学思维

#include <cstdio>
#include <cstring>
#include "iostream"
using namespace std;
#define LL long long
int main()
{
int t,n;
int w[];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int x;
LL dp=,sum=;
memset(w,,sizeof(w));
for(int i=;i<=n;i++)
{
scanf("%d",&x);
dp+=(i-w[x])*x;
sum+=dp;
w[x]=i;
}
printf("%lld\n",sum);
}
}

ZOJ3872 Beauty of Array---规律 | DP| 数学能力的更多相关文章

  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 di ...

  2. AndyQsmart ACM学习历程——ZOJ3872 Beauty of Array(递推)

    Description Edward has an array A with N integers. He defines the beauty of an array as the summatio ...

  3. Rigid Frameworks (画图二分图规律 + DP + 数学组合容斥)

    题意:方格n*m,然后对于每一个格子有3种画法1左对角线2右对角线3不画,求让图形稳定的画法有多少种? 思路:通过手画二分图可以发现当二分图联通时改图满足条件,然后我们对于一个dp[n][m]可以利用 ...

  4. DP ZOJ 3872 Beauty of Array

    题目传送门 /* DP:dp 表示当前输入的x前的包含x的子序列的和, 求和方法是找到之前出现x的位置(a[x])的区间内的子序列: sum 表示当前输入x前的所有和: a[x] 表示id: 详细解释 ...

  5. # E. Mahmoud and Ehab and the xor-MST dp/数学+找规律+xor

    E. Mahmoud and Ehab and the xor-MST dp/数学/找规律 题意 给出一个完全图的阶数n(1e18),点由0---n-1编号,边的权则为编号间的异或,问最小生成树是多少 ...

  6. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Beauty of Array

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5496 The 12th Zhejiang Provincial ...

  7. 2015 浙江省赛 Beauty of Array (思维题)

    Beauty of Array Edward has an array A with N integers. He defines the beauty of an array as the summ ...

  8. 第十二届浙江省大学生程序设计大赛-Beauty of Array 分类: 比赛 2015-06-26 14:27 12人阅读 评论(0) 收藏

    Beauty of Array Time Limit: 2 Seconds Memory Limit: 65536 KB Edward has an array A with N integers. ...

  9. ZOJ 3872 Beauty of Array

    /** Author: Oliver ProblemId: ZOJ 3872 Beauty of Array */ /* 需求: 求beauty sum,所谓的beauty要求如下: 1·给你一个集合 ...

随机推荐

  1. 3 web服务器:静态文件

    1.处理客户端请求数据 >>> s = "GET / HTTP/1.1\r\nHost: 127.0.0.1:8080\r\nConnection: keep-alive& ...

  2. python内置模块[re]

    python内置模块[re] re模块: python的re模块(Regular Expression正则表达式)提供各种正则表达式的匹配操作,在文本解析.复杂字符串分析和信息提取时是一个非常有用的工 ...

  3. 基于Mysql-Proxy实现Mysql的主从复制以及读写分离(上)

    基于Mysql-Proxy实现Mysql的主从复制以及读写分离(上) 上周BOSS给分配任务让实现一下Mysql数据库的主从复制以及读写分离,然后花了一盏茶的功夫进行了调研,发现主从复制数据库进行一番 ...

  4. python第一天(安装运行python)

    1. 安装Python 3.7 目前,Python有两个版本,一个是2.x版,一个是3.x版,这两个版本是不兼容的.由于3.x版越来越普及,我们的教程将以最新的Python 3.7版本为基础.请确保你 ...

  5. HDU 4725 The Shortest Path in Nya Graph(最短路径)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)

    Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...

  6. Windows下LATEX排版论文攻略—CTeX、JabRef使用心得

    笔者刚刚接触到TEX排版,相关知识完全空白,用了两天时间学习并完成了一篇论文的完整排版. 期间遇到不少小问题,着实辛苦,分享至上,现将其解决办法总结归纳,共同学习. 一.工具介绍 TeX是一个很好排版 ...

  7. Android问题:could not install *smartsocket* listener;Address already in use

     今天启动genymotion后,发现没有ip地址,运行项目时报错:     可见,没有连接到模拟器,无法运行,而先前说过没有ip,自然而然连接不上,   解决放法:将资源管理器打开,将adb全部退出 ...

  8. JS让网页上文字出现键盘打字的打字效果

    一个挺简单的网页特效:JS让网页上文字出现键盘打字的打字效果实现 演示地址:http://codepen.io/guihailiuli/pen/jPOYMZ 以代码形式实现过程分析: <html ...

  9. Node js 安装+回调函数+事件

    /* 从网站 https://nodejs.org/zh-cn/ 下载 这里用的 9.4.0 版本 下载完安装 安装目录是 D:\ApacheServer\node 一路默认安装 安装后打开cmd命令 ...

  10. REST接口设计规范

    URI格式规范 URI(Uniform Resource Identifiers) 统一资源标示符 URL(Uniform Resource Locator) 统一资源定位符 URI的格式定义如下: ...