CodeForces - 1491C Pekora and Trampoline(差分+贪心)
题目链接:点击查看
题目大意:给出一个长度为 nnn 的序列 aaa,aia_iai 表示第 iii 个蹦床的强度,当从第 iii 个蹦床起跳的话可以跳到 i+aii+a_ii+ai 的位置,但是相应的第 iii 个蹦床的强度会减少一,更具体的来说,假如在蹦床 iii 起跳后,会有 ai=max(ai−1,1)a_i=max(a_i-1,1)ai=max(ai−1,1)。问最小需要多少次弹跳,才能使得所有的蹦床强度都等于 111
题目分析:口胡了一个 n2n^2n2 的算法,交了一发之后发现会被卡成 n3n^3n3,看了看题解发现大家都是 O(n)O(n)O(n) 写的。。拉低了全网的平均水平我真的很抱歉 orz
因为蹦床只能从前往后蹦,所以考虑从前往后贪心,假设 iii 位置为 aia_iai,为了让 aia_iai 变成 111,肯定是需要直接或间接 ai−1a_i-1ai−1 次以点 iii 为起点开始往后跳的,而从 iii 为起点的话,那么 [i+2,min(n,i+ai)][i+2,min(n,i+a_i)][i+2,min(n,i+ai)] 这一段区间的蹦床会间接成为一次起点
考虑模拟上述过程,因为数据范围允许 O(n2)O(n^2)O(n2),所以可以直接暴力更新所有间接当作起点的位置,当到达某个位置 iii 后,分两种情况讨论一下即可,假设 cnticnt_icnti 为点 iii 间接当做起点的次数
- 点 iii 间接成为起点的次数已经将 aia_iai 变成 111 了,即 deltai>=ai−1delta_i>=a_i-1deltai>=ai−1 此时点 iii 不用当直接起点,而多出来的部分会因为 aia_iai 早就变成了 111 从而继承到了 ai+1a_{i+1}ai+1 的位置去
- 点 iii 还需要作为直接起点才行,更具体的来说,还需要作为 ai−1−deltaia_i-1-delta_iai−1−deltai 次直接起点才行,将这些贡献计入答案即可
上述是 O(n2)O(n^2)O(n2) 的做法,考虑到瓶颈在于区间更新,所以可以用差分代替,直接用前缀和平推过去就好啦,最后时间复杂度优化成了 O(n)O(n)O(n)
代码:
// Problem: C. Pekora and Trampoline
// Contest: Codeforces - Codeforces Global Round 13
// URL: https://codeforces.com/contest/1491/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<list>
#include<unordered_map>
#define lowbit(x) x&-x
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
template<typename T>
inline void read(T &x)
{T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f;
}
template<typename T>
inline void write(T x)
{if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0');
}
const int inf=0x3f3f3f3f;
const int N=1e6+100;
LL a[N],delta[N];
void add(int l,int r,int val) {if(l>r) {return;}delta[l]+=val;delta[r+1]-=val;
}
int main()
{#ifndef ONLINE_JUDGE
// freopen("data.in.txt","r",stdin);
// freopen("data.out.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);int w;cin>>w;while(w--) {int n;read(n);memset(delta,0,sizeof(LL)*(n+5));for(int i=1;i<=n;i++) {read(a[i]);}LL ans=0;for(int i=1;i<=n;i++) {delta[i]+=delta[i-1];if(delta[i]<a[i]-1) {//不够ans+=(a[i]-1)-delta[i];} else {//够了add(i+1,i+1,delta[i]-(a[i]-1));}add(i+2,min(1LL*n,i+a[i]),1);}cout<<ans<<endl;}return 0;
}
CodeForces - 1491C Pekora and Trampoline(差分+贪心)相关推荐
- cf1491C. Pekora and Trampoline
cf1491C. Pekora and Trampoline 题意: 有n个蹦床,每个蹦床有它的弹力值bib_{i}bi,从i蹦床起跳可以落到i+bii+b_{i}i+bi的位置上(前提是这个位置 ...
- Codeforces 437C The Child and Toy(贪心)
题目连接:Codeforces 437C The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...
- codeforces#320(div2) D Or Game 贪心
codeforces#320(div2) D "Or" Game 贪心 D. "Or" Game time limit per test 2 seconds ...
- CodeForces - 1395D - Boboniu Chats with Du 贪心
CodeForces - 1395D - Boboniu Chats with Du 贪心 题意:如果ai>ma_i>mai>m,并且当天可以说话,则接下来ddd天不能说话.其余所 ...
- Codeforces 846 B Math Show DFS + 贪心
题目链接: http://codeforces.com/contest/846/problem/B 题目描述: 有N个节点, 每个节点有相同的K个子节点, 每个子节点有时间花费,完成一个子节点获得1分 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- Educational Codeforces Round 12 C. Simple Strings 贪心
C. Simple Strings 题目连接: http://www.codeforces.com/contest/665/problem/C Description zscoder loves si ...
- C. Little Girl and Maximum Sum【差分 / 贪心】
贪心,思路很简单.肯定是公共次数最多的,放最大的值.以此类推. 我们可以用差分来维护,快速的给区间加1.最后统计每一个点背统计的次数. 然后将数值按照从大到小.给出现次数多的点赋值构造数组.最后前缀和 ...
- CodeForces - 1480D1 Painting the Array I(贪心)
题目链接:点击查看 题目大意:给出一个长度为 nnn 的序列,现在要求拆分成两个子序列,使得两个子序列的贡献之和最 大.对于一个序列的贡献就是,去掉相邻且相同的字母后的长度,即 ∑i=1n[a[i]! ...
最新文章
- appium运行报错java.net.SocketException: socket write error
- visual studio 2019创建项目报错:对com组件的调用返回了错误hresult_fail(未解决)
- UITableView cell自定义视图中插入Table实现复杂界面
- 【VS开发】CTimeSpan类
- session的removeattribute移除一个不存在的属性会怎么用_公认峡谷机制最完美,对线几乎无解,夏侯惇高端局为何火不起来?...
- jdk监控与故障处理工具
- 基于AForge.Net框架的扑克牌识别(Nazmi Altun著,野比 译)
- 【es】FATAL [circuit_breaking_exception] [parent] Data too large, data for [<http_request>] would be
- 阿里云云计算 17 块存储的分类
- uniapp如何分享文件到微信 文件分享
- 静态路由和默认路由的原理与基础配置命令
- 【牛客网】最小公倍数
- android 進度條_Android ProgressBar 反向進度條/進度條從右到左走
- 科士达STATIONAIR系列精密空调远程监控解决方案
- IPTV机顶盒使用MediaPlayer 播放rtsp协议的视频
- 应用SVM预测澳大利亚降雨(含数据预处理与调参)
- 【合泰HT32F52352GPTM多路PWM控制】
- cv_bridge用于ROS图像和OpenCV图像的转换
- MUI的。。。三个意思
- Jenkins上配置Robot Framework自动化测试脚本工程