[USACO09DEC]音符Music Notes (二分、STL)
https://www.luogu.org/problem/P2969
题目描述
However, recently the cows have lost interest in the song, as they feel that it is too long and boring. Thus, to make sure his cows are paying attention, he asks them Q (1 <= Q <= 50,000) questions of the form, "In the interval from time T through just before time T+1, which note should you be playing?" The cows need your help to answer these questions which are supplied as Ti (0 <= Ti <= end_of_song).
Consider this song with three notes of durations 2, 1, and 3 beats:
Beat: 0 1 2 3 4 5 6 ...
|----|----|----|----|----|----|--- ...
1111111111 : :
22222: :
333333333333333: Here is a set of five queries along with the resulting answer:
Query Note
2 2
3 3
4 3
0 1
1 1
约翰准备教他的奶牛们弹一首歌.这首歌由N(1<=n<= 50000)个音阶组成,第i个音阶要敲击Bi<=10000次.奶牛从第0时刻开始弹,因此他从0时刻到Bi-1时刻都是敲第1个音阶, 然后他从B1时刻到B1+B2-1时刻敲第2个音阶,从B1+B2到B1+B2+B3-1时刻敲第3个音阶……现在有q(i<q<50000)个问题:在时间段区间t,T+1内,奶牛敲的是哪个音阶?
输入描述:
* Line 1: Two space-separated integers: N and Q
* Lines 2..N+1: Line i+1 contains the single integer: Bi
* Lines N+2..N+Q+1: Line N+i+1 contains a single integer: Ti
输出描述:
* Lines 1..Q: Line i of the output contains the result of query i as a single integer.
示例1
输入
输出
题解:
二分查找
STL的upper_bound函数,找到第一个大于待查元素的值,用法见代码。类似的还有lower_bound,找到的是第一个不小于待查元素的值。
下面介绍一下STL中的lower_bound()和upper_bound():(原文链接:https://blog.csdn.net/qq_40160605/article/details/80150252)
lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的。
在从小到大的排序数组中,
lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
upper_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
在从大到小的排序数组中,重载lower_bound()和upper_bound()
lower_bound( begin,end,num,greater<type>() ):从数组的begin位置到end-1位置二分查找第一个小于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
upper_bound( begin,end,num,greater<type>() ):从数组的begin位置到end-1位置二分查找第一个小于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
STL写法:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <queue>
#include <set>
#include <math.h>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
const double PI=acos(-);
const int maxn=;
using namespace std; int n,q;
int B[maxn];
int ans[maxn]; int main()
{
scanf("%d %d",&n,&q);
for(int i=;i<=n;i++)
{
scanf("%d",&B[i]);
ans[i]=ans[i-]+B[i];
} for(int i=;i<q;i++)
{
int x;
scanf("%d",&x);
printf("%d\n",upper_bound(ans+,ans++n,x)-ans);
}
return ;
}
二分写法:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <math.h>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
const double PI=acos(-);
const int maxn=;
using namespace std;
//ios::sync_with_stdio(false);
// cin.tie(NULL); int n,q;
int l,r,mid;
int B[];
int num[]; int main()
{
scanf("%d %d",&n,&q);
for(int i=;i<=n;i++)
{
scanf("%d",&B[i]);
num[i]=num[i-]+B[i];
}
for(int i=;i<=q;i++)
{
int x;
scanf("%d",&x);
int l,r;
l=;
r=n;
while(l<=r)
{
int mid=(l+r)/;
if(num[mid]<=x)
l=mid+;
else r=mid-;
}
printf("%d\n",l);
}
}
[USACO09DEC]音符Music Notes (二分、STL)的更多相关文章
- 洛谷 P2969 [USACO09DEC]音符Music Notes
P2969 [USACO09DEC]音符Music Notes 题目描述 FJ is going to teach his cows how to play a song. The song cons ...
- Bridging signals(二分 二分+stl dp)
欢迎参加——每周六晚的BestCoder(有米!) Bridging signals Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 6 ...
- Can you find it?(二分 二分+STL set map)
Can you find it? Time Limit : 10000/3000ms (Java/Other) Memory Limit : 32768/10000K (Java/Other) T ...
- P2161 [SHOI2009]会场预约[线段树/树状数组+二分/STL]
题目描述 PP大厦有一间空的礼堂,可以为企业或者单位提供会议场地.这些会议中的大多数都需要连续几天的时间(个别的可能只需要一天),不过场地只有一个,所以不同的会议的时间申请不能够冲突.也就是说,前一个 ...
- CodeForces - 706B 二分stl
#include<iostream> #include<cstdio> #include<cstring> #include<string> #incl ...
- NC24866 [USACO 2009 Dec S]Music Notes
NC24866 [USACO 2009 Dec S]Music Notes 题目 题目描述 FJ is going to teach his cows how to play a song. The ...
- P1540 机器翻译 模拟
题目背景 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 题目描述 这个翻译软件的原理很简单,它只是从头到尾,依次将每个英文单词用对应的中文含义来替换.对于每个英文单词,软件会先 ...
- noip2012开车旅行 题解
题目大意: 给出n个排成一行的城市,每个城市有一个不同的海拔.定义两个城市间的距离等于他们的高度差的绝对值,且绝对值相等的时候海拔低的距离近.有两个人轮流开车,从左往右走.A每次都选最近的,B每次都选 ...
- 电信学院第一届新生程序设计竞赛题解及std
首先非常感谢各位同学的参加,还有出题验题同学的辛勤付出 昨天想偷懒就是不想再把我C++11的style改没了,大家看不懂的可以百度一下哦,懒得再写gcc了,毕竟代码是通的 //代表的是行注释,所以那个 ...
随机推荐
- ArchLinux安装(BIOS)
ArchLinux安装(BIOS) 说在前头:在经历过无数次的失败尝试过后总结出的可用的安装过程(比官方的简单一点) 官方安装指导 一.连接网络 1.连接 # wifi-menu 2.检查是否联通 ( ...
- SQL基础之JDBC、连接池
JDBC JDBC四个核心对象 这几个类都是在java.sql包中 DriverManager(类): 数据库驱动管理类.这个类的作用:1)注册驱动; 2)创建java代码和数据库之间的连接,即获取C ...
- promise核心6 自定义promise
1.定义整体结构(不写实现) 定义一个自己的promise的库 lib(库的简写) 一个js文件.一个js模块(不能用es6 也不能commjs)(用es5模块语法 ) 匿名函数自调用.IIFE ( ...
- MySQL 插入 中文数据乱码解决
问题描述: 1.在命令行中进行插入,没有问题.但是显示存在部分乱码 2.在JDBC中插入成功.中文是直接以“??”形式显示. 通过Navicat客户端查看 与在网页中看到的一一致,说明读取没有问题,问 ...
- Python的 5 种高级用法,效率提升没毛病!
任何编程语言的高级特征通常都是通过大量的使用经验才发现的.比如你在编写一个复杂的项目,并在 stackoverflow 上寻找某个问题的答案.然后你突然发现了一个非常优雅的解决方案,它使用了你从不知道 ...
- rocketmq 使用
rocketmq 基本使用可以看官网和官网给的demo. https://github.com/apache/rocketmq/tree/master/example 这里主要说明几个点:rocke ...
- PAT Advanced 1110 Complete Binary Tree (25) [完全⼆叉树]
题目 Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each ...
- Thread--对象锁猜想
堆内存地址未发生变化: 对象堆内存地址没发生变化的情况下,即值是否与变仍然是同一把锁. 堆内存地址变化: 在线程尝试进入过同步代码时复制当前对象锁副本. 在复制对象锁副本之后改变对象指向不影响对象锁, ...
- 修复grub
进入命令行模式,#chroot /mnt/sysimage :切换根目录#grub2-install /dev/sda :安装grub2到第一硬盘#grub2-mkconfig -o /boot/gr ...
- Vue 集成环信 全局封装环信WebSDK 可直接使用
2019-11-25更新 npm install --save easemob-websdk请直接使用官方安装方式即可.import WebIM from 'easemob-websdk' 以下是最开 ...