“玲珑杯”ACM比赛 Round #19
Posted BobHuang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了“玲珑杯”ACM比赛 Round #19相关的知识,希望对你有一定的参考价值。
A -- A simple math problem
Time Limit:2s Memory Limit:128MByte
Submissions:1599Solved:270
DESCRIPTION
You have a sequence anan, which satisfies:
Now you should find the value of ⌊10an⌋⌊10an⌋.
INPUT
The input includes multiple test cases. The number of test case is less than 1000. Each test case contains only one integer n(1≤n≤109)n(1≤n≤109)。
OUTPUT
For each test case, print a line of one number which means the answer.
SAMPLE INPUT
5
20
1314
SAMPLE OUTPUT
5
21
1317
这个题就是找规律啊,一个很明显的规律是每一项都有一个贡献,但是这个范需要自己找,10的话按照log是要加1结果不需要,入手点就是这里,看看哪些少加了1,每个数量级多一个,这个pow损精度,wa了好几次,主要是n==1时输出值也是1,这个才是我被坑的最严重的
以下是题解
#include <stdio.h> #include <math.h> int pow10(int i){ int ans=1; for(int j=0;j<i;j++) ans*=10; return ans; } int main(){ int n; while(~scanf("%d",&n)){ int ans=n+(int)log10(n*1.0); if(n==10)ans-=1; for(int i=2;i<=9;i++){ int x=pow10(i); if(x+1-i<n&&n<x) ans++; } printf("%d\\n",ans); } return 0;}
B - Buildings
Time Limit:2s Memory Limit:128MByte
Submissions:699Solved:186
DESCRIPTION
There are nn buildings lined up, and the height of the ii-th house is hihi.
An inteval [l,r][l,r](l≤r)(l≤r) is harmonious if and only if max(hl,…,hr)−min(hl,…,hr)≤kmax(hl,…,hr)−min(hl,…,hr)≤k.
Now you need to calculate the number of harmonious intevals.
INPUT
The first line contains two integers n(1≤n≤2×105),k(0≤k≤109)n(1≤n≤2×105),k(0≤k≤109). The second line contains nn integers hi(1≤hi≤109)hi(1≤hi≤109).
OUTPUT
Print a line of one number which means the answer.
SAMPLE INPUT
3 1 1 2 3
SAMPLE OUTPUT
5
HINT
Harmonious intervals are: [1,1],[2,2],[3,3],[1,2],[2,3][1,1],[2,2],[3,3],[1,2],[2,3].
模板题???区间最大值最小值的差小于等于k的值有多少组,单调栈,RMQ都不会被卡的
#include <bits/stdc++.h> #define LL long long using namespace std; const int maxN = 2e5+10; int n, k, a[maxN]; LL ans; void input() { scanf("%d%d", &n, &k); for (int i = 0; i < n; ++i) scanf("%d", &a[i]); } void work() { ans = 0; deque<int> mi, ma; int p = 0; for (int i = 0; i < n; ++i) { while (!(mi.empty() && ma.empty()) && !(abs(a[i]-a[mi.front()]) <= k && abs(a[i]-a[ma.front()]) <= k)) { p++; while (!mi.empty() && mi.front() < p) mi.pop_front(); while (!ma.empty() && ma.front() < p) ma.pop_front(); } ans += i-p+1; while (!mi.empty() && a[mi.back()] > a[i]) mi.pop_back(); mi.push_back(i); while (!ma.empty() && a[ma.back()] < a[i]) ma.pop_back(); ma.push_back(i); } printf("%lld\\n", ans); } int main() { input(); work(); return 0; }
ST表
void rmqinit() { for(int i = 1; i <= n; i++) mi[i][0] = mx[i][0] = w[i]; int m = (int)(log(n * 1.0) / log(2.0)); for(int i = 1; i <= m; i++) for(int j = 1; j <= n; j++) { mx[j][i] = mx[j][i - 1]; if(j + (1 << (i - 1)) <= n) mx[j][i] = max(mx[j][i], mx[j + (1 << (i - 1))][i - 1]); mi[j][i] = mi[j][i - 1]; if(j + (1 << (i - 1)) <= n) mi[j][i] = min(mi[j][i], mi[j + (1 << (i - 1))][i - 1]); } } int rmqmin(int l,int r) { int m = (int)(log((r - l + 1) * 1.0) / log(2.0)); return min(mi[l][m] , mi[r - (1 << m) + 1][m]); } int rmqmax(int l,int r) { int m = (int)(log((r - l + 1) * 1.0) / log(2.0)); return max(mx[l][m] , mx[r - (1 << m) + 1][m]); }
以上是关于“玲珑杯”ACM比赛 Round #19的主要内容,如果未能解决你的问题,请参考以下文章
“玲珑杯”ACM比赛 Round #19 B -- Buildings