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·给你一个集合 ...
随机推荐
- 在编程的时候,NotePad++ 中闪烁的光标突然有竖着闪烁的编程蓝色下划线闪烁的--小技巧告诉你-费元星
当在写代码时出现的光标闪烁(横线闪烁) 在键盘上找 Insert ,按这个Insert就可以把横向闪烁光标( _ )修改成竖向闪烁光标样式( | ),横向光标会在你写代码的时候修改前面的代码,把光标移 ...
- 对工具的反思 & deadlines与致歉
人和动物最大的区别就是使用工具的水平. 有些人只凭着对工具的熟练掌握便成了牛人. 工具,到底应该以何种态度去看待? 在我小的时候,工具仅仅是指树枝.线.粉笔,可以让自己有更多游戏可玩:上学之后,便又有 ...
- jackson 处理空值
@JsonInclude(value=Include.NON_NULL) public class ResultBean 这样在返回数据的时候, { "code": "s ...
- 「暑期训练」「基础DP」 Common Subsequence (POJ-1458)
题意与分析 很简单:求最长公共子序列. 注意子序列与子串的差别:一个不连续一个连续.一份比较好的参考资料见:https://segmentfault.com/a/1190000002641054 状态 ...
- KVM WEB管理工具——WebVirtMgr(二)日常配置
配置宿主机 1.登录WebVirtMgr管理平台 2.添加宿主机 选择首页的WebVirtMgr -->Addd Connection 选择“SSH链接“,设置Label,IP,用户 注意:La ...
- EM算法浅析(二)-算法初探
EM算法浅析,我准备写一个系列的文章: EM算法浅析(一)-问题引出 EM算法浅析(二)-算法初探 一.EM算法简介 在EM算法之一--问题引出中我们介绍了硬币的问题,给出了模型的目标函数,提到了这种 ...
- 【虚拟货币钱包】从 BIP32、BIP39、BIP44 到 Ethereum HD Wallet
干货 | [虚拟货币钱包]从 BIP32.BIP39.BIP44 到 Ethereum HD Wallet {{uploading-image-882090.png(uploading...)}}
- 小程序开发时PC端调试返回结果和手机端IOS不一致问题
IOS11登录时遇到一个请求与PC返回不一致情况, 在小程序调试时IOS上始终没有wx.request() 不能发送请求 尝试解决方法 打开微信小程序调试的设置, 将TLS设为可信任的域名 设置 -- ...
- UVA 11880 Ball in a Rectangle(数学+平面几何)
Input: Standard Input Output: Standard Output � There is a rectangle on the cartesian plane, with bo ...
- 【SSH】——使用ModelDriven的利与弊
在以往的web开发中,如果要在表单显示什么内容,我们就需要在Action中提前定义好表单显示的所有属性,以及一系列的get和set方法.如果实体类的属性非常多,那么Action中也要定义相同的属性.在 ...