ZOJ 3872 浙江2015年省赛试题
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
这个题的意识就是给你n个数,
你求这n个数的子序列中不算重复的数的和,
比如第二个样例他的子序列就是
{2},{2,3},{2,3,3},{3},{3,3},{3};
但每个子序列中重复的元素不被算入,
所以他们的总和就是2+5+5+3+3+3=21;
你求这n个数的子序列中不算重复的数的和,
比如第二个样例他的子序列就是
{2},{2,3},{2,3,3},{3},{3,3},{3};
但每个子序列中重复的元素不被算入,
所以他们的总和就是2+5+5+3+3+3=21;
dp[i]: 以第i个数结尾的所有子序列的和是多少。
那么这样我们就有了一个很简单的转移方法:
dp[i] = dp[i-1] + x[i] * (i - v[num[i]]);
v表示的是num[i]这个数字上一次出现在哪个位置,
这样可以确保不会重复计算
#include <cstdio> #include <cstring> #include <algorithm> #include<math.h> using namespace std; #define max_v 100005 typedef long long LL; LL dp[max_v]; LL sum; LL num[max_v]; LL v[max_v]; void init() { sum=0; memset(dp,0,sizeof(dp)); memset(num,0,sizeof(num)); memset(v,0,sizeof(v)); } int main() { int t; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); init(); for(int i=1;i<=n;i++) { scanf("%lld",&num[i]); } for(int i=1;i<=n;i++) { dp[i]=dp[i-1]+(i-v[num[i]])*num[i]; sum+=dp[i]; v[num[i]]=i; } printf("%lld\n",sum); } return 0; } /* 这个题的意识就是给你n个数, 你求这n个数的子序列中不算重复的数的和, 比如第二个样例他的子序列就是 {2},{2,3},{2,3,3},{3},{3,3},{3}; 但每个子序列中重复的元素不被算入, 所以他们的总和就是2+5+5+3+3+3=21; dp[i]: 以第i个数结尾的所有子序列的和是多少。 那么这样我们就有了一个很简单的转移方法: dp[i] = dp[i-1] + x[i] * (i - v[num[i]]); v表示的是num[i]这个数字上一次出现在哪个位置, 这样可以确保不会重复计算 */
心之所向,素履以往
分类:
ACM
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南