Subsequence(暴力+二分)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10875 | Accepted: 4493 |
Description
Input
Output
Sample Input
2
10 15
5 1 3 5 10 7 4 9 2 8
5 11
1 2 3 4 5
Sample Output
2
3
题解:让求连续的一个序列数之和大于等于S的最短序列长度;这道题,我前后换了三种方法才A了,刚开始看到,一想不就是个线段树,写完了发现答案不对。。。然后发现线段树只能找到一半,
还呆加上区间合并,区间合并也很可能不对,然后想着树状数组,写了一半感觉还不如用个数组直接存到i的总值和,然后找到起点终点就好了,于是开始了暴力,暴力肯定超时啊;就想着二分下;
调试了下就过了;二分还要判断下当前点与前一个点插哪个;
可能我写的太麻烦了。。。有空看看大神怎么写的;
AC代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
#define ll root<<1
#define rr root<<1|1
#define lson ll,l,mid
#define rson rr,mid+1,r
const int INF=0x3f3f3f3f;
const int MAXN=;
int tree[MAXN]; int main(){
int T,N,M;
SI(T);
while(T--){
SI(N);SI(M);
mem(tree,);
int ans=INF;
int t=;
for(int i=;i<N;i++){
int x;
SI(x);
if(!i)tree[i]=x;
else tree[i]=tree[i-]+x;
}
for(int i=N-;i>=;i--){
if(tree[i]-M>=){
int t=lower_bound(tree,tree+i,tree[i]-M)-tree;
if(tree[i]-tree[t]>=M)ans=min(ans,i-t);
else ans=min(ans,i-t+);
//printf("%d\n",ans);
}
}
if(ans==INF)puts("");
else printf("%d\n",ans);
}
return ;
}
// handsomecui.cpp : 定义控制台应用程序的入口点。
// //#include "stdafx.h"
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int MAXN = ;
LL seq[MAXN];
int erfen(int l, int r, int v){
int mid;
while(l <= r){
mid = (l + r) >> ;
if(seq[mid] >= v)
r = mid - ;
else
l = mid + ;
}
return r + ;
}
int main()
{
int N, S, T;
cin >> T;
while(T--){
cin >> N >> S;
int ans = 0x3f3f3f3f;
memset(seq, , sizeof(seq));
for(int i = ; i < N; i++){
cin >> seq[i];
if(i)
seq[i] += seq[i - ];
}
for(int i = ; i < N; i++){
if(seq[i] - S < )
continue;
int p = erfen(, i - , seq[i] - S);
//if(p < 0 || p >= i)continue;
if(seq[p] + S > seq[i])p--;
ans = min(ans, i - p);
}
if(ans == 0x3f3f3f3f)
puts("");
else
printf("%d\n",ans);
}
return ;
}
Subsequence(暴力+二分)的更多相关文章
- poj3977 - subset - the second time - 暴力 + 二分
2017-08-26 11:38:42 writer:pprp 已经是第二次写这个题了,但是还是出了很多毛病 先给出AC代码: 解题思路: 之前在培训的时候只是笼统的讲了讲怎么做,进行二分对其中一边进 ...
- Codeforces Round #367 (Div. 2) A B C 暴力 二分 dp(字符串的反转)
A. Beru-taxi time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- Vijos P1116 一元三次方程求解【多解,暴力,二分】
一元三次方程求解 描述 有形如:ax^3+bx^2+cx+d=0 这样的一个一元三次方程.给出该方程中各项的系数(a,b,c,d 均为实数),并约定该方程存在三个不同实根(根的范围在-100至100之 ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) D. Dense Subsequence 暴力
D. Dense Subsequence 题目连接: http://codeforces.com/contest/724/problem/D Description You are given a s ...
- I Count Two Three HDU - 5878(暴力二分)
为甚么16年Qingdao Online 都是暴力题emm///... 先暴力预处理 然后lower _bound二分 #include <iostream> #include <c ...
- HDU6127 简单几何 暴力二分
LINK 题意:给出n个点,每个点有个权值,可以和任意另外一点构成线段,值为权值积.现问过原点的直线中交所有线段的权值和的最大值,注意直线必不经过点. 思路:直线可以将点集分为两侧,此时的权值为两侧点 ...
- Codeforces Beta Round #3 B. Lorry 暴力 二分
B. Lorry 题目连接: http://www.codeforces.com/contest/3/problem/B Description A group of tourists is goin ...
- Codeforces Round #345 (Div. 2) D. Image Preview 暴力 二分
D. Image Preview 题目连接: http://www.codeforces.com/contest/651/problem/D Description Vasya's telephone ...
- 8VC Venture Cup 2016 - Elimination Round E. Simple Skewness 暴力+二分
E. Simple Skewness 题目连接: http://www.codeforces.com/contest/626/problem/E Description Define the simp ...
随机推荐
- aliyun.com
https://help.aliyun.com/knowledge_detail/39495.html?spm=5176.7839494.2.1.AhdvPM
- Photoshop技能167个经典的Photoshop技巧大全
Photoshop技能167个经典的Photoshop技巧大全 学PS基础:Photoshop 技能167个 经典的Photoshop技巧大全,如果你是初级阶段的水平,熟读此文并掌握,马上进阶为中级 ...
- hdu 4496 D-City(并查集)
Problem Description Luxer is a really bad guy. He destroys everything he met. One day Luxer went to ...
- 格而知之1:UIButton中imageView和titleLabel的位置调整
在使用UIButton时,有时候需要调整按钮内部的imageView和titleLabel的位置和尺寸.在默认情况下,按钮内部的imageView和titleLabel的显示效果是图片在左文字在右,然 ...
- ShareSDK QQ分享失败的坑
QQ分享的话,有标题和内容字符数限制,QQ好友的话限制的很小,标题30字符,内容40字符.分享之前限制一下.
- c#中 HttpContext作用(一)【转】
HttpContext 主要作用是要获得你客户端向服务端请求提交的相关信息 HttpContext 类:封装有关个别 HTTP 请求的所有 HTTP 特定的信息.也有人叫上下文信息. 1.生存周期 ...
- JAVA Socket连接服务器时可能抛出的异常
1.UnknownHostException:host(非ip)无法识,就会报该异常,www.google.com.hk这个虽然也ping不通,但是不会报该错,因为他是一个确实存在的域名.他会报Con ...
- Arcgis Runtime sdk for android 授权
要下载和安装 ArcGISRuntime SDK for Android,您需要注册开发者账户,进而便拥有了访问所有功能的权限,从而实现开发和测试目的.但是,这种情况下,应用程序中的所有地图都具有水印 ...
- Android中view的事件
view:top.left.right.bottom,相对于parent的位置参数,获取通过get*()来获取.width=right-left.height=bottom-top.x=left+tr ...
- asp.net mvc 生成条形码
using System; using System.Collections; using System.Collections.Generic; using System.Drawing; usin ...