题目连接:http://oj.haihongblog.com/problem.php?id=1003

线段树维护区间最小值,并且求解下标

#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std; const int maxn=+ ;
const int INF=0x7FFFFFFF;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int N,K,zzt;
vector<int> ans; int MIN[maxn<<]; void PushUP(int rt)
{
MIN[rt]=min(MIN[rt<<],MIN[rt<<|]);
} void Build(int l,int r,int rt)
{
if(l==r)
{
scanf("%d",&MIN[rt]);
return;
}
int m=(l+r)>>;
Build(lson);
Build(rson);
PushUP(rt);
} void Update(int p,int add,int l,int r,int rt)
{
if(l==r)
{
MIN[rt]=add;
return;
}
int m=(l+r)>>;
if(p<=m)
Update(p,add,lson);
else
Update(p,add,rson);
PushUP(rt);
} int Query(int L,int R,int l,int r,int rt)
{
if(L<=l&&R>=r) return MIN[rt];
int m=(l+r)>>;
int ret=INF;
if(L<=m) ret=min(ret,Query(L,R,lson));
if(R>m) ret=min(ret,Query(L,R,rson));
return ret;
} void Find(int L,int R,int l,int r,int rt)
{ if(L<=l&&R>=r)
{
int lx=l,rx=r,node=rt;
while()
{
int mx=(lx+rx)/;
if(lx==rx) {zzt=lx;break;}
if(K>=MIN[*node])
{
node=*node;
rx=mx;
continue;
} if(K>=MIN[*node+])
{
node=*node+;
lx=mx+;
continue;
}
}
return;
} int m=(l+r)>>;
if(K>=MIN[rt<<]&&L<=m) Find(L,R,lson);
if(zzt!=-) return;
if(K>=MIN[rt<<|]&&R>m) Find(L,R,rson);
if(zzt!=-) return;
} int main()
{
// freopen("F:\\test (1).in","r",stdin);
// freopen("F:\\out.txt","w",stdout); while(~scanf("%d%d",&N,&K)){
Build(,N,); bool fail=;
int pos=;
ans.clear(); for(int i=;i<=N;i++)
{
int P=Query(pos,N,,N,);
if(K>=P) //后面一段存在可以吃的点
{
zzt=-; //找到第一个大于等于K的位置 zzt
Find(pos,N,,N,);
ans.push_back(zzt);
pos=zzt;
//该位置的值更新为INF
Update(zzt,INF,,N,);
K++;
}
else //后面一段不存在可以吃的点,从头开始找
{
P=Query(,N,,N,);
if(K>=P)
{
zzt=-; //找到第一个大于等于K的位置 zzt
Find(,N,,N,);
ans.push_back(zzt);
pos=zzt;
//该位置的值更新为INF
Update(zzt,INF,,N,);
K++;
} else //找不到了,失败
{
fail=;
break;
}
}
} if(fail) printf("-1\n");
else
{
for(int i=;i<ans.size();i++)
{
printf("%d",ans[i]);
if(i<ans.size()-) printf(" ");
else printf("\n");
}
}
}
return ;
}

HaiHongOJ 1003 God Wang的更多相关文章

  1. Bestcoder#5 1003

    Bestcoder#5 1003 Poor RukawTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  2. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

  3. dp 动态规划 hdu 1003 1087

    动态规划就是寻找最优解的过程 最重要的是找到关系式 hdu 1003 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目大意:求最大字序列和, ...

  4. PKU 1003解题

    首先庆祝一下,今天连A了3题.感觉后面这题太简单了.. 由于英文不好 ,找了个翻译: 若将一叠卡片放在一张桌子的边缘,你能放多远?如果你有一张卡片,你最远能达到卡片长度的一半.(我们假定卡片都正放在桌 ...

  5. POJ做题笔记:1000,1004,1003

    1000 A+B Problem 题目大意:输入两个数a和b,输出他们的和. 代码: #include <stdio.h> int main() { int a, b; while (sc ...

  6. 未注册wang域名批量查询工具

    一.支持规则查询 可自定义生成域名进行查询,可生成任意位数的字母数字域名,根据[声母].[韵母]生成单拼,双拼,三拼等域名,还可根据字典生成,支持全拼.首拼识别,全国城市区号.城市全拼.城市首拼.热门 ...

  7. 迪杰斯特拉算法——PAT 1003

    本文主要是将我对于我对于迪杰斯特拉算法的理解写出来,同时通过例题来希望能够加深对于算法的理解,其中有错误的地方希望大家指正. 迪杰斯特拉算法 我将这个算法理解成一个局部到整体的算法,这个方法确实越研究 ...

  8. hdu5024 Wang Xifeng's Little Plot (水

    http://acm.hdu.edu.cn/showproblem.php?pid=5024 网络赛 Wang Xifeng's Little Plot Time Limit: 2000/1000 M ...

  9. Codeforces Round #270 1003

    Codeforces Round #270 1003 C. Design Tutorial: Make It Nondeterministic time limit per test 2 second ...

随机推荐

  1. linux手工笔记

    一.vi编辑器常用命令: -------------------------------- •插入文字:I键A键 •退回命令模式:ESC •存盘:w •不存盘退出:q •存盘退出:wq •强制退出:q ...

  2. hdu_5748_Bellovin(LIS)

    题目链接:hdu_5748_Bellovin 题意: 给你一个数列ai,设f(a1,a2,a3,..an)=(f1,f2,f3,...,fn),其中fi表示以ai结尾的最长递增子序列长度,注意:必须要 ...

  3. hdu_1011_Starship Troopers(树形DP)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 题意:有N个房间,房间的连通性为树形的,就是说你要占领子结点,必须要先占领 父结点,每个房间有第 ...

  4. hdu_1254_推箱子(双BFS)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1254 题解:以箱子为主体,第一层BFS,然后用第二层BFS来判断人是否可以到达,这里细节比较多,要注意 ...

  5. webapp前端开发软键盘与position:fixed为我们带来的不便

    前提:我们考虑兼容的环境为android和ios两种智能手机 兼容环境测试结果显示android的表现明显好于ios,ios手机在软键盘呼起收起时存在着很严重的兼容性问题 场景展示: 页面正常状态 软 ...

  6. 获得正在编辑行的数据 esayui datagrid

    function getEditRow(datagridId) {//datagridId为table容器的id var input = $('#' + datagridId).parent().fi ...

  7. org.hibernate.PropertyNotFoundException: Could not find a getter for employee in class com.itcast.f_hbm_oneToMany.Department

    <hibernate-mapping package="com.itcast.f_hbm_oneToMany"> <class name="Depart ...

  8. NavigationBar的显隐和颜色设置

    [self.navigationController setNavigationBarHidden:NO animated:NO]; self.navigationController.navigat ...

  9. Inno Setup入门(十七)——Inno Setup类参考(3)

    分类: Install Setup 2013-02-02 11:28 433人阅读 评论(0) 收藏 举报 标签 标签(Label)是用来显示文本的主要组件之一,也是窗口应用程序中最常用的组件之一,通 ...

  10. zk reconnect

    http://blog.csdn.net/hengyunabc/article/details/41450003 http://blog.csdn.net/hengyunabc/article/det ...