ZOJ - 3872 Beauty of Array (技巧&模拟)好题
来源:互联网 发布:商城数据库建表 编辑:程序博客网 时间:2024/06/06 01:27
ZOJ - 3872
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 arrayA.
<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 containsN 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
351 2 3 4 532 3 342 3 3 2<h4< body="">
Sample Output
1052138
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;}
0 0
- ZOJ - 3872 Beauty of Array (技巧&模拟)好题
- ZOJ 3872 Beauty of Array(模拟)
- ZOJ 3872 Beauty of Array (技巧)
- ZOJ 3872 Beauty of Array(数学)
- zoj--3872--Beauty of Array(思维)
- ZOJ 3872Beauty of Array
- ZOJ 3872 Beauty of Array
- ZOJ 3872 Beauty of Array
- ZOJ 3872 Beauty of Array
- ZOJ 3872 Beauty of Array
- ZOJ 3872 Beauty of Array
- ZOJ 3872 Beauty of Array(数学啊)
- ZOJ 3872-Beauty of Array(递推||DP)
- ZOJ 3872 Beauty of Array( DP思想 )
- ZOJ-3872Beauty of Array (动态规划)
- ZOJ 3872 Beauty of Array (思维 dp思想)
- ZOJ 3872 — Beauty of Array
- [dp] zoj 3872 Beauty of Array
- apache+tomcat负载均衡
- 疯狂java讲义笔记2:面向对象的基础
- C#第七次上机
- 换个格式输出整数,PAT,1006
- ios面试题3
- ZOJ - 3872 Beauty of Array (技巧&模拟)好题
- java zxing 生成二维码工具类
- C++随笔
- rapidjson初探——使用C++解析json
- java面向对象
- Java 二叉树的前序、中序、后续遍历 递归和迭代实现
- 异常 找不到xsd文件的声明
- 我的大学规划
- 磁盘分区