ZOJ 3726 RMQ + 二分法
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 + 二分法的更多相关文章
- 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 ...
- 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 ...
- zoj 3665 数论 二分法 两个参数
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4888 标题两个参数,途径:小参数的枚举,然后二分法大参数 想想两个点.以后就不 ...
- I - Beautiful People ZOJ - 2319 (二分法)
The most prestigious sports club in one city has exactly N members. Each of its members is strong an ...
- HDU 4791 & ZOJ 3726 Alice's Print Service (数学 打表)
题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=4791 ZJU:http://acm.zju.edu.cn/onlinejudge/showP ...
- 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 ...
- 近几年ACM/ICPC区域赛铜牌题
2013 changsha zoj 3726 3728 3736 3735 2013 chengdu hud 4786 4788 4790 2013 hangzhou hdu 4770 4771 47 ...
- ZOJ Design the city LCA转RMQ
Design the city Time Limit: 1 Second Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...
- ZOJ 3195 Design the city LCA转RMQ
题意:给定n个点,下面n-1行 u , v ,dis 表示一条无向边和边权值,这里给了一颗无向树 下面m表示m个询问,问 u v n 三点最短距离 典型的LCA转RMQ #include<std ...
随机推荐
- php压缩
php压缩的一个demo,随便测试了一下,可以用 <?php class PHPZip { private $ctrl_dir = array(); private $datasec = arr ...
- ios开发runtime学习三:动态添加方法(实际应用少,面试)
#import "ViewController.h" #import "Person.h" /* 1: Runtime(动态添加方法):OC都是懒加载机制,只要 ...
- pandas 学习(四)—— 数据处理(清洗)、缺失值的处理
创建 DataFrame: df = pd.DataFrame(np.random.randint(0, 10, (2, 4)), columns=list('ABCD')) 0. 为 data fr ...
- Codeforces Round #315 (Div. 2)——C. Primes or Palindromes?
这道题居然是一个大暴力... 题意: π(n):小于等于n的数中素数的个数 rub(n) :小于等于n的数中属于回文数的个数 然后给你两个数p,q,当中A=p/q. 然后要你找到对于给定的A.找到使得 ...
- Android改变图片颜色的自定义控件
效果如下: 理解:Xfermode的16总模式如图 第一步: package com.rong.activity; import com.rong.test.R; import android.con ...
- php实现 字符个数统计
php实现 字符个数统计 一.总结 一句话总结: 1.php字符转ascii码函数? ord() 6 if(ord($input[$i]) < 128 and 0 < ord($input ...
- 安装innotop
安装方法一: 下载地址:https://github.com/innotop/innotop yum install -y perl-TermReadKey yum install -y perl-D ...
- C# WebQQ协议群发机器人(一)
原创性申明 本文地址 http://blog.csdn.net/zhujunxxxxx/article/details/38931287 转载的话请注明出处. 之前我也写过一篇使用python来实现的 ...
- php中读取文件内容的几种方法。(file_get_contents:将文件内容读入一个字符串)
php中读取文件内容的几种方法.(file_get_contents:将文件内容读入一个字符串) 一.总结 php中读取文件内容的几种方法(file_get_contents:将文件内容读入一个字符串 ...
- linux使用.rpm包安装mysql
一:下载mysql的.rpm安装包 点击链接查看下载教程:点击打开链接 二:创建目录,上传文件 创建mysql目录:# mkdir mysql 进入目录:# cd mysql 将下载好的MySQL-s ...