hdu4970 Killing Monsters (差分数列)
2014多校9 1011
http://acm.hdu.edu.cn/showproblem.php?pid=4970
Killing MonstersTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem Description
Kingdom Rush is a popular TD game, in which you should build some
towers to protect your kingdom from monsters. And now another wave of monsters is coming and you need again to know whether you can get through it. The path of monsters is a straight line, and there A witch helps your enemies and makes Now that Input
The input contains multiple test cases.
The first line of each case is an integer N (0 < N <= 100000), The input is terminated by N = 0. Output
Output one line containing the number of surviving monsters.
Sample Input
5
2 1 3 1 5 5 2 5 1 3 3 1 5 2 7 3 9 1 0 Sample Output
3
Hint
In the sample, three monsters with origin HP 5, 7 and 9 will survive. Source
Recommend
|
题意:塔防,怪走一条直线,可以分成1~n共n格。给出m个塔的攻击范围(Li~Ri),攻击力Di,怪物走过这格会减少Di血量。给出k个怪物的血量、出生格,求有多少个怪物可以走到终点。
题解:差分数列搞。
粗略一看,是区间加减、区间求和,线段树!会超时,怕了。
再一看,是区间加减完再区间求和,而且求和还是有限制的,就求i~n的和。
用差分数列可以轻松区间加减,差分数列就是b[i]=a[i]-a[i-1],区间[i,j]加D就是b[i]=b[i]+d , b[j+1]=b[j+1]-d。
但是怎么求和呢?我们把和写出来观察一下:
an = bn
an-1 + an =2an - bn
an-2 + an-1 + an = 3an - 2bn - bn-1
看起来很好算的样子!
于是这样就能算(其中an就是an ,其中c[i]就是ai加到an的和):
ll one=an,many=an;
for(i=n;i>;i--){
c[i]=many;
one-=b[i];
many+=one;
}
这样就轻松算啦。
我一开始想到差分数列,不过没仔细想怎么求和,然后就去线段树了,逗乐。后来才发现居然这么好求。
全代码:
//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll long long
#define usll unsigned ll
#define mz(array) memset(array, 0, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) prllf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("1biao.out","w",stdout)
#define mp make_pair
#define pb push_back const int maxn=; int n,m,k;
int l[maxn],r[maxn],d[maxn];
int x[maxn];
ll h[maxn];
ll b[maxn];
ll c[maxn];
ll an=; void Update(int L, int R, int x){
b[L]+=x;
b[R+]-=x;
if(R==n)an+=x;
} int main(){
int i;
while(scanf("%d",&n)!=EOF){
if(n==)break;
scanf("%d",&m);
mz(b);mz(c);an=;
REP(i,m) {
scanf("%d%d%d",&l[i],&r[i],&d[i]);
Update(l[i],r[i],d[i]);
}
ll one=an,many=an;
for(i=n;i>;i--){
c[i]=many;
one-=b[i];
many+=one;
}
//for(i=1;i<=n;i++)printf("%I64d\n",c[i]);
int ans=;
//for(i=1;i<=n;i++)printf("(%d,%d),%d\n",i,n,Query(i,n,1,n,1));
scanf("%d",&k);
REP(i,k) {
scanf("%I64d%d",&h[i],&x[i]);
if(c[x[i]]< h[i])ans++;
}
printf("%d\n",ans);
}
return ;
}
hdu4970 Killing Monsters (差分数列)的更多相关文章
- Killing Monsters(hdu4970)
Killing Monsters Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- CF460C Present (二分 + 差分数列)
Codeforces Round #262 (Div. 2) C C - Present C. Present time limit per test 2 seconds memory limit p ...
- 周赛-Killing Monsters 分类: 比赛 2015-08-02 09:45 3人阅读 评论(0) 收藏
Killing Monsters Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- UVALive 4119 Always an integer (差分数列,模拟)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Always an integer Time Limit:3000MS M ...
- [CF 295A]Grag and Array[差分数列]
题意: 有数列a[ ]; 操作op[ ] = { l, r, d }; 询问q[ ] = { x, y }; 操作表示对a的[ l, r ] 区间上每个数增加d; 询问表示执行[ x, y ]之间的o ...
- HDU 4970 Killing Monsters(树状数组)
Killing Monsters Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- [CF 276C]Little Girl and Maximum Sum[差分数列]
题意: 给出n项的数列A[ ], q个询问, 询问 [ l, r ] 之间项的和. 求A的全排列中该和的最大值. 思路: 记录所有询问, 利用差分数列qd[ ], 标记第 i 项被询问的次数( 每次区 ...
- hdu 4970 Killing Monsters(数组的巧妙运用) 2014多校训练第9场
pid=4970">Killing Monsters ...
- 【HDU4970】Killing Monsters
题意 数轴上有n个点,有m座炮塔,每个炮塔有一个攻击范围和伤害,有k个怪物,给出他们的初始位置和血量,问最后有多少怪物能活着到达n点.n<=100000 分析 对于某个怪物,什么情况下它可以活着 ...
随机推荐
- vs2010 mvc3安装时报错
今天在研究以往的商城项目时,由于前台使用的是MVC3,在没有安装MVC3的插件时,提示未能加载项目,但是在安装过程中,又提示安装失败: 决定折腾一下->居然找到一篇以前别人写的神作,特此记录一下 ...
- AspectJ获取方法注解的信息
在使用Aspectj获取方法注解信息的时候,可以使用下面的代码片段: /** * Get value of annotated method parameter */ private <T ex ...
- 使用Eval()绑定数据时使用三元运算符
ASP.NET邦定数据“<%#Eval("Sex")%>”运用三元运算符: <%#(Eval("Sex", "{0}") ...
- POJ 3617 Best Cow Line (贪心)
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16104 Accepted: 4 ...
- Some Simple Models of Neurons
Linear neuron: \[y=b+\sum\limits_i{x_i w_i}\] Binary threshold neuron: \[z = \sum\limits_i{x_i w_i}\ ...
- Negative log-likelihood function
Softmax function Softmax 函数 \(y=[y_1,\cdots,y_m]\) 定义如下: \[y_i=\frac{exp(z_i)}{\sum\limits_{j=1}^m{e ...
- Install latest R for ubuntu
### delete old version rm -rf /usr/local/lib/R /usr/lib/R ~/**/R sudo apt-get autoremove rstudio sud ...
- mysql数据库添加索引优化查询效率
项目中如果表中的数据过多的话,会影响查询的效率,那么我们需要想办法优化查询,通常添加索引就是我们的选择之一: 1.添加PRIMARY KEY(主键索引) mysql>ALTER TABLE `t ...
- Matlab小技巧
记录一些用Matlab的技巧. //imshow全屏 subplot(1,3,3); imshow(topSketMat); hold on; set(gcf, 'units', 'normalize ...
- WinForm------TreeList修改节点图标和按钮样式
转载: https://documentation.devexpress.com/#WindowsForms/DevExpressXtraTreeListTreeList_CustomDrawNode ...