BC16&&hdoj5086&&hdoj5087
2016-02-28 19:26
197 查看
Revenge of Segment Tree
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1474 Accepted Submission(s): 526
Problem Description
In computer science, a segment tree is a tree data structure for storing intervals, or segments. It allows querying which of the stored segments contain a given point. It is, in principle, a static structure; that is, its content cannot be modified once the
structure is built. A similar data structure is the interval tree.
A segment tree for a set I of n intervals uses O(n log n) storage and can be built in O(n log n) time. Segment trees support searching for all the intervals that contain a query point in O(log n + k), k being the number of retrieved intervals or segments.
---Wikipedia
Today, Segment Tree takes revenge on you. As Segment Tree can answer the sum query of a interval sequence easily, your task is calculating the sum of the sum of all continuous sub-sequences of a given number sequence.
Input
The first line contains a single integer T, indicating the number of test cases.
Each test case begins with an integer N, indicating the length of the sequence. Then N integer Ai follows, indicating the sequence.
[Technical Specification]
1. 1 <= T <= 10
2. 1 <= N <= 447 000
3. 0 <= Ai <= 1 000 000 000
Output
For each test case, output the answer mod 1 000 000 007.
Sample Input
2
1
2
3
1 2 3
Sample Output
2
20
HintFor the second test case, all continuous sub-sequences are [1], [2], [3], [1, 2], [2, 3] and [1, 2, 3]. So the sum of the sum of the sub-sequences is 1 + 2 + 3 + 3 + 5 + 6 = 20.
Huge input, faster I/O method is recommended. And as N is rather big, too straightforward algorithm (for example, O(N^2)) will lead Time Limit Exceeded.
And one more little helpful hint, be careful about the overflow of int.
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<list> #include<vector> #define MOD 1000000007 using namespace std; const int maxn=10010; int main() { int t; long long i,n; scanf("%d",&t); while(t--){ scanf("%lld",&n); long long sum=0,num; for(i=1;i<=n;++i){ scanf("%lld",&num); sum=(sum+num*(n-i+1)%MOD*i)%MOD; } printf("%lld\n",sum); } return 0; }
Revenge of LIS II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1332 Accepted Submission(s): 450
Problem Description
In computer science, the longest increasing subsequence problem is to find a subsequence of a given sequence in which the subsequence's elements are in sorted order, lowest to highest, and in which the subsequence is as long as possible. This subsequence is
not necessarily contiguous, or unique.
---Wikipedia
Today, LIS takes revenge on you, again. You mission is not calculating the length of longest increasing subsequence, but the length of the second longest increasing subsequence.
Two subsequence is different if and only they have different length, or have at least one different element index in the same place. And second longest increasing subsequence of sequence S indicates the second largest one while sorting all the increasing subsequences
of S by its length.
Input
The first line contains a single integer T, indicating the number of test cases.
Each test case begins with an integer N, indicating the length of the sequence. Then N integer Ai follows, indicating the sequence.
[Technical Specification]
1. 1 <= T <= 100
2. 2 <= N <= 1000
3. 1 <= Ai <= 1 000 000 000
Output
For each test case, output the length of the second longest increasing subsequence.
Sample Input
3
2
1 1
4
1 2 3 4
5
1 1 2 2 2
Sample Output
1
3
2
HintFor the first sequence, there are two increasing subsequence: [1], [1]. So the length of the second longest increasing subsequence is also 1, same with the length of LIS.
Source
BestCoder Round #16
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<list> #include<vector> using namespace std; const int maxn=1010; int num[maxn]; int dp[maxn][2]; void update(int x,int y,int &temp,int &ans){ if(x>=temp){ ans=temp;temp=x; } else if(x>ans)ans=x; if(y>=temp){ ans=temp;temp=y; } else if(y>ans)ans=y; } int main() { int t,i,j,k,n; scanf("%d",&t); while(t--){ scanf("%d",&n); for(i=1;i<=n;++i){ scanf("%d",&num[i]); } int temp=0,ans=0; for(i=1;i<=n;++i){ dp[i][0]=1;dp[i][1]=0; for(j=1;j<i;++j){ if(num[i]>num[j]){ int cnt1=dp[j][0]+1; int cnt2=dp[j][1]+1; if(cnt1>dp[i][0])swap(dp[i][0],cnt1); if(cnt1>dp[i][1])dp[i][1]=cnt1; if(cnt2>dp[i][0])swap(dp[i][0],cnt2); if(cnt2>dp[i][1])dp[i][1]=cnt2; } } update(dp[i][0],dp[i][1],temp,ans); } printf("%d\n",ans); } return 0; }
相关文章推荐
- Linux内核分析——x86汇编基础
- 0-1背包问题
- 【三层学习】——VB.NET实现登录
- 售票问题
- Scrum 之 角色
- java程序实现打印完美对齐的杨辉三角形
- viewport 手机设备
- Java 反射机制浅析
- 图像处理与计算机视觉基础,经典以及最近发展
- UVA 12716(p318)----GCD XOR
- 生成函数
- Matlab melband的计算
- 无限轮播加点
- 角函数
- jQuery--对话框插件--dialog
- vs快捷键汇总
- java-tip-关于StringBuilder的使用
- 【HDOJ】4305 Lightning
- LeetCode OJ 8.String to Integer (atoi)
- 1016. Phone Bills (25)