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在容器内的时候,从 ...
随机推荐
- artDialog 简单使用!
简介 artDialog是一个轻巧且高度兼容的javascript对话框组件,可让你的网页交互拥有桌面软件般的用户体验. 功能: 支持锁定屏幕(遮罩).模拟alert和confirm.多窗口弹出.静止 ...
- ASP.WEB Form 几点知识
1.GridView 行的多选 <asp:TemplateField ControlStyle-Width="30" HeaderText="选择" &g ...
- 图像处理检测方法 — ORB(Oriented FAST and Rotated BRIEF)
1.FAST FAST算子的基本原理是:若某像素点与其周围领域内足够多的连续的像素点存在某一属性差异,并且该差异大于指定阈值,则可以断定该像素点与其邻域像素有可被识别的不同之处,可以作为一个特征点( ...
- codeforces 655B B. Mischievous Mess Makers(贪心)
题目链接: B. Mischievous Mess Makers time limit per test 1 second memory limit per test 256 megabytes in ...
- Struts2 - 通过 ActionContext 访问 Web 资源
public String execute(){ //0. 获取 ActionContext 对象 //ActionContext 是 Action 的上下文对象. 可以从中获取到当往 Action ...
- tensorflow中张量(tensor)的属性——维数(阶)、形状和数据类型
tensorflow的命名来源于本身的运行原理,tensor(张量)意味着N维数组,flow(流)意味着基于数据流图的计算,所以tensorflow字面理解为张量从流图的一端流动到另一端的计算过程. ...
- C语言逗号运算符和逗号表达式
在C语言中逗号","也是一种运算符,称为逗号运算符. 其功能是把两个表达式连接起来组成一个表达式, 称为逗号表达式.其一般形式为:表达式1,表达式2 其求值过程是分别求两个表达式的 ...
- 【VisualStudio】软件安装中出现的问题
针对2017版本安装 1. 安装windows通用平台工具出错 报错信息:15605 FQ安装. 2. LINK : fatal error LNK1104: 无法打开文件“gdi32.lib” 在 ...
- easy_install下载地址及安装
下载地址 https://pypi.python.org/pypi/setuptools 解压 tar -xzvf xx.tar.gz 安装 cd 解压目录 sudo python setup.py ...
- MD5 校验文件
https://blog.csdn.net/wudishine/article/details/42466831 MD5.h #ifndef MD5_H #define MD5_H #include ...