首先学习一下lower_bound()

函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置

举例如下:

一个数组number序列为:4,10,11,30,69,70,96,100.设要插入数字3,9,111.pos为要插入的位置的下标

pos = lower_bound( number, number + 8, 3) - number,pos = 0.即number数组的下标为0的位置。

pos = lower_bound( number, number + 8, 9) - number, pos = 1,即number数组的下标为1的位置(即10所在的位置)。

pos = lower_bound( number, number + 8, 111) - number, pos = 8,即number数组的下标为8的位置(但下标上限为7,所以返回最后一个元素的下一个元素)。

所以,要记住:函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置,且last的位置是越界的!!~

返回查找元素的第一个可安插位置,也就是“元素值>=查找值”的第一个元素的位置

参考http://blog.csdn.net/niushuai666/article/details/6734403

http://blog.csdn.net/ck_boss/article/details/23040385

解题思路:

看网上题解说至多会有3个可以拆分的因素...应该是可以证明的吧 =  =

思路就是,先判断n是否可以由一个因素构成

然后就是判断n是否可以有两个因素构成

这里有点小技巧:

 int t2 = lower_bound( p , p + i + 1 , n - p[i] ) - p;//t2为另外一上限
for(int j = t2 ; j && p[i] + p[j] * 2 >= n ; j-- )

t2即为第二个因素的上限

然后查找第三个元素的时候,就简单了

只需要判断是否恰好满足条件即可

