题目链接: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())的更多相关文章

  1. STL源码学习----lower_bound和upper_bound算法

    转自:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 先贴一下自己的二分代码: #include <cstdio&g ...

  2. 【刷题记录】 && 【算法杂谈】折半枚举与upper_bound 和 lower_bound

    [什么是upper_bound 和 lower_bound] 简单来说lower_bound就是你给他一个非递减数列[first,last)和x,它给你返回非递减序列[first, last)中的第一 ...

  3. STL_lower_bound&upper_bound用法

    ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, la ...

  4. STL之lower_bound和upper_bound

    ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, la ...

  5. 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 ...

  6. [STL] lower_bound和upper_bound

    STL中的每个算法都非常精妙, ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一 ...

  7. STL lower_bound upper_bound binary-search

    STL中的二分查找——lower_bound .upper_bound .binary_search 二分查找很简单,原理就不说了.STL中关于二分查找的函数有三个lower_bound .upper ...

  8. vector的插入、lower_bound、upper_bound、equal_range实例

    对于这几个函数的一些实例以便于理解: #include <cstdlib> #include <cstdio> #include <cstring> #includ ...

  9. STL中的lower_bound和upper_bound的理解

    STL迭代器表述范围的时候,习惯用[a, b),所以lower_bound表示的是第一个不小于给定元素的位置 upper_bound表示的是第一个大于给定元素的位置. 譬如,值val在容器内的时候,从 ...

随机推荐

  1. python-多线程趣味(锁)

    接上一篇,程序员在敲代码的时候觉得无聊,无聊的时候,会想到去吃零食,那么假如一个函数: #! /usr/bin/env python #coding=utf-8 ''' ''' import time ...

  2. conda 里的 jupyter

    1. 安装conda https://mirrors.tuna.tsinghua.edu.cn/anaconda/ 下载并安装. 2. 安装jupyter (1)在ananconda主环境安装:pip ...

  3. loj515贪心只能过样例

    bitset练习题... 位运算真的是玄学... 一开始真的“只能过样例” 后来发现把左移写成了小于号 鬼知道我在想什么/手动微笑 loj第一题 #include<iostream> #i ...

  4. BZOJ3700: 发展城市

    BZOJ3700: 发展城市 https://lydsy.com/JudgeOnline/problem.php?id=3700 分析: 枚举两个人,先求链交,求到两个端点的时间. 链交求法:求两两\ ...

  5. Tensorflow Summary用法

    本文转载自:https://www.cnblogs.com/lyc-seu/p/8647792.html Tensorflow Summary用法 tensorboard 作为一款可视化神器,是学习t ...

  6. 2.2synchronized同步语句块

    使用synchronized虽然能够避免不同步的现象出现,但是也会出现弊端,比如代码执行时间过长,那么其他线程就必须等待该线程执行完毕释放锁之后才能拿到锁. 面对这种问题可以使用同步代码块来解决. 2 ...

  7. centos7添加环境变量

    # vim /etc/profile在最后,添加:export PATH="/usr/local/webserver/mysql/bin:$PATH" #添加的路径保存,退出,然后 ...

  8. mariadb复制

    实验环境:CentOS7 #安装mariadb-server #主数据库: [root@~ localhost]#vim /etc/my.cnf.d/server.cnf [mysqld] skip_ ...

  9. linux命令-zip压缩unzip解压缩

    和windows的zip的压缩文件是通用的  可以解压缩 压缩文件 /////////////////////////////////////////////////////////// [root@ ...

  10. maven spring3.2.5

    出现的情形: 开发环境: spring3.2.5 + springmvc +spirngDATA +maven 一. 偶然的spring Junit4测试 加载applicationContext.x ...