ACM-水题 Beauty of Array
2016-04-03 16:21
176 查看
题目:
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.
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
Sample Output
【题目大意】定义Beauty数是一个序列里所有不相同的数的和,求一个序列所有字序列的Beauty和
1 <= N <= 100000
【解题思路】由于数据比较大,常规方法求字序列和肯定是行不通的,我们不妨这样想:因为要区别于不同的数
,可以看成序列里的数是一个一个加进去的,每次加入一个数,统计前面序列里第一次出现新加入的这个数的位置,表达的不好,
举个例子:
1 2 3
定义dp(当前元素前面(包括自己)所有包含自己的字序列的和)
定义sum(当前元素前面所有字序列的和,包括此元素)
//输入 1 2 3
//dp 1 5 14
//sum 1 6 20
//a[i] 1 2 3
代码:
用MAP:
C++中map容器的说明和使用技巧
http://www.cnblogs.com/anywei/archive/2011/10/27/2226830.html
map的详细用法
http://blog.csdn.net/sunshinewave/article/details/8067862
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.
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
【题目大意】定义Beauty数是一个序列里所有不相同的数的和,求一个序列所有字序列的Beauty和
1 <= N <= 100000
【解题思路】由于数据比较大,常规方法求字序列和肯定是行不通的,我们不妨这样想:因为要区别于不同的数
,可以看成序列里的数是一个一个加进去的,每次加入一个数,统计前面序列里第一次出现新加入的这个数的位置,表达的不好,
举个例子:
1 2 3
定义dp(当前元素前面(包括自己)所有包含自己的字序列的和)
定义sum(当前元素前面所有字序列的和,包括此元素)
//输入 1 2 3
//dp 1 5 14
//sum 1 6 20
//a[i] 1 2 3
代码:
#include <iostream> #include <cstring> #include<stdio.h> using namespace std; const int MAXN = 1e5 + 10; int main() { int a[MAXN]; int t, n; scanf ("%d", &t); while (t--) { memset (a, 0, sizeof (a)); scanf ("%d", &n); int x; long long dp = 0, sum = 0; for (int i=1; i<=n; ++i) { scanf ("%d", &x); dp = (i - a[x]) * x + dp; sum += dp; a[x] = i; } printf ("%lld\n", sum); } return 0; }
用MAP:
#include<stdio.h> #include<iostream> #include<map> using namespace std; map<int, long long int> mark; int main() { int T, a, n; scanf("%d", &T); while(T--) { scanf("%d", &n); long long int sum = 0, ans = 0; mark.clear(); for(int i = 1; i <= n; i++) { scanf("%d", &a); ans += (i*a); sum += ans; sum -= (mark[a] * a); ans -= (mark[a] * a); mark[a] = i; // cout<<" "<<sum<<" "<<ans<<endl; } // map<int, long long int>::iterator iter; // for(iter = mark.begin(); iter != mark.end(); iter++) // { // cout<<iter->first<<" "<<iter->second<<endl; // } printf("%lld\n", sum); } }知识点:
C++中map容器的说明和使用技巧
http://www.cnblogs.com/anywei/archive/2011/10/27/2226830.html
map的详细用法
http://blog.csdn.net/sunshinewave/article/details/8067862
相关文章推荐
- Okttp3.0简单的二次回调封装
- STL(1) - Lambda表达式
- html5学习系列之cd标签
- web进修之—Hibernate 懒加载(6)
- 汇编学习1
- liunx 用户,群组和权限
- 服务器架构--------iis与Apache共存搭建(都可以使用域名访问,而不带后面的:80,90等)
- from __future__ import absolute_import的作用
- Android中的Semaphore
- hdu1007 Quoit Design【平面最近点对+分治】
- 设计模式 - 动态代理
- RxJava-创建Observable操作符
- UVA5135 Mining Your Own Business ( 无向图双连通分量)
- 在centos7 或 RHEL 7上使用yum测试zabbix 2.4
- HDU 4812 D Tree 点分治
- fzu 2122 又见LKity
- 97 等价交换(贪心-2)
- 计算机网络overview-2
- 最小堆最大堆的详细解读
- 为什么开通博客