题目连接: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. inflate的使用注意事项

    public View inflate (int resource, ViewGroup root, boolean attachToRoot) 我们在使用这个方法时,要清楚原理,下面是这个方法的文档 ...

  2. 按钮特效-Enter键自动提交表单

    —————————————————————— <script type="text/javascript">                        //当用户按 ...

  3. Mac 下格式化U盘

    diskutil list 查看U盘盘符: lapommedeMacBook-Pro:~ lapomme$ diskutil list /dev/disk0 (internal, physical): ...

  4. Uploadify自定义提示信息

    Uploadify是一款基于Jquery的上传插件,用起来很方便.但上传过程中的提示语言为英文,这里整理下如何修改英文为中文提示.方法1:直接修改uploadify.js中的提示信息,将英文提示改成对 ...

  5. 搭建Ubuntu下c/c++编译环境【转】

    1.       安装Ubuntu. 2.       安装gcc 方法一: sudo apt-get  install  build-essential 安装完了可以执行 gcc--version的 ...

  6. 制作 macOS Sierra 正式版U盘USB启动安装盘方法教程 (全新安装 Mac 系统)

    使用命令行创建制作 macOS Sierra 正式版 USB 安装盘 1.准备一个 8GB 或更大容量的 U盘,并备份好里面的所有资料. 2.下载好 macOS Sierra 正式版的安装程序(app ...

  7. js连续赋值、指针

    jq的源码中有很多连续赋值,类似这样的: var a = {n:1}; var b = a; // 持有a,以回查 a.x = a = {n:2}; alert(a.x);// --> unde ...

  8. 笨方法学python--安装和准备

    1 下载并安装python http://python.org/download 下载python2.7. python2.7并不是python3.5的旧版本. python2现在应用较广,网上资料较 ...

  9. 解决centos无法上传文件和打开文件夹

    使用yum搭建了ftp服务..yum的使用参考:http://blog.csdn.net/enson16855/article/details/9140623 windows使用FileZilla连接 ...

  10. Android摄像头:只拍摄SurfaceView预览界面特定区域内容(矩形框)---完整(原理:底层SurfaceView+上层绘制ImageView)

    Android摄像头:只拍摄SurfaceView预览界面特定区域内容(矩形框)---完整实现(原理:底层SurfaceView+上层绘制ImageView) 分类: Android开发 Androi ...