【51Nod - 1279】 扔盘子(思维)(on-p会超时)

题干:

有一口井,井的高度为N,每隔1个单位它的宽度有变化。现在从井口往下面扔圆盘,如果圆盘的宽度大于井在某个高度的宽度,则圆盘被卡住(恰好等于的话会下去)。

盘子有几种命运:1、掉到井底。2、被卡住。3、落到别的盘子上方。

盘子的高度也是单位高度。给定井的宽度和每个盘子的宽度,求最终落到井内的盘子数量。

 

 

如图井和盘子信息如下:

井:5 6 4 3 6 2 3

盘子:2 3 5 2 4

 

最终有4个盘子落在井内。

本题由   @javaman  翻译。

Input

第1行:2个数N, M中间用空格分隔,N为井的深度,M为盘子的数量(1 <= N, M <= 50000)。 
第2 - N + 1行,每行1个数,对应井的宽度Wi(1 <= Wi <= 10^9)。 
第N + 2 - N + M + 1行,每行1个数,对应盘子的宽度Di(1 <= Di <= 10^9)

Output

输出最终落到井内的盘子数量。

Sample Input

7 5
5
6
4
3
6
2
3
2
3
5
2
4

Sample Output

4

解题报告:

     预处理一下最小值,o(n)扫一遍。

    法二  这题还可以线段树做、维护一个区间最小值

超时代码:

#include<bits/stdc++.h>
//on-p处理 
using namespace std;
int a[50000 + 5];
int main()
{int n,m,cur,ans = 0;int tmp;cin>>n>>m;for(int i = 1; i<=n; i++) {cin>>a[i];}cur = n + 1;while(m--) {scanf("%d",&tmp);int flag = 1;if(cur<=1) break;if(tmp > a[1]) break;for(int i = 1; i<cur; i++) {if(tmp <= a[i] && tmp > a[i+1]) {	ans++;cur = i;flag = 0;break;}}if(flag == 1) {ans++;cur--;}}printf("%d\n",ans);return 0 ;
}

AC代码:(218ms)

#include<bits/stdc++.h>using namespace std;int a[50000 + 5];
int  main()
{int n,m,ans=0;int tmp;cin>>n>>m;a[0]=INT_MAX;for(int i = 1; i<=n; i++) {scanf("%d",&a[i]);a[i]=min(a[i],a[i-1]);}int cur = n;while(m--) {scanf("%d",&tmp);while(a[cur]<tmp) cur--;if(cur > 0)ans++,cur--;}printf("%d\n",ans);}

AC代码:(线段树)(140ms)

