51nod1521(set.upper_bound())
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1521
题意:中文题诶~
思路:
我们先看一下set容器的三个库函数:
iterator lower_bound (const value_type& val) const; iterator upper_bound (const value_type& val) const; pair<iterator,iterator> equal_range (const value_type& val) const;
前两者和一般的lower_bound()/upper_bound()函数差不多; equal_range返回两个迭代器,第一个迭代器是set.lower_bound的返回值,第二个迭代器是set.upper_bound的返回值 (注意是使用相同val值调用的情况下。) 对于连续s个可以放置战舰的空格,假设其最多可以放置x艘战舰,那么有:x*a+(x-1)<=s, 解得: x<=(s+1)/(size+1);
我们先声明一个set变量st, 里面存储不能放置战舰的空格.
对于每一次询问的位置x,其都会将一段连续可以放置战舰的空间分成两段;假设我们用gg变量存储当前最多可以放置的战舰的数目,
那么我们询问一次后将gg更新为:gg=gg-被分成两段的空间原本最多可以放的战舰数目+被拆成两段后最多可以放置的战舰数目;
若更新后gg小于战舰数目,那么我们可以确定爱丽丝说谎了; 代码:
#include <bits/stdc++.h>
using namespace std; int main(void){
set<int> st;
int n, k, size, x, gg=, pos=, t;
bool flag=true;
set<int>::iterator it;
scanf("%d%d%d", &n, &k, &size);
gg=(n+)/(size+);
st.insert(n+); //***一开始第n+1个空格坑定不能放战舰啦
cin >> t;
for(int i=; i<=t; i++){
scanf("%d", &x);
if(flag){
it=st.upper_bound(x);
int cnt=*it;
if(it==st.begin()){
gg=gg-cnt/(size+)+x/(size+)+(cnt-x)/(size+);
}else{
it--;
int num=*it;
gg=gg-(cnt-num)/(size+)+(x-num)/(size+)+(cnt-x)/(size+);
}
if(gg<k){
pos=i;
flag=false;
}else{
st.insert(x);
}
}
}
if(flag){
cout << - << endl;
}else{
cout << pos << endl;
}
}
51nod1521(set.upper_bound())的更多相关文章
- STL源码学习----lower_bound和upper_bound算法
转自:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 先贴一下自己的二分代码: #include <cstdio&g ...
- 【刷题记录】 && 【算法杂谈】折半枚举与upper_bound 和 lower_bound
[什么是upper_bound 和 lower_bound] 简单来说lower_bound就是你给他一个非递减数列[first,last)和x,它给你返回非递减序列[first, last)中的第一 ...
- STL_lower_bound&upper_bound用法
ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, la ...
- STL之lower_bound和upper_bound
ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, la ...
- LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- [STL] lower_bound和upper_bound
STL中的每个算法都非常精妙, ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一 ...
- STL lower_bound upper_bound binary-search
STL中的二分查找——lower_bound .upper_bound .binary_search 二分查找很简单,原理就不说了.STL中关于二分查找的函数有三个lower_bound .upper ...
- vector的插入、lower_bound、upper_bound、equal_range实例
对于这几个函数的一些实例以便于理解: #include <cstdlib> #include <cstdio> #include <cstring> #includ ...
- STL中的lower_bound和upper_bound的理解
STL迭代器表述范围的时候,习惯用[a, b),所以lower_bound表示的是第一个不小于给定元素的位置 upper_bound表示的是第一个大于给定元素的位置. 譬如,值val在容器内的时候,从 ...
随机推荐
- linux-常用指令1
掌握下面的命令是最基本的噢!那是我们使用一个系统最基本的操作. 玩过dos么,其实,linux下的文件操作和dos差不多.没什么难的,多练习就记住了.下面如果有条件的话请跟我一样操作吧!百看不如一做. ...
- 用React写一个工大导航
Online: https://brenner8023.github.io/gdut-nav/ GitHub地址: https://github.com/brenner8023/gdut-nav 笔者 ...
- ADO:连接,执行语句与关闭(sql server数据库)
一,身份验证: sql server数据库连接身份验证有两种:windows身份验证和SQL Server身份验证 windows验证:是使用windows的安全子系统对用户连接进行有效性验证.(个人 ...
- codeforces 29D Ant on the Tree (dfs,tree,最近公共祖先)
D. Ant on the Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- redis源码笔记 - redis-cli.c
这份代码是redis的client接口,其和server端的交互使用了deps目录下的hiredis c库,同时,在这部分代码中,应用了linenoise库完成类似history命令查询.自动补全等终 ...
- UDEV管理RAC共享存储
背景:操作系统 centos 6.7 数据库:11.2.0.1 操作流程: 1. 确认在所有RAC节点上已经安装了必要的UDEV包[root@11gnode1 ~]# rpm -qa|grep ude ...
- 【转】Pro Android学习笔记(八):了解Content Provider(下中)
在之前提供了小例子BookProvider,我们回过头看看如何将通过该Content Provider进行数据的读取. (1)增加 private void addBook(String name , ...
- 多校联合训练&hdu5791 Two
hdu5791 dp[i][j]表示的是序列A前i个数字和序列B前j个数字的公共子序列的总个数,那么的dp公式就可以这么表示 理解一下此公式若最尾部的a[i]和b[j]相等的话,那么单独的a[i]和b ...
- 忘记mysql密码,但是可以用navicat修改MySQL密码
1.首先:要知道你的账户 2.打开可以连接的navicat,在查询语句页面,输入以下操作,就可以修改成功密码了
- 如何在niosII中添加i2c外设_winday_新浪博客
如何在niosII中添加i2c外设_winday_新浪博客 如何在niosII中添加i2c外设 winday 摘要:本文说明了如何在niosII添加第三方i2c外设,以供参考. 由于本人使用的Alte ...