if(p[i]+p[j]<n){
int temp=n-p[i]-p[j];
t=lower_bound(p,p+,temp)-p;
if(p[t]==temp){
flag3=true;
c=i,d=j,e=t;
}

完整AC代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int p[];
void init(){
int sum=;
for(int i=;i<;i++){
sum += i;
p[i]=sum;
}
}
int main(){
int numCase,n;
scanf("%d",&numCase);
init();
while(numCase--){
scanf("%d",&n);
int t = lower_bound(p,p+,n)-p;//t为n在p数组中下标上限
//printf("t = %d\n",t);
if(p[t] == n){
printf("%d\n",t);
continue;
}
bool flag2=false,flag3=false;
int a,b,c,d,e;
for(int i = t ; i && p[i] * >= n ; i--){//p[i] * 2 >= n为下限
if(flag2) break;
int t2 = lower_bound(p,p+i+,n-p[i]) - p;//t2为另外一上限
for(int j=t2;j&&p[i]+p[j]*>=n;j--){
if(flag2)
break;
if(p[i]+p[j]==n){
flag2=true;
a=i,b=j;
break;
}
else if(flag3)
continue;
else if(p[i]+p[j]<n){
int temp=n-p[i]-p[j];
t=lower_bound(p,p+,temp)-p;
if(p[t]==temp){
flag3=true;
c=i,d=j,e=t;
}
}
}
} if(flag2)
printf("%d %d\n",a,b);
else
printf("%d %d %d\n",c,d,e);
}
return ;
}

Continuous Login


Time Limit: 2 Seconds      Memory Limit: 131072 KB      Special Judge

Pierre is recently obsessed with an online game. To encourage users to log in, this game will give users a continuous login reward. The mechanism of continuous login reward is as follows: If you have not logged in on a certain day, the reward of that day is 0, otherwise the reward is the previous day's plus 1.

On the other hand, Pierre is very fond of the number N. He wants to get exactly N points reward with the least possible interruption of continuous login.

Input

There are multiple test cases. The first line of input is an integer T indicates the number of test cases. For each test case:

There is one integer N (1 <= N <= 123456789).

Output

For each test case, output the days of continuous login, separated by a space.

This problem is special judged so any correct answer will be accepted.

Sample Input

4
20
19
6
9

Sample Output

4 4
3 4 2
3
2 3

Hint

20 = (1 + 2 + 3 + 4) + (1 + 2 + 3 + 4)

19 = (1 + 2 + 3) + (1 + 2 + 3 + 4) + (1 + 2)

6 = (1 + 2 + 3)

9 = (1 + 2) + (1 + 2 + 3)

Some problem has a simple, fast and correct solution.

ZOJ3768 夹逼查找【STL__lower_bound()_的应用】的更多相关文章

  1. sublime text 3 的在文件夹中查找的快捷键没有反应 的bug冲突

    11:19 2015/11/18 sublime text 3 的在文件夹中查找的快捷键没有反应 的bug冲突 在文件夹查找的快捷键:ctrl shift f没有反应,后来发现是百度输入法与它有问题, ...

  2. VIM在文件夹中查找

    在vim中提供2中方法来在其他文件或者文件夹中搜索字符串,第一种是vimgrep还有一种是grep. 如果只是在当前打开的文件中查找字符串的,使用 :? 后面加上想要搜索的字符串就可以. 这里要解决的 ...

  3. Linux常用命令--文件(夹)查找之find命令

    Linux系统用得越久,就会发现这真的是一个很优秀的系统,各种方便各种实用各种高效率. 晚饭前写一下find命令的笔记. 其实这篇笔记,也是看到一篇外文博客,写得不错,自己拿来练一练,然后才顺便写篇笔 ...

  4. cb34a_c++_STL_算法_查找算法_(7)_lower_bound

    cb34a_c++_STL_算法_查找算法_(7)_lower_bound//针对已序区间的查找算法,如set,multiset关联容器-自动排序lower_bound()--第一个可能的位置uppe ...

  5. cb33a_c++_STL_算法_查找算法_(6)binary_search_includes

    cb33a_c++_STL_算法_查找算法_(6)binary_search_includes//针对已序区间的查找算法,如set,multiset关联容器-自动排序binary_search(b,e ...

  6. cb32a_c++_STL_算法_查找算法_(5)adjacent_find

    cb32a_c++_STL_算法_查找算法_(5)adjacent_findadjacent_find(b,e),b,begin(),e,end()adjacent_find(b,e,p),p-par ...

  7. cb31a_c++_STL_算法_查找算法_(4)find_first_of

    cb31a_c++_STL_算法_查找算法_(4)find_first_offind_first_of(b,e,sb,se),sb,second begin, se,second end();find ...

  8. cb30a_c++_STL_算法_查找算法_(3)search_find_end

    cb30a_c++_STL_算法_查找算法_(3)search_find_endsearch()pos = search(ideq.begin(), ideq.end(), ilist.begin() ...

  9. cb29a_c++_STL_算法_查找算法_(2)search_n

    cb29a_c++_STL_算法_查找算法_(2)search_n//比如:连续查找连续的n个8search_n(b,e,c,v),迭代器b,begin(),e,end().连续的c个vpos=sea ...

随机推荐

  1. 【科研论文】基于文件解析的飞行器模拟系统软件设计(应用W5300)

    摘要: 飞行器模拟系统是复杂飞行器研制和使用过程中的重要设备,它可以用来模拟真实飞行器的输入输出接口,产生与真实系统一致的模拟数据,从而有效避免因使用真实飞行器带来的高风险,极大提高地面测发控系统的研 ...

  2. EF使用时异常:对一个或多个实体的验证失败。有关详细信息

    //最顶级异常中是不提示具体哪个字段验证失败,必须到详细异常类型中查看 try { //EF操作 } catch (System.Data.Entity.Validation.DbEntityVali ...

  3. Angular JS 学习笔记(二)

    1. AngularJS 是一个 JavaScript 框架.它可通过 <script> 标签添加到 HTML 页面.AngularJS 通过 指令 扩展了 HTML,且通过 表达式 绑定 ...

  4. c#程序添加其他网站的webservice引用。

    前言: 有A网站,B网站两个网站独立运行.某一天,需要两个网站进行合作来完成一件事,比如用A网站接收一些数据进行处理,存入中转数据库,B网站定时去取数据进入自己的系统进行处理. 可能我们会先想到在B网 ...

  5. BZOJ 2190 仪仗队

           这道题的关键就是找到所有的点中,x与y互质的点,第一反应必定是暴搜,稍想一下可以从中分开求一半,但范围仍然限定了这条路行不通,仔细画了几张图后发现下图中从第三行起第k行可连fai k个, ...

  6. ELF文件数据布局探索(1)

    作为一名Linux小白,第一次看到a.out这个名字,感觉实在是奇怪,搜了一下才知道这是编译器输出的默认可执行文件名 然后vi一下,哇,各种乱码,仔细看看,发现了三个清晰的字符ELF.继续搜索, 第一 ...

  7. C++的发展,特点和源程序构成

    最近一段时间在学习C++,也借了几本相关的书籍.因为之前主要用C#写程序,大概写了也有两年了吧.所以在回过头来学习C++,还是挺快的.但是我觉得光看书是不行的,要写!!因此我想把我整个学习C++的过程 ...

  8. 南阳师范学院ACM官方博客使用说明

    登录之后跳到如下页面: 点击博客进入如下页面: 这里每个人都有一个专栏,大家可以把自己写得博客放到自己的专栏下,同时也可以查看其他人写的博客,相互交流! 在发表博客的时候,选择个人分类中自己的专栏即可 ...

  9. js的初始化

    在项目中遇到了类似于这样的js, myinit.js文件: var wyl_01 = { tip : function(msg){ alert('i am a tip,msg:'+msg); }, t ...

  10. pageContext.request.contextPath 和 request.getContextPath()

    作用是取出部署的应用程序名,这样不管如何部署,所用路径都是正确的. El表达式的写法:${pageContext.request.contextPath} jsp的写法:<%=request.g ...