ZOJ - 3872 Beauty of Array (技巧&模拟)好题
2016-05-01 15:46
281 查看
ZOJ - 3872
Beauty of Array
SubmitStatus
Description
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.
<h4< body="">
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.
<h4< body="">
Output
For each case, print the answer in one line.
<h4< body="">
Sample Input
<h4< body="">
Sample Output
Hint
Source
The 12th Zhejiang Provincial Collegiate Programming Contest
//题意:一个序列的漂亮值的定义为:这个序列中所有不重复的数的和。
给你一个含有n个数的序列,让你找出这个序列的所有连续子序列的漂亮值的和。
//思路:
是一道挺好的题,比赛是队友花了好长时间终于把它A了。
因为n的值很大,所以不可能用两重for循环(肯定会TML),所以就想着用一重for实现。
用sum[i]数组存放前i个数的所有可能的组合的情况的和,用pre[x]数组标记x最后一次出现的位置,通过它来计算x会被累加几次,有了这个思路就可以了。
Beauty of Array
Time Limit: 2000MS | Memory Limit: 65536KB | 64bit IO Format: %lld & %llu |
Description
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.
<h4< body="">
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.
<h4< body="">
Output
For each case, print the answer in one line.
<h4< body="">
Sample Input
3 5 1 2 3 4 5 3 2 3 3 4 2 3 3 2
<h4< body="">
Sample Output
105 21 38
Hint
Source
The 12th Zhejiang Provincial Collegiate Programming Contest
//题意:一个序列的漂亮值的定义为:这个序列中所有不重复的数的和。
给你一个含有n个数的序列,让你找出这个序列的所有连续子序列的漂亮值的和。
//思路:
是一道挺好的题,比赛是队友花了好长时间终于把它A了。
因为n的值很大,所以不可能用两重for循环(肯定会TML),所以就想着用一重for实现。
用sum[i]数组存放前i个数的所有可能的组合的情况的和,用pre[x]数组标记x最后一次出现的位置,通过它来计算x会被累加几次,有了这个思路就可以了。
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<cstring> #include<algorithm> #include<set> #include<vector> using namespace std; long long sum[100000+100],pre[100000+100]; int main() { int t; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); memset(sum,0,sizeof(sum)); memset(pre,0,sizeof(pre)); long long ans=0; for(long long i=1;i<=n;i++) { long long x; scanf("%lld",&x); sum[i]=sum[i-1]+(i-pre[x])*x; pre[x]=i; ans+=sum[i]; } printf("%lld\n",ans); } return 0; }
相关文章推荐
- java语言的跨平台型原理
- 算法学习笔记之约瑟夫环问题
- UML六种关系
- 深入Java单例模式
- 懒加载异常的解决办法
- R数据清理与转换
- 机器学习常见的优化算法
- 架构漫谈(一):什么是架构?
- mod_jk.so下载地址
- C#第七次上机
- gcc(g++)多文件编译的问题
- thinkphp 设置跨域请求
- 驱动
- rapidjson初探——使用C++解析json
- 学习进度(2016.5.1)
- 异常 找不到xsd文件的声明
- workers.properties说明
- Android学习笔记1:创建Hello World项目
- CListCtrl::DeleteColumn
- hdu1061 Rightmost Digit 标准快速幂