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 ...
随机推荐
- AsyncTask加载图片
http://blog.csdn.net/sodino/article/details/7741674 http://www.cnblogs.com/weisenz/archive/2012/04/1 ...
- Makefile里调用Shell注意点
http://www.linuxidc.com/Linux/2012-04/59093.htm 大家经常编写和使用Makefile, Makefile里面也经常用到shell, 但对其中一些需要注意的 ...
- Codeforces 433 C. Ryouko's Memory Note
C. Ryouko's Memory Note time limit per test 1 second memory limit per test 256 megabytes input stand ...
- 让Qt for Windows Phone 8.1在真机上执行
让Qt for Windows Phone 8.1在真机上执行 前面几篇博文是为这篇文章做铺垫的,终于目的为的是使用Qt框架制作出可以在Windows Phone 8.1真机上执行的程序.因为Qt f ...
- PHP 生成UUID的方法
. . . ...
- RDLC报表系列(四) 矩阵
继续接上一篇的内容,本文主要是讲矩阵的内容 用到的数据源如下: DataTable dt = new DataTable(); dt.Columns.Add("FiscalYear" ...
- android stuido 快捷键
Alt+回车 导入包,自动修正 Ctrl+N 查找类 Ctrl+Shift+N 查找文件 Ctrl+Alt+L 格式化代码 Ctrl+Alt+O 优化导入的类和包 Alt+Insert 生成代码 ...
- java工厂类与反射机制
java 简单工厂类 2012-04-22 15:44:07| 分类: java | 标签:java工厂类 简单工厂类 |举报|字号 订阅 简单工厂模式需要由以下角色组成: 接口 ...
- ZOJ 1530 - Find The Multiple
Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal repr ...
- 兼容IE6,IE7和firefox可以使用的一些css hack:
.一些问题是浏览器自身的问题,遇到问题发生无法避免的情况下,那就要考虑使用一些css hack了,以下是针对IE6,IE7和firefox可以使用的一些css hack:(1) a: 针对区别IE6 ...