题目链接: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. php 微信公众平台开发之微信群发信息

    这篇文章主要为大家详细介绍了php微信公众平台开发之微信群发信息,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 1.目的 完成在微信公众号中群发消息.这里只是完成简单的文字发送.也可以发送语音图片 ...

  2. kaggle 欺诈信用卡预测——不平衡训练样本的处理方法 综合结论就是:随机森林+过采样(直接复制或者smote后,黑白比例1:3 or 1:1)效果比较好!记得在smote前一定要先做标准化!!!其实随机森林对特征是否标准化无感,但是svm和LR就非常非常关键了

    先看数据: 特征如下: Time Number of seconds elapsed between each transaction (over two days) numeric V1 No de ...

  3. GeoServer基础教程(二):GeoServer的Web管理界面快速入门

    转载:http://blog.163.com/daimiao_study/blog/static/248923117201542522742373/ GeoServer的控制和管理是基于网页形式,所有 ...

  4. BEC listen and translation exercise 38

    很高兴看到有这么多人想了解我们的体育设施.It's good to see that there are so many people wanting to find out about our sp ...

  5. leetcode 162 Find Peak Element(二分法)

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  6. SQL Server 2008可以安装在win7 64位的系统上吗?

    可以安装的.SQL 支持32和64位.安装时它自动选择的.下载时注意是完整安装包. SQLFULL_CHS 2008.iso大小:3.28G 已经过百度安全检测,放心下载  

  7. Convolutional Neural Networks for Visual Recognition 5

    Setting up the data and the model 前面我们介绍了一个神经元的模型,通过一个激励函数将高维的输入域权值的点积转化为一个单一的输出,而神经网络就是将神经元排列到每一层,形 ...

  8. 2017.10.5北京清北综合强化班DAY5

    拼不出的数lost.in/.out/.cpp[问题描述]3 个元素的集合{5, 1,2} 的所有子集的和分别是0,1, 2, 3, 5, 6, 7, 8.发现最小的不能由该集合子集拼出的数字是4.现在 ...

  9. Django来敲门~第一部分【4. 创建第一个模块应用】

    成若缺,其用不弊.大盈若冲,其用不穷.大直若屈.大巧若拙.大辩若讷.静胜躁,寒胜热.清静为天下正 ——老子<道德经> 本章内容 创建应用(app) 开发第一个视图(View) URL访问配 ...

  10. Netty,Netty

    Windows防火墙会自动关闭空闲的TCP链接,所以Netty需要心跳,如果发现链接断开需要进行关闭Session: 怎么来理解TCP的流式传输呢? int blocksize = buffer.re ...