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在容器内的时候,从 ...
随机推荐
- 分享知识-快乐自己:SSH 整合 Demo
楼主A: XML 版 SSH整合Demo https://github.com/MlqBeginner/BlogGardenWarehouse/blob/master/SSH%E6%95%B4%E5% ...
- linux应用之xampp集成环境的安装及配置(centos)
1.xampp集成环境的下载 在xampp的官网上选择对应系统的版本进行下载,官网地址:https://www.apachefriends.org/zh_cn/index.html #wget htt ...
- php常用函数htmlspecialchars、strip_tags、addslashes解析
本文章向大家介绍php开发中经常使用到的字符串函数htmlspecialchars.strip_tags.addslashes的使用方法及他们之间的区别,需要的朋友可以参考一下. 1.函数strip_ ...
- codeforces 653C C. Bear and Up-Down(乱搞题)
题目链接: C. Bear and Up-Down time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- windows 安装 pytorch
之前都在服务器上跑pytorch,近来发现新版本可在windows上跑了,甚是开心. 环境: windows7 python3 无CPU 步骤: 1. 确保确保python版本在3.5.3/3.6. ...
- ROS 负载均衡
[xuan89@MikroTik] > :for i from=1 to=$z do= {/ip firewall mangle add action=mark-connection chain ...
- loj515贪心只能过样例
bitset练习题... 位运算真的是玄学... 一开始真的“只能过样例” 后来发现把左移写成了小于号 鬼知道我在想什么/手动微笑 loj第一题 #include<iostream> #i ...
- HDU3037Saving Beans(组合数+lucas定理)
Problem Description Although winter is far away, squirrels have to work day and night to save beans. ...
- Python Class 的实例方法/类方法/静态方法
实例方法.类方法.静态方法 class MyClass(object): class_name = "MyClass" # 类属性, 三种方法都能调用 def __init__(s ...
- maven的配置文件取不同版本
1. 两个地方需要配置首先是要定义配置源,dev/test/prod对应的配置源需要指定:这是灵活的,可变的:其次要定义配置文件,该配置文件里面的内容采用占位符的方式来编制,在编译打包的过程中要动态根 ...