传送门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. UVA 1175 - Ladies' Choice

    1175 - Ladies' Choice 链接 稳定婚姻问题. 代码: #include<bits/stdc++.h> using namespace std; typedef long ...

  2. 自动化测试--封装JDBCUnit

    在进行测试的时候,经常需要对数据库进行操作.我们知道,通过代码与数据库交互,需要以下几步: 1.加载驱动 之前有盆友问我,为什么Selenium操作浏览器的时候,非要下载浏览器驱动?为啥对数据库进行操 ...

  3. 系统学习Docker 践行DevOps理念

    Docker代表的容器技术是近两年的大热技术,和人工智能.区块链等热点不同,容器技术的门槛并不高,每一个开发.测试.运维人员都能在日常工作中掌握和使用,是当今IT从业人员的必备技能之一.本课程会带大家 ...

  4. 贝叶斯网(1)尝试用Netica搭建简单的贝叶斯网并使用贝叶斯公式解释各个bar的结果

    近来对贝叶斯网十分感兴趣,按照博客<读懂概率图模型:你需要从基本概念和参数估计开始>给出的第一个例子,试着搭建了一个student网. (1)点击绿F,对条件概率表予以输入(包括两个祖先节 ...

  5. POJ 3076 / ZOJ 3122 Sudoku(DLX)

    Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...

  6. C++STL——set

    一.相关定义 set 集合,有唯一性,即每一个元素只有一个: 是一个有序的容器,里面的元素都是排序好的: 支持插入,删除,查找等操作. 注意 set中的元素可以是任意类型的,但是由于需要排序,所以元素 ...

  7. winform showDialog() 退出问题

    今日发现: 当返回值为Dialog.OK时,会自动退出,不需要this.close().别的返回值仍需要.

  8. Packet filtering with Linux & NAT

    http://www.linuxfocus.org/ChineseGB/May2003/article289.shtml Gateway, Proxy-Arp 和 Ethernet Bridge ? ...

  9. Mybatis学习系列(四)Mapper接口动态代理

    实现原理及规范 Mapper接口动态代理的方式需要手动编写Mapper接口,Mybatis框架将根据接口定义创建接口的动态代理对象,代理对象的方法体实现Mapper接口中定义的方法. 使用Mapper ...

  10. 【Linux】如何设置Linux开机 ,默认进入图形界面或命令行界面?

    原创链接: https://blog.csdn.net/prophet10086/article/details/78501019 [7版本] 在root用户权限下: 查看当前启动模式 systemc ...