ZOJ3872 Beauty of Array---规律 | DP| 数学能力
传送门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| 数学能力的更多相关文章
- 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 ...
- 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 ...
- Rigid Frameworks (画图二分图规律 + DP + 数学组合容斥)
题意:方格n*m,然后对于每一个格子有3种画法1左对角线2右对角线3不画,求让图形稳定的画法有多少种? 思路:通过手画二分图可以发现当二分图联通时改图满足条件,然后我们对于一个dp[n][m]可以利用 ...
- DP ZOJ 3872 Beauty of Array
题目传送门 /* DP:dp 表示当前输入的x前的包含x的子序列的和, 求和方法是找到之前出现x的位置(a[x])的区间内的子序列: sum 表示当前输入x前的所有和: a[x] 表示id: 详细解释 ...
- # E. Mahmoud and Ehab and the xor-MST dp/数学+找规律+xor
E. Mahmoud and Ehab and the xor-MST dp/数学/找规律 题意 给出一个完全图的阶数n(1e18),点由0---n-1编号,边的权则为编号间的异或,问最小生成树是多少 ...
- 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 ...
- 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 ...
- 第十二届浙江省大学生程序设计大赛-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. ...
- ZOJ 3872 Beauty of Array
/** Author: Oliver ProblemId: ZOJ 3872 Beauty of Array */ /* 需求: 求beauty sum,所谓的beauty要求如下: 1·给你一个集合 ...
随机推荐
- 在List中删除符合条件的内容
objDAList.RemoveAll(s => s.daCID == "20170725152407CD");
- ubuntu 把软件源修改为国内源
国内有很多Ubuntu的镜像源,比如:阿里源.网易源等,还有很多教育网的源,比如:清华源.中科大源等. 这里以清华源为例讲解如何修改Ubuntu 18.04里面默认的源. 修改步骤 第一步:备份原始源 ...
- 关于Scala文件操作中出现的问题
在各种项目中,我们常常需要用到文件操作,笔者在近期的项目中遇到了一个与文件操作相关的问题. 在代码实现的过程中,笔者首先定义了一个文件路径:def PATH = "/a/b/c.txt&qu ...
- 深度可分卷积(Depthwise Separable Conv.)计算量分析
上次读到深度可分卷积还是去年暑假,各种细节都有些忘了.记录一下,特别是计算量的分析过程. 1. 标准卷积和深度可分卷积 标准卷积(MobileNet论文中称为Standard Convolution, ...
- erc20代币合约
看这篇文章需要对以太坊,智能合约,代币等概念有基本的了解. 什么是ERC20 可以把ERC20简单理解成以太坊上的一个代币协议,所有基于以太坊开发的代币合约都遵守这个协议.遵守这些协议的代币我们可以认 ...
- apollo命令行传入参数
Java apollo客户端运行配置 需要在META-INF中创建app.properties文件,以配置app.id 还需要在/opt/settings/server.properties或C:/o ...
- AMF3 在Unity中使用AMF3和Java服务器通信
现在在做的项目是一个网页游戏的移植到移动端. 所以服务器直接使用原来的代码.原来的游戏是as3实现,使用flash amf3数据通信. Unity 使用C#作为脚本语言,所以就需要.net的amf3解 ...
- jquery UI 跟随学习笔记——拖拽(Draggable)
引言 这周暂时没有任务下达,所以老大给我的任务就是熟悉jquery相关插件,我就先选择了jquery UI插件,以及jquery库学习. 我用了两天的时候熟悉Interactions模块中的Dragg ...
- PAT 1088 三人行
https://pintia.cn/problem-sets/994805260223102976/problems/1038429286185074688 子曰:“三人行,必有我师焉.择其善者而从之 ...
- PAT 1005 继续(3n+1)猜想
https://pintia.cn/problem-sets/994805260223102976/problems/994805320306507776 卡拉兹(Callatz)猜想已经在1001中 ...