#include<bits/stdc++.h>using namespace std;
const int MAX = 50000 + 5;
int a[MAX],b[MAX];
int n,m;
struct TREE {int l,r;int val;
} tree[MAX * 4];void pushup(int cur ) {tree[cur].val = min( tree[cur*2].val , tree[cur*2+1].val ) ;
}void build(int l,int r,int cur) {tree[cur].l = l;tree[cur].r = r;if(l == r) {tree[cur].val = a[l];return ;}int m = (l+r)/2;build(l,m,2*cur);build(m+1,r,cur*2+1);pushup(cur);
}
void update(int tar,int val,int l,int r,int cur) {if(l == r) {tree[cur].val = val;return ;}int m = (l+r)/2;if(tar <= m) update(tar,val,l,m,cur*2);else update(tar,val,m+1,r,cur*2+1);pushup(cur);
}
int query(int val,int l,int r,int cur) {if(l == r) return r;int m = (l+r)/2;if(val <= tree[cur*2].val) return query(val,m+1,r,cur*2+1);else return query(val,l,m,cur*2);
}int main()
{cin>>n>>m;for(int i = 1; i<=n; i++) {scanf("%d",&a[i]);}for(int j = 1; j<=m; j++ ) {scanf("%d",&b[j]);}int tmp,ans = 0 ;build(1,n,1);for(int i = 1; i<=m; i++) {tmp = query(b[i],1,n,1);
//        printf("tmp = %d\n",tmp);if(tmp != 1) ans++;else {break;}update(tmp-1,0,1,n,1);}cout << ans << endl;return 0 ;
}

 

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441915.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java 内部类私有成员 能访问,为什么外部Java类可以访问内部类私有成员?

HUX布斯如果您想隐藏内部类的私有成员&#xff0c;您可以与公共成员定义一个接口&#xff0c;并创建一个实现此接口的匿名内部类。下面的例子&#xff1a;class ABC{private interface MyInterface{void printInt();}private static MyInterface mMember new MyInterface(){pr…

【HDU - 6290】 奢侈的旅行 (对题目预处理 + DIjkstra最短路)

题干&#xff1a; 高玩小Q不仅喜欢玩寻宝游戏&#xff0c;还喜欢一款升级养成类游戏。在这个游戏的世界地图中一共有nn个城镇&#xff0c;编号依次为11到nn。 这些城镇之间有mm条单向道路&#xff0c;第ii 条单项道路包含四个参数ui,vi,ai,biui,vi,ai,bi&#xff0c;表示一条…

数学建模matlab推荐,推荐数学建模matlab方法整理 - 图文

disp()函数的常见用法1、显示字符串 >> disp(sqrt(2)) sqrt(2)将要显示的字符串必须放在单引号里面&#xff01;&#xff01;&#xff01; 2、显示结果 >> disp(sqrt(2)) 1.41423、显示多个字符>> disp([sqrt(2),num2str(sqrt(2))]) sqrt(2)1.4142格式必须如…

【POJ - 3321】 Apple Tree(dfs序 + 线段树维护 或 dfs序 + 树状数组维护)

题干&#xff1a; There is an apple tree outside of kakas house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree. The tree has N forks which are connected by branches. …

gjr garch Matlab,基于Copula-ARIMA-GJR-GARCH模型的股票指数相关性分析

摘要&#xff1a;当资产收益率分布较为复杂时,研究它们之间的相关关系就变得较为困难。特别是股票市场中资产的收益率分布非正态时,我们几乎不可能去精确模拟几种资产收益率之间的联合分布函数。然而Copula函数恰恰可以解决这类问题。Copula函数是用来描述随机变量间相依结构的…

【HDU - 3974】 Assign the task (dfs序 + 线段树维护 区间更新+ 单点查询)

题干&#xff1a; There is a company that has N employees(numbered from 1 to N),every employee in the company has a immediate boss (except for the leader of whole company).If you are the immediate boss of someone,that person is your subordinate, and all hi…

php 插件 代码架构,php反射机制详以及插件架构实例详解

1。用途&#xff1a;该扩展分析php程序&#xff0c;导出或提取出关于类、方法、属性、参数等的详细信息&#xff0c;包括注释。Reflection可以说是对php库函数&#xff1a;“Classes/Objects 类&#xff0f;对象函数”的一个扩展。主要用在通过程序检测现有php程序内部关于类、…

【HDU - 1540】 Tunnel Warfare (线段树进阶操作 区间合并+ 单点更新+ 最长覆盖区间查询 )

题干&#xff1a; During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every village was …

php扩展包安装了为啥没加载,已安装PHP扩展但未加载

我正在尝试安装php的ssh2扩展,并且有一点点困难.文件在那里,它只是没有加载到PHP.首先,我安装了ssh2&#xff1a;aptitude install libssh2-1-dev libssh2-php(对于它的价值,我在Nginx上运行Ubuntu 12.04.)我可以看到使用modules命令加载ssh2&#xff1a;php -m |grep ssh2ssh2…

【HDU - 1166】敌兵布阵 (线段树模板 单点更新+ 区间查询)

题干&#xff1a; C国的死对头A国这段时间正在进行军事演习&#xff0c;所以C国间谍头子Derek和他手下Tidy又开始忙乎了。A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况。由于采取了某种先进的监测手段&#xff0c;所以每个工兵营地…

php中获取本月第二天,php第二天

//heredoc方式,可以保存长文本 <<<$str <<长文本EOTS;header("Content-Type:text/html;charsetutf-8"); //防止乱码0 1 2 3 4 5 十进制0 1 10 11 100 101 二进制十进制专二进制除二取余二进制专十进制1011*2*20*2*1(1*2/2)5;120 0 1 0 16 3 211111$a…

【HDU - 1754】I Hate It (线段树模板 单点覆盖更新+区间最大值查询)

题干&#xff1a; 很多学校流行一种比较的习惯。老师们很喜欢询问&#xff0c;从某某到某某当中&#xff0c;分数最高的是多少。 这让很多学生很反感。 不管你喜不喜欢&#xff0c;现在需要你做的是&#xff0c;就是按照老师的要求&#xff0c;写一个程序&#xff0c;模拟老…

php微信上传头像,微信小程序怎么上传头像

这次给大家带来的是微信小程序 上传头像的实例详解&#xff0c;最近在做微信小程序上传头像和上传照片功能就随手写一下代码&#xff0c;这篇文章就给大家好好分析一下。上传头像html&#xff1a;头像JS代码// 切换头像changeAvatar: function () {var that this;// var child…

【HDU - 1698】 Just a Hook(线段树模板 区间覆盖更新(laz标记) + 区间和查询 )

题干&#xff1a; In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length. Now Pudge wants to do some operations on the hoo…

反序列化 php R类型,pikachu-PHP反序列化、XXE、SSFR

一、PHP反序列化1.1概述在理解这个漏洞前,你需要先搞清楚php中serialize()&#xff0c;unserialize()这两个函数。序列化serialize()序列化说通俗点就是把一个对象变成可以传输的字符串,比如下面是一个对象:class S{public $test"pikachu";}$snew S(); //创建一个对象…

【POJ - 3468 】 A Simple Problem with Integers (线段树模板 区间更新 + 区间和查询)(不能树状数组或差分数组)

题干&#xff1a; You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval. I…

php向下滑动,js如何判断鼠标滚轮是向下还是向上滚动

判断鼠标滚轮是向上或向下滚动&#xff0c;不同的浏览器的判别方式是不一样的&#xff0c;当前比较流行的浏览器有 IE&#xff0c;Opera&#xff0c;Safari&#xff0c;Firefox&#xff0c;Chrome&#xff0c;在这个问题上Firefox和其他浏览器的实现方式是不一样的。现在通过一…

学习php技巧,对初学者非常有用的PHP技巧

对初学者非常有用的PHP技巧131415161718function add_to_cart($item_id , $qty){if(!is_array($item_id)){$_SESSION[cart][$item_id] $qty;}else{foreach($item_id as $i_id > $qty){$_SESSION[cart][$i_id] $qty;}}}add_to_cart( IPHONE3 , 2 );add_to_cart( array(IPHO…

【51nod - 1065】 最小正子段和( 前缀和排序 )

题干&#xff1a; N个整数组成的序列a11,a22,a33,…,ann&#xff0c;从中选出一个子序列&#xff08;aii,ai1i1,…ajj&#xff09;&#xff0c;使这个子序列的和>0&#xff0c;并且这个和是所有和>0的子序列中最小的。 例如&#xff1a;4&#xff0c;-1&#xff0c;5&a…

oracle trace 文件名,限制oracle trace 文件大小

限制oracle trace 文件大小2007-10-10 17:34:55| 分类&#xff1a; oracle | 标签&#xff1a;|字号max_dump_file_size参数在oracle数据库中用来限制 oracle trace 文件大小max_dump_file_size 的单位是 操作系统块 默认值为 UNLIMITED在有些时候&#xff0c;oracle会产生…