http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5072

区域赛真干的话题

通过率最高的一个问题 不到一半认为这OK  然后WA果断地 另外int无用long long WA

好久没用RMQ 调试也花了一点时间,

upper——bound返回的是大于x的第一个数的下标,最大当然是返回end的位置,注意推断下

注意一点,如果须要打印的张数为x              s[i]=<x<s[i+1]   事实上 要找的是ans=min(p[i]*x,s[i+1]*p[i+1],s[i+1]*p[i+2]...s[n]*p[n]),所以单单二分必定不行啊

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#include <cmath> using namespace std; const int MAXN = 1e5+100;
#define IN(s) freopen(s,"r",stdin)
#define ll long long
#define ull unsigned long long
const ll INF = ((ull)(-1))>>1;
ll s[MAXN],p[MAXN],tot[MAXN];
int n,m;
ll d[20];
ll st[MAXN][20];
void init()
{
for(int i=0;i<n;i++)st[i][0]=tot[i+1];
int k = (int)( log(double(n*1.0)/log(2.0)) ) +1;
for(int j=1;j<k;j++)
for(int i=0;i<n;i++)
{
if(i + d[j-1]-1 <n)
{
st[i][j] = min( st[i][j-1], st[i+d[j-1]][j-1] );
}
else break;
}
} void query(int q)
{
int y=n-1;
for(int i=0;i<q;i++)
{
int x,k;
scanf("%d",&x);
int id= upper_bound(s+1,s+1+n,x)-(s+1);
if(id>=n)
{
printf("%lld\n",p[n]*x);
continue;
}
ll ans=INF;
ans=min(ans,p[id]*x);
k=int( log(double(y-id+1)/log(2.0)) );
ans=min(ans,min(st[id][k],st[y-d[k]+1][k]));
printf("%lld\n",ans);
}
} int main()
{
//IN("zoj3726.txt");
d[0]=1;
for(int i=1;i<21;i++)d[i]=2*d[i-1];
int ncase;
scanf("%d",&ncase);
while(ncase--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%lld%lld",&s[i],&p[i]);
tot[i]=s[i]*p[i];
}
init();
query(m);
}
return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

ZOJ 3726 RMQ + 二分法的更多相关文章

  1. 2013 ACM/ICPC 长沙现场赛 A题 - Alice's Print Service (ZOJ 3726)

    Alice's Print Service Time Limit: 2 Seconds      Memory Limit: 65536 KB Alice is providing print ser ...

  2. Codeforces 514 D R2D2 and Droid Army(RMQ+二分法)

    An army of n droids is lined up in one row. Each droid is described by m integers a1, a2, ..., am, w ...

  3. zoj 3665 数论 二分法 两个参数

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4888 标题两个参数,途径:小参数的枚举,然后二分法大参数 想想两个点.以后就不 ...

  4. I - Beautiful People ZOJ - 2319 (二分法)

    The most prestigious sports club in one city has exactly N members. Each of its members is strong an ...

  5. HDU 4791 &amp; ZOJ 3726 Alice&#39;s Print Service (数学 打表)

    题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=4791 ZJU:http://acm.zju.edu.cn/onlinejudge/showP ...

  6. A - Alice's Print Service ZOJ - 3726 (二分)

    Alice is providing print service, while the pricing doesn't seem to be reasonable, so people using h ...

  7. 近几年ACM/ICPC区域赛铜牌题

    2013 changsha zoj 3726 3728 3736 3735 2013 chengdu hud 4786 4788 4790 2013 hangzhou hdu 4770 4771 47 ...

  8. ZOJ Design the city LCA转RMQ

    Design the city Time Limit: 1 Second      Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...

  9. ZOJ 3195 Design the city LCA转RMQ

    题意:给定n个点,下面n-1行 u , v ,dis 表示一条无向边和边权值,这里给了一颗无向树 下面m表示m个询问,问 u v n 三点最短距离 典型的LCA转RMQ #include<std ...

随机推荐

  1. perl对比两个文件的行

    perl对比两个文件的行 对比两个文件的各行,得到A与B相同的行/A与B不相同的行 主要功能 得到相同行 得到A中包含,B不包含的行 得到B中包含,A中不包含的行 具体执行情况 Perl代码 #!/u ...

  2. oracle 重置序列从指定数字开始的方法详解

    原文 oracle 重置序列从指定数字开始的方法详解 重置oracle序列从指定数字开始 declare n ); v_startnum ):;--从多少开始 v_step ):;--步进 tsql ...

  3. php实现求数组中出现次数超过一半的数字(isset($arr[$val]))(取不同数看剩)(排序取中)

    php实现求数组中出现次数超过一半的数字(isset($arr[$val]))(取不同数看剩)(排序取中) 一.总结 1.if(isset($arr[$val])) $arr[$val]++; //1 ...

  4. MySQL参数文件位置

    对于linux/unix: mysql --help|grep my.cnf   /etc/my.cnf, /etc/mysql/my.cnf, /usr/local/etc/my.cnf, ~/.m ...

  5. hibernate框架配置文件

    配置文件:和自己封装的工具类放在同一个包中 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...

  6. [Javascript] Identify and Deal with NaN in JavaScript

    Dealing with the special NaN value can be tricky in JavaScript. It behaves like a number and not a n ...

  7. 【转】C++11 并发指南五(std::condition_variable 详解)

    http://www.cnblogs.com/haippy/p/3252041.html 前面三讲<C++11 并发指南二(std::thread 详解)>,<C++11 并发指南三 ...

  8. [Ramda] Change Object Properties with Ramda Lenses

    In this lesson we'll learn the basics of using lenses in Ramda and see how they enable you to focus ...

  9. 小强的HTML5移动开发之路(28)—— JavaScript回顾3

    一.基本数据类型 number:数字类型 string:字符串 (注意s小写:string是基本类型) boolean:布尔类型   //前三个都有对应的包装类 null:空类型 undefined: ...

  10. iOS BCD编码

    inline static NSData* encodeBCD(NSString *value){ //NSString *value = @"123456"; NSMutable ...