hdoj5805 NanoApe Loves Sequence && hdoj 5806 NanoApe Loves Sequence Ⅱ
来源:互联网 发布:2016星火英语听力软件 编辑:程序博客网 时间:2024/06/05 04:21
NanoApe Loves Sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 219 Accepted Submission(s): 92
Problem Description
NanoApe, the Retired Dog, has returned back to prepare for the National Higher Education Entrance Examination!
In math class, NanoApe picked up sequences once again. He wrote down a sequence withn numbers on the paper and then randomly deleted a number in the sequence. After that, he calculated the maximum absolute value of the difference of each two adjacent remained numbers, denoted as F .
Now he wants to know the expected value ofF , if he deleted each number with equal probability.
In math class, NanoApe picked up sequences once again. He wrote down a sequence with
Now he wants to know the expected value of
Input
The first line of the input contains an integer T , denoting the number of test cases.
In each test case, the first line of the input contains an integern , denoting the length of the original sequence.
The second line of the input containsn integers A1,A2,...,An , denoting the elements of the sequence.
1≤T≤10, 3≤n≤100000, 1≤Ai≤109
In each test case, the first line of the input contains an integer
The second line of the input contains
Output
For each test case, print a line with one integer, denoting the answer.
In order to prevent using float number, you should print the answer multiplied byn .
In order to prevent using float number, you should print the answer multiplied by
Sample Input
141 2 3 4
Sample Output
6
求出前i个数里相邻差值的最大值fi,i到n里相邻差值的最大值gi,那么ans=∑i=1nmax(∣Ai−1−Ai+1∣,fi−1,gi+1)。
时间复杂度O(n)。
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>using namespace std;const int maxn = 200010;typedef long long ll;int T,n;int A[maxn],pre[maxn],suf[maxn];int main(){ scanf("%d",&T); while(T--){ scanf("%d",&n); for(int i = 1; i <= n; ++i) scanf("%d",&A[i]); memset(pre,0,sizeof(pre)); memset(suf,0,sizeof(suf)); for(int i = 2; i <= n; ++i){ pre[i] = abs(A[i] - A[i - 1]); pre[i] = max(pre[i],pre[i - 1]); } for(int i = n - 1; i >= 1; --i){ suf[i] = abs(A[i] - A[i + 1]); suf[i] = max(suf[i],suf[i + 1]); } ll ans = 0; for(int i = 1; i <= n; ++i){ if(i == 1) ans += suf[2]; else if(i == n) ans += pre[n - 1]; else{ ans += max(pre[i - 1],max(suf[i + 1],abs(A[i + 1] - A[i - 1]))); //printf("%d %d\n",i,max(pre[i - 1],max(suf[i + 1],abs(A[i + 1] - A[i - 1])))); } } printf("%I64d\n",ans); } return 0;}
NanoApe Loves Sequence Ⅱ
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 230 Accepted Submission(s): 112
Problem Description
NanoApe, the Retired Dog, has returned back to prepare for for the National Higher Education Entrance Examination!
In math class, NanoApe picked up sequences once again. He wrote down a sequence withn numbers and a number m on the paper.
Now he wants to know the number of continous subsequences of the sequence in such a manner that thek -th largest number in the subsequence is no less than m .
Note : The length of the subsequence must be no less thank .
In math class, NanoApe picked up sequences once again. He wrote down a sequence with
Now he wants to know the number of continous subsequences of the sequence in such a manner that the
Note : The length of the subsequence must be no less than
Input
The first line of the input contains an integer T , denoting the number of test cases.
In each test case, the first line of the input contains three integersn,m,k .
The second line of the input containsn integers A1,A2,...,An , denoting the elements of the sequence.
1≤T≤10, 2≤n≤200000, 1≤k≤n/2, 1≤m,Ai≤109
In each test case, the first line of the input contains three integers
The second line of the input contains
Output
For each test case, print a line with one integer, denoting the answer.
Sample Input
17 4 24 2 7 7 6 5 1
Sample Output
18
将不小于m的数看作1,剩下的数看作0,那么只要区间内1的个数不小于k则可行,枚举左端点,右端点可以通过two-pointer求出。
时间复杂度O(n)。
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 200005;int T, n, m, k;int a[N];int main() { scanf("%d", &T); while (T--) { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); if (a[i] >= m) a[i] = 1; else a[i] = 0; a[i] += a[i - 1]; } int r = 1; long long ans = 0; for (int l = 1; l <= n; l++) { while (r <= n && a[r] - a[l - 1] < k) r++; ans += n - r + 1; } printf("%lld\n", ans); } return 0;}
0 0
- hdoj5805 NanoApe Loves Sequence && hdoj 5806 NanoApe Loves Sequence Ⅱ
- HDOJ 5806 NanoApe Loves Sequence Ⅱ
- hdoj-5806-NanoApe Loves Sequence Ⅱ
- BC#86.1002(HDOJ5805)NanoApe Loves Sequence
- BC#86.1002(HDOJ5805)NanoApe Loves Sequence
- NanoApe Loves Sequence Ⅱ
- NanoApe Loves Sequence Ⅱ
- hdoj-5805-NanoApe Loves Sequence
- HDU 5806 NanoApe Loves Sequence
- hdu NanoApe Loves Sequence Ⅱ
- HDU5806 NanoApe Loves Sequence Ⅱ
- hdu 5806 NanoApe Loves Sequence Ⅱ
- HDU 5806 NanoApe Loves Sequence Ⅱ
- hdu 5806 NanoApe Loves Sequence Ⅱ
- HDU 5806:NanoApe Loves Sequence Ⅱ
- HDU 5806 NanoApe Loves Sequence Ⅱ
- HDU 5806 NanoApe Loves Sequence Ⅱ
- HDU 5806 - NanoApe Loves Sequence Ⅱ
- recycleview 定义item的 距离的方法。
- 做多选框必备
- c++实现ros by example volume1 例子timed_out_and_back功能
- 一次优化web项目的经历记录(三)
- AngularJS中的定时器,以及定时器的移除
- hdoj5805 NanoApe Loves Sequence && hdoj 5806 NanoApe Loves Sequence Ⅱ
- android studio R.java的path
- 2016.8.5 学习总结
- Linux桌面坏境小结
- 跨域访问
- HDU 3709 数位dp
- SGU101 Domino
- 九度OJ 1101 计算表达式
- Gym 100531 GGrave【水题】