Description A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are given. Write a program to find the minimal length of the subsequence of consecutive elements o…
Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10172   Accepted: 4160 Description A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) ar…
Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8403   Accepted: 3264 Description A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are…
题目连接 http://poj.org/problem?id=3061 Subsequence Description A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are given. Write a program to find the minimal len…
题目大意:给出长度为n的一个序列,给出一个数字S,求长度最短的序列和大于等于S的连续子序列,输出该长度,如果没有答案输出0. 题目思路:看数据范围,这道题就是卡时间的.我们可以用sum[i]记录前i项和,然后用二分优化查找过程.这样时间复杂度为 n*logn.具体看代码吧. #include<cstdio> #include<stdio.h> #include<cstdlib> #include<cmath> #include<iostream>…
地址 http://poj.org/problem?id=3061 解法1 使用双指针 由于序列是连续正数 使用l r 表示选择的子序列的起始 每当和小于要求的时候 我们向右侧扩展 增大序列和 每当和大于等于要求的时候 我们将子序列左边的数字剔除 看能是在减少长度情况下 还能保持子序列和满足要求 这样在指定起点下的满足要求的最短子序列和都会被记录 然后在比较出最短长度的子序列 如图 代码 #include <iostream> #include <vector> #include…
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12333 Accepted: 5178 Description A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are gi…
http://poj.org/problem?id=3061 题目大意: 给定长度为n的整列整数a[0],a[1],--a[n-1],以及整数S,求出总和不小于S的连续子序列的长度的最小值. 思路: 方法一: 首先求出各项的和sum[i],这样可以在O(1)的时间内算出区间上的总和,这样,枚举每一个起点i,然后二分搜索出结果大于sum[i]+tot的最小下标.(tot是题目中的S) 总的时间为O(nlogn) 方法二: 设以a[s]开始的总和最初大于S时的连续子序列为a[s]+a[s+1]+--…
题目链接: 传送门 Subsequence Time Limit: 1000MS     Memory Limit: 65536K 题目描述 给定长度为n的数列整数以及整数S.求出总和不小于S的连续子序列的长度的最小值.如果解不存在,则输出0. 思路 O(nlogn)算法 #include<cstdio> #include<iostream> #include<algorithm> using namespace std; int main() { int T; sca…
[题目链接] http://poj.org/problem?id=3061 [题目大意] 给出S和一个长度为n的数列,问最短大于等于S的子区间的长度. [题解] 利用双指针获取每一个恰好大于等于S的子区间,更新答案即可. [代码] #include <cstdio> int T,a[100005]; int main(){ scanf("%d",&T); while(T--){ int n,S,s,h,t,ans; scanf("%d%d",&a…
Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18145   Accepted: 7751 Description A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) ar…
题目链接 Description A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are given. Write a program to find the minimal length of the subsequence of consecutive eleme…
Description A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are given. Write a program to find the minimal length of the subsequence of consecutive elements o…
转自博客:http://blog.chinaunix.net/uid-24922718-id-4848418.html 尺取法就是两个指针表示区间[l,r]的开始与结束 然后根据题目来将端点移动,是一种十分有效的做法.适合连续区间的问题 poj3061 给定长度为n的数列整数a0,a1,a2,a3 ..... an-1以及整数S.求出综合不小于S的连续子序列的长度的最小值.如果解不存在,则输出0. 这里我们拿第一组测试数据举例子,即 n=10, S = 15, a = {5,1,3,5,10,7…
Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13955   Accepted: 5896 Description A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) ar…
Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9050   Accepted: 3604 Description A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are…
Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14698   Accepted: 6205 Description A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) ar…
题意 : 找出给定序列长度最小的子序列,子序列的和要求满足大于或者等于 S,如果存在则输出最小长度.否则输出 0(序列的元素都是大于 0 小于10000) 分析 : 有关子序列和的问题,都可以考虑采用先构造前缀和的方式来进行接下来的操作 ( 任意子序列的和都能由某两个前缀和的差表示 ). 二分做法 ==> 我们枚举起点,对于每一个起点 St 二分查找看看 St 后面有没有前缀和是大于或者等于 [ St的前缀和 ] + S 的,如果有说明从当前起点开始有一个终点使得起终之和是大于或者等于 S 的,…
<题目链接> 题目大意: 给你一段长度为n的整数序列,并且给出一个整数S,问你这段序列中区间之和大于等于S的最短区间长度是多少. 解题分析:本题可以用二分答案做,先求出前缀和,然后枚举区间长度,然后再判断其是否合法即可,复杂度$O(nlog(n))$.同时,尺取法也是一个不错的选择,通过不断的移动区间的头.尾指针来寻求答案,复杂度为 $O(n)$. 尺取法: #include <cstdio> #include <cstring> #include <algori…
题目传送门 /* 题意:求连续子序列的和不小于s的长度的最小值 尺取法:对数组保存一组下标(起点,终点),使用两端点得到答案 1. 记录前i项的总和,求[i, p)长度的最小值,用二分找到sum[p] - s[i] >= s的p 2. 除了O (nlogn)的方法,还可以在O (n)实现,[i, j)的区间求和,移动两端点,更新最小值,真的像尺取虫在爬:) */ #include <cstdio> #include <algorithm> #include <cstri…
public class EmployeeDemo { //方法一: public int search(String str,String strRes) {//查找字符串里与指定字符串相同的个数 int n=0;//计数器 // for(int i = 0;i<str.length();i++) { // // } while(str.indexOf(strRes)!=-1) { int i = str.indexOf(strRes); n++; str = str.substring(i+…
php实现 查找输入整数二进制中1的个数 一.总结 一句话总结: 1.if($j&intval($num)){}的作用是什么? 1 <?php 2 while($num=trim(fgets(STDIN))){ 3 $n=0; 4 for($i=0;$i<32;$i++){ 5 $j=1<<$i; 6 //echo $j.PHP_EOL; 7 if($j&intval($num)){ 8 $n++; 9 } 10 } 11 echo $n.PHP_EOL; 12 }…
package leetcode;import edu.princeton.cs.algs4.Cycle;import java.util.ArrayList;import java.util.Arrays;import java.util.LinkedList;import java.util.List;import java.util.function.Consumer;public class FirstDay { public static void main(String[] args…
最大几个数和最小几个数 import heapq a = [7, 5, 3, 4, 8, 6, 0] cc = heapq.nsmallest(2, a) #最小的两个数 dd = heapq.nlargest(3, a) #最大的三个数 print(cc) # [1, 2] print(dd) # [7, 6, 5] heapq.heapify(a) # 堆排序 heapq.heappop(a) # 弹出一个,堆数据结构最重要的特征是 heap[0] 永远是最小的元素 print(a) # 当…
seq命令Shell内建命令 seq命令用于产生从某个数到另外一个数之间的所有整数. 语法 : seq [选项]... 尾数 seq [选项]... 首数 尾数 seq [选项]... 首数 增量 尾数 选项 -f, --format=格式 使用printf 样式的浮点格式 -s, --separator=字符串 使用指定字符串分隔数字(默认使用:\n) -w, --equal-width 在列前添加0 使得宽度相同 实例: -f选项:指定格式 [root@cobbler6 ~]# 这样的话数字…
package com.lw.HomeWork1;//包名 2 import java.util.Scanner; public class Demo18 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); System.out.println("用哪个数循环?:");…
有2n+1个数,其中有2n个数出现过两次,找出其中只出现一次的数 例如这样一组数3,3,1,2,4,2,5,5,4,其中只有1出现了1次,其他都是出现了2次,如何找出其中的1? 最简便的方法是使用异或,代码如下: public class XOR { public static void main(String[] args){ ,,,,,,,,}; ;//初始值 ;i<arr.length;i++){ res ^=arr[i]; } System.out.println(res); } } 运…
题目:有一个函数int getNum(),每运行一次可以从一个数组V[N]里面取出一个数,N未知,当数取完的时候,函数返回NULL.现在要求写一个函数int get(),这个函数运行一次可以从V[N]里随机取出一个数,而这个数必须是符合1/N平均分布的,也就是说V[N]里面任意一个数都有1/N的机会被取出,要求空间复杂度为O(1). 解法:设置一个整形num,用来存放get()返回的数.调用getNum()从V[N]取数,按一定概率存入num中(覆盖以前的),直到getNum()返回NULL.设…
这周Java课程有个小作业:Java递归实现从n个数中选取m个数的所有组合 代码如下: //其中 n 取 1,2,3,4,5 五个数, m 取 3 package javaText; public class text { static int N = 5; static int M = 3; static int[] a= new int[]{1,2,3,4,5}; static int[] b = new int[M]; public static void main(String[] ar…
从n个数里面选择m个数 #include<iostream> #include<vector> using namespace std; vector<int> s; void dfs(int a[],int n,int m,int index,int nowk) { if(nowk==m){//当我挑选的数已经够得时候,就把它输出. ;i<;i++){ cout << s[i] << ' '; } cout << endl;…