csu1364 Interview
对拍了一波才找到的错误,此题我用的是二分答案加倍增查询,实际上query那里我觉得仍然有缺陷,因为每一次我的查找还是在循环找到一个k使得x+2^k <= y,而错的地方也正在此地,一开始没有判断x+2^k > y,反而直接将k=y的二进制下的最大的为1的位置,以后一定要注意边界条件。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<string>
#include<set>
#include<algorithm>
#include<vector>
#include<queue>
#include<list>
#include<cmath>
#include<cstring>
#include<map>
#include<stack>
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 2005
#define ull unsigned long long
#define ll long long
#define hashmod 99999839
#define mod 9997
int a[maxn];
int dp[maxn][],p[];
int n,k;
//dp(i,j) 表示区间[i,i+2^j]中的最大值
//dp(i,j) = max(dp(i,j-1),dp(i+2^(j-1),j-1);
//dp(i,j) = max(dp(i,j-1),q(i+2^(j-1)+1,i+2^j));
//如果查询[x,y]中的最大值则q(x,y) = max(dp(x,k),q(x+2^k+1,y)),x+2^k <= y,k <= log(y-x)
int bitsearch(int x){
int l = ,r = ,mid,ans;
while(l <= r){
mid = (l+r)>>;
if(p[mid] > x) r = mid - ;
else l = mid + ,ans = mid;
}
return ans;
}
int query(int x,int y){
if(x > y) return ;
if(x == y) return a[x];
int k = bitsearch(y);
while(x + p[k] > y) k--;
return max(dp[x][k],query(x+p[k]+,y));
}
bool judge(int m){
int t = n / m,sum = ;
for(int i = ;i <= t * m;i += t){
sum += query(i,i+t-);
}
if(sum > k) return true;
return false;
}
void init(){
p[] = ;
for(int i = ;i <= ;++i) p[i] = p[i - ] << ;
}
int main(){
// freopen("a.in","r",stdin);
// freopen("b.out","w",stdout);
init();
while(~scanf("%d%d",&n,&k)){
if(n < && k < ) break;
memset(dp,,sizeof(dp));
int Max = ;
for(int i = ;i <= n;++i) scanf("%d",&a[i]),dp[i-][] = max(a[i-],a[i]),Max = max(Max,a[i]);
dp[n][] = a[n];
int li = bitsearch(n) + ;
for(int j = ;j <= li;++j){
for(int i = ;i <= n;++i){
if(i + p[j] > n){
if(i + p[j - ] <= n) dp[i][j] = max(dp[i][j-],dp[i+p[j-]][j-]);
else dp[i][j] = dp[i][j-];
continue;
}
dp[i][j] = max(dp[i][j-],dp[i+p[j-]][j-]);
}
}
int l = ,r = n,mid,ans = -;
while(l <= r){
mid = (l + r) >> ;
if(judge(mid)){
ans = mid;
r = mid - ;
}
else l = mid + ;
}
printf("%d\n",ans);
}
return ;
}
csu1364 Interview的更多相关文章
- Pramp mock interview (4th practice): Matrix Spiral Print
March 16, 2016 Problem statement:Given a 2D array (matrix) named M, print all items of M in a spiral ...
- WCF学习系列二---【WCF Interview Questions – Part 2 翻译系列】
http://www.topwcftutorials.net/2012/09/wcf-faqs-part2.html WCF Interview Questions – Part 2 This WCF ...
- WCF学习系列一【WCF Interview Questions-Part 1 翻译系列】
http://www.topwcftutorials.net/2012/08/wcf-faqs-part1.html WCF Interview Questions – Part 1 This WCF ...
- Amazon Interview | Set 27
Amazon Interview | Set 27 Hi, I was recently interviewed for SDE1 position for Amazon and got select ...
- Java Swing interview
http://www.careerride.com/Swing-AWT-Interview-Questions.aspx Swing interview questions and answers ...
- Pramp - mock interview experience
Pramp - mock interview experience February 23, 2016 Read the article today from hackerRank blog on ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- [译]Node.js Interview Questions and Answers (2017 Edition)
原文 Node.js Interview Questions for 2017 什么是error-first callback? 如何避免无止境的callback? 什么是Promises? 用什么工 ...
- WCF学习系列三--【WCF Interview Questions – Part 3 翻译系列】
http://www.topwcftutorials.net/2012/10/wcf-faqs-part3.html WCF Interview Questions – Part 3 This WCF ...
随机推荐
- 使用Dotfuscator保护.NET DLL加密DLL,防止DLL反编译
1.下载地址 https://pan.baidu.com/s/1ztWlBxw1Qf462AE7hQJQRg 2.操作步骤 2.1安装后打开DotfuscatorPro软件,如下图所示: 2.2 选择 ...
- Java文件上传(基础性)
/** * * 上传文件 * */ public class FileUploadServlet2 extends HttpServlet { protected void doGet(HttpSer ...
- 微信官方UI库—WeUI
WeUI 为微信 Web 服务量身设计 概述 WeUI是一套同微信原生视觉体验一致的基础样式库,由微信官方设计团队为微信 Web 开发量身设计,可以令用户的使用感知更加统一.包含button.cell ...
- Python __str__(self)和__unicode__(self)
object.__str__(self) Called by the str() built-in function and by the print statement to compute the ...
- 重构26-Remove Double Negative(去掉双重否定)
尽管我在很多代码中发现了这种严重降低可读性并往往传达错误意图的坏味道,但这种重构本身还是很容易实现的.这种毁灭性的代码所基于的假设导致了错误的代码编写习惯,并最终导致bug.如下例所示: public ...
- qt creator转换到 COFF 期间失败: 文件无效或损坏
转载请注明出处http://www.cnblogs.com/dachen408/p/7226198.html 环境 Qt5.5+Vs2010,删除vs2010安装目录bin下的cvtres.exe解决 ...
- PDO 错误处理模式
异常模式: $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 这个模式需要配合 try 使用 :一旦出错,就会: 1. ...
- 第3节 hive高级用法:16、17、18
第3节 hive高级用法:16.hive当中常用的几种数据存储格式对比:17.存储方式与压缩格式相结合:18.总结 hive当中的数据存储格式: 行式存储:textFile sequenceFile ...
- Delphi新注释
标准请看帮助文件里的:XML Documentation Comments 个人常用 <summary></summary>:摘要 /// <summary> // ...
- Java代码的编译和执行
Java代码编译和执行的整个过程包含了以下三个重要的机制: (1)Java源码编译机制 (2)类加载机制 (3)类执行机制 1.Java代码编译是由Java源码编译器来完成,流程图: Java 源码编 ...