「日常训练」Single-use Stones (CFR476D2D)
题意(Codeforces 965D)
$w$表示河的宽度,$l$表示青蛙所能跳的最远的距离,第二行的$w-1$个元素表示离河岸为$i$的地方有$a[i]$个石头,一个石头被踩两次,问最多有多少只青蛙可以跳到河对岸。
分析
我是激情看题解的,很惭愧。因为自己的算法喜提TLE了w
我参考的题解在这儿。官方题解用了二分的思想。
这个算法是怎么贪心的呢?就在 while(p<i && frog[p]+frog[i]<=a[i]) 这一句。当第$i$处已经满了的时候,我们就不考虑$[p+1,i-1]$到$i$的跳跃了。后面那句if是减去多余的。其他地方结合注释和原题解的讲解来看。注意维护$p$指针始终和$i$的距离$<l$。
代码
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define PB push_back
#define MP make_pair
#define fi first
#define se second
#define lowbit(x) (x&(-x))
#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define per(i, a, b) for(int i = (a); i >= (b); i--)
#define pr(x) cout << #x << " = " << x << " ";
#define prl(x) cout << #x << " = " << x << endl;
#define ZERO(X) memset((X),0,sizeof(X))
#define ALL(X) (X).begin(),(X).end()
#define SZ(x) (int)x.size()
using namespace std;
typedef pair<int, int> PI;
typedef pair<pair<int, int>, int> PII;
typedef pair<pair<pair<int, int>, int>, int> PIII;
using ull= unsigned long long;
using ll = long long;
using ld = long double;
#define quickio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
/* debug("Precalc: %.3f\n", (double)(clock()) / CLOCKS_PER_SEC);
clock_t z = clock();
solve();
//debug("Test: %.3f\n", (double)(clock() - z) / CLOCKS_PER_SEC);
*/
template<typename T = int>
inline T read() {
T val=0, sign=1;
char ch;
for (ch=getchar();ch<'0'||ch>'9';ch=getchar())
if (ch=='-') sign=-1;
for (;ch>='0'&&ch<='9';ch=getchar())
val=val*10+ch-'0';
return sign*val;
}
int main()
{
int w,l; cin>>w>>l;
int a[100005]; ZERO(a);
int frog[100005]; ZERO(frog);
a[w]=0x3f3f3f3f; // INF
rep(i,1,w-1)
{
cin>>a[i];
if(i<=l) frog[i]=a[i];
}
int p=1;
rep(i,l+1,w)
{
while(i-p>l) ++p;
while(p<i && frog[p]+frog[i]<=a[i])
frog[i]+=frog[p++];
if(p<i&&frog[i]!=a[i]) // greedy here. It's impossible for p+1~i-1 to jmp to a[i]. here, imply that frog[p]+frog[i]>a[i]
{
frog[p]-=(a[i]-frog[i]); // the a[i]-frog[i] frogs cannot jmp from p, because it's full already.
frog[i]=a[i];
}
}
cout<<frog[w]<<endl;
return 0;
}
「日常训练」Single-use Stones (CFR476D2D)的更多相关文章
- 「日常训练」ZgukistringZ(Codeforces Round #307 Div. 2 B)
题意与分析(CodeForces 551B) 这他妈哪里是日常训练,这是日常弟中弟. 题意是这样的,给出一个字符串A,再给出两个字符串B,C,求A中任意量字符交换后(不限制次数)能够得到的使B,C作为 ...
- 「日常训练」Magic Stones(CodeForces-1110E)
题意 给定两个数组c和t,可以对c数组中的任何元素变换\(c_i\)成\(c_{i+1}+c_{i-1}-c_i\),问c数组在若干次变换后能否变换成t数组. 分析 这种魔法题目我是同样的没做过. ...
- 「日常训练」 Fire!(UVA-11624)
与其说是训练不如说是重温.重新写了Java版本的代码. import java.util.*; import java.math.*; import java.io.BufferedInputStre ...
- 「日常训练」COMMON 约数研究(HYSBZ-1968)
题意与分析 感谢https://www.cnblogs.com/Leohh/p/7512960.html的题解.这题话说原来不在我的训练范围,正好有个同学问我,我就拿来做做.数学果然不是我擅长的啊,这 ...
- 「日常训练」 Mike and Fun (CFR305D2B)
题意(CodeForces 548B) 每次对01矩阵中的一位取反,问每次操作后,单列中最长连续1的长度. 分析 非常非常简单,但是我当时训练的时候WA了四次...无力吐槽了,人间 不值得.jpg 代 ...
- 「日常训练」Common Subexpression Elimination(UVa-12219)
今天做的题目就是抱佛脚2333 懂的都懂. 这条题目干了好几天,最后还是参考别人的代码敲出来了,但是自己独立思考了两天多,还是有收获的. 思路分析 做这条题我是先按照之前的那条题目(The SetSt ...
- 「日常训练」Jongmah(Codeforces-1110D)
题意 你有n个数字,范围[1, m],你可以选择其中的三个数字构成一个三元组,但是这三个数字必须是连续的或者相同的,每个数字只能用一次,问这n个数字最多构成多少个三元组? 分析 根据官方Editori ...
- 「日常训练」The Necklace(UVA-10054)
代码 for(int i=0; i!=n; ++i) { int u = cin.nextInt(); int v = cin.nextInt(); edges.add(new Edge(u,v)); ...
- 「日常训练」Known Notation(ZOJ-3829)
题意与分析 题意是这样的:给一个字符串,字符串中只包含数字和运算符'*'.现在问字符串是不是一个合法的逆波兰式(后缀表达式).已知逆波兰式的空格消除,也就是说123可以看成123也可以看成1和23.如 ...
随机推荐
- C# Response 下载
//TransmitFile实现下载 protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一个新 ...
- 【luogu P2590 [ZJOI2008]树的统计】 题解
题目链接:https://www.luogu.org/problemnew/show/P2590 我想学树剖QAQ #include <cstdio> #include <cstri ...
- 123. Best Time to Buy and Sell Stock III ——LeetCode
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Angularjs 数据过滤
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- 《深入理解Spring Cloud与微服务构建》书籍目录
转载请标明出处: https://blog.csdn.net/forezp/article/details/79735542 本文出自方志朋的博客 作者简介 方志朋,毕业于武汉理工大学,CSDN博客专 ...
- Openresty最佳案例 | 第7篇: 模块开发、OpenResty连接Redis
转载请标明出处: http://blog.csdn.net/forezp/article/details/78616714 本文出自方志朋的博客 Lua模块开发 在实际的开发过程中,不可能把所有的lu ...
- xcode 快捷键大全、XCode常用快捷键图文介绍
其实就是设置里面的快捷键变成了文字版,刚开始用Xcode是不是发现以前熟悉的开发环境的快捷键都不能用了?怎么快捷运行,停止,编辑等等.都不一样了.快速的掌握这些快捷键,能提供开发的效率. 其实快捷键在 ...
- iOS Alamofire插件使用方法
let parameters = [ " ] Alamofire.request("http://110.185.104.100:8888/skproject/HvLogistic ...
- 判断js中数据类型 的最短代码
判断字符串类型的: function isString(obj) { return obj+"" === obj; } 判断bool类型的: function isBool(obj ...
- chrome debugger 调试
debugger 使用chrome调试时,html页面的js代码中可能不好打断点(因为在jvm中才会有代码) 我一开始是故意在需要断点的后面或前面写个错的alert,通过jvm找到此处,然后在需要的地 ...