堆,二分,尺取

A - Aggressive cows

POJ - 2456

Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN (0 <= xi <= 1,000,000,000).

His C (2 <= C <= N) cows don‘t like this barn layout and become aggressive towards each other once put into a stall. To prevent the cows from hurting each other, FJ want to assign the cows to the stalls, such that the minimum distance between any two of them is as large as possible. What is the largest minimum distance?Input

* Line 1: Two space-separated integers: N and C

* Lines 2..N+1: Line i+1 contains an integer stall location, xi

Output

* Line 1: One integer: the largest minimum distance

Sample Input

5 3
1
2
8
4
9

Sample Output

3

Hint

OUTPUT DETAILS:

FJ can put his 3 cows in the stalls at positions 1, 4 and 8, resulting in a minimum distance of 3.

Huge input data,scanf is recommended.

#include"stdio.h"

#include"stdlib.h"

int a[100000],c,n;

int cmp(const void *a,const void *b)

{

    return *(int*)a-*(int*)b;

}

int pd(int y)

{

    int i,j=1,s;s=a[0];

    for(i=0;i<n;i++)

    {

        if(a[i]-s>=y)

        {

            j++;

            s=a[i];

        }

    }

    if(j>=c)

    return 1;

    return 0;

}

int main()

{

    extern int a[100000],c,n;

    int i,x,y,z;

    scanf("%d%d",&n,&c);

    for(i=0;i<n;i++)

    {

        scanf("%d",&a[i]);    

    }    

    qsort(a,n,4,cmp);

    x=a[n-1]-a[0];z=0;

    while(x>z)

    {

        y=z+(x-z+1)/2;

        if(pd(y)==1)

        {

            z=y;

        }

        else

        {

            x=y-1;

        }

    }

    printf("%d\n",z);

} 

B - Subsequence

POJ - 3061

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 of the sequence, the sum of which is greater than or equal to S.Input

The first line is the number of test cases. For each test case the program has to read the numbers N and S, separated by an interval, from the first line. The numbers of the sequence are given in the second line of the test case, separated by intervals. The input will finish with the end of file.

Output

For each the case the program has to print the result on separate line of the output file.if no answer, print 0.

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
#include"stdio.h"
int main()
{
    int i,j,q,n,m,s,l=0,a[100000],c,b=0;
    scanf("%d",&m);
    for(i=0;i<m;i++)
    {
        q=0;b=0;c=0;
        scanf("%d%d",&n,&s);
        for(j=0;j<n;j++)
        {
            scanf("%d",&a[j]);
        }
        for(j=0;j<=n;)
        {
            if(b<s&&j<n)
            {
                b=b+a[j];j++;continue;
            }
        /*    if(b>s)
            {
                b=b-a[q];q++;continue;
            }*/
            if(b>=s&&q<n)
            {
                if(c==0)
                {
                    c=j-q;b=b-a[q];q++;
                }
                else
                {
                    c=c<j-q?c:j-q;b=b-a[q];q++;//printf("c:%d b:%d j:%d q:%d",c,b,j,q);
                }
            }
            if(q==n)
            break;
            if(j>=n&&b<s)
            q++;
        }
        printf("%d\n",c);
    }
}

C - Jessica‘s Reading Problem

POJ - 3320

Jessica‘s a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she has spent little time on it. If she wants to pass it, she has to master all ideas included in a very thick text book. The author of that text book, like other authors, is extremely fussy about the ideas, thus some ideas are covered more than once. Jessica think if she managed to read each idea at least once, she can pass the exam. She decides to read only one contiguous part of the book which contains all ideas covered by the entire book. And of course, the sub-book should be as thin as possible.

A very hard-working boy had manually indexed for her each page of Jessica‘s text-book with what idea each page is about and thus made a big progress for his courtship. Here you come in to save your skin: given the index, help Jessica decide which contiguous part she should read. For convenience, each idea has been coded with an ID, which is a non-negative integer.

Input

The first line of input is an integer P (1 ≤ P ≤ 1000000), which is the number of pages of Jessica‘s text-book. The second line contains P non-negative integers describing what idea each page is about. The first integer is what the first page is about, the second integer is what the second page is about, and so on. You may assume all integers that appear can fit well in the signed 32-bit integer type.

Output

Output one line: the number of pages of the shortest contiguous part of the book which contains all ideals covered in the book.

Sample Input

5
1 8 8 8 1

Sample Output

2
#include"stdio.h"
#include"set"
#include"cstdio"
#include"map"
using namespace std;
int main()
{
    int h=0,l=0,p,i,n,num,res;

    int a[1000000];scanf("%d",&p);
    res=p;
    for(i=0;i<p;i++)
    {
        scanf("%d",&a[i]);
    }
    set<int>s;
    for(i=0;i<p;i++)
    {
        s.insert(a[i]);
    }
    n=s.size();
    map<int,int>count;
    while(1)
    {
        while(h<p&&num<n)
        {
            if(count[a[h++]]++==0)
            num++;
        }
        if(num<n)
        break;
        res=res<h-l?res:h-l;
        if(--count[a[l++]]==0)
        {
            num--;
        }
    }
    printf("%d",res);
}

D - Windows Message Queue

ZOJ - 2724

https://vjudge.net/problem/ZOJ-2724

Message queue is the basic fundamental of windows system. For each process, the system maintains a message queue. If something happens to this process, such as mouse click, text change, the system will add a message to the queue. Meanwhile, the process will do a loop for getting message from the queue according to the priority value if it is not empty. Note that the less priority value means the higher priority. In this problem, you are asked to simulate the message queue for putting messages to and getting message from the message queue.

Input

There‘s only one test case in the input. Each line is a command, "GET" or "PUT", which means getting message or putting message. If the command is "PUT", there‘re one string means the message name and two integer means the parameter and priority followed by. There will be at most 60000 command. Note that one message can appear twice or more and if two messages have the same priority, the one comes first will be processed first.(i.e., FIFO for the same priority.) Process to the end-of-file.

Output

For each "GET" command, output the command getting from the message queue with the name and parameter in one line. If there‘s no message in the queue, output "EMPTY QUEUE!". There‘s no output for "PUT" command.

Sample Input

GET
PUT msg1 10 5
PUT msg2 10 4
GET
GET
GET

Sample Output

EMPTY QUEUE!
msg2 10
msg1 10
EMPTY QUEUE!
#include"stdio.h"
#include"vector"
#include<cstdio>
#include<queue>
using namespace std;
struct dat
{
    char a[100];
    int m,n,i;
    friend bool operator<(const dat &a,const dat &b)
    {
        if(a.n==b.n)
        return a.i>b.i;
        return a.n>b.n;
    }
}temp;
int main()
{
    char b[10];
    int p=0;
    priority_queue<dat>q;
//    priority_ queue<dat,vector<dat.n>.greater<int> >q;
    while(scanf("%s",b)!=EOF)
    {
        if(b[0]==‘G‘)
        {
            if(!q.empty())
            {
                temp=q.top();
                q.pop();
                printf("%s %d\n",temp.a,temp.m);
            }
            else
            printf("EMPTY QUEUE!\n");
        }
        else
        {
            scanf("%s%d%d",temp.a,&temp.m,&temp.n);
            temp.i=++p;
            q.push(temp);
        }
    }
}

E - The kth great number

HDU - 4006

https://vjudge.net/problem/HDU-4006

Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round Xiao Ming can choose to write down a number, or ask Xiao Bao what the kth great number is. Because the number written by Xiao Ming is too much, Xiao Bao is feeling giddy. Now, try to help Xiao Bao.InputThere are several test cases. For each test case, the first line of input contains two positive integer n, k. Then n lines follow. If Xiao Ming choose to write down a number, there will be an " I" followed by a number that Xiao Ming will write down. If Xiao Ming choose to ask Xiao Bao, there will be a "Q", then you need to output the kth great number.
OutputThe output consists of one integer representing the largest number of islands that all lie on one line.
Sample Input

8 3
I 1
I 2
I 3
Q
I 5
Q
I 4
Q

Sample Output

1
2
3

Hint

Xiao  Ming  won‘t  ask  Xiao  Bao  the  kth  great  number  when  the  number  of  the  written number is smaller than k. (1=<k<=n<=1000000).
#include"stdio.h"
#include"vector"
#include"iostream"
#include<queue>
using namespace std;
struct dat
{
    long long int a;
    friend bool operator<(const dat &x,const dat &y)
    {
        return x.a>y.a;
    }
}temp;
int main()
{
    long long int s;
    int n,k,i;
    char b;

    while(scanf("%d%d",&n,&k)!=EOF)
    {
    priority_queue<dat>q;
    for(i=0;i<n;i++)
    {//getchar();
    cin>>b;    //scanf("%c",&b);
        if(b==‘I‘)
        {
            scanf("%lld",&s);
            if(q.size()<k)
            {
                temp.a=s;
                q.push(temp);
            }
            else
            {
                if(s>q.top().a)
                {
                    temp.a=s;

                    q.pop();q.push(temp);
                }
            }
        }
        else
        {
            printf("%lld\n",q.top().a);
        }
    }
    }
    return 0;
}

F - Tree

UVA - 548

https://vjudge.net/problem/UVA-548

You are to determine the value of the leaf node in a given binary tree that is the terminal node of a path of least value from the root of the binary tree to any leaf. The value of a path is the sum of values of nodes along that path. Input The input file will contain a description of the binary tree given as the inorder and postorder traversal sequences of that tree. Your program will read two line (until end of file) from the input file. The first line will contain the sequence of values associated with an inorder traversal of the tree and the second line will contain the sequence of values associated with a postorder traversal of the tree. All values will be different, greater than zero and less than 10000. You may assume that no binary tree will have more than 10000 nodes or less than 1 node. Output For each tree description you should output the value of the leaf node of a path of least value. In the case of multiple paths of least value you should pick the one with the least value on the terminal node. Sample Input 3 2 1 4 5 7 6 3 1 2 5 6 7 4 7 8 11 3 5 16 12 18 8 3 11 7 16 18 12 5 255 255 Sample Output 1 3 255

G - River Hopscotch

POJ - 3258

https://vjudge.net/problem/POJ-3258

Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end, L units away from the start (1 ≤ L ≤ 1,000,000,000). Along the river between the starting and ending rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral distance Di from the start (0 < Di < L).

To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river.

Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up to M rocks (0 ≤ MN).

FJ wants to know exactly how much he can increase the shortest distance *before* he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set of M rocks.

Input

Line 1: Three space-separated integers: L, N, and M
Lines 2.. N+1: Each line contains a single integer indicating how far some rock is away from the starting rock. No two rocks share the same position.

Output

Line 1: A single integer that is the maximum of the shortest distance a cow has to jump after removing M rocks

Sample Input

25 5 2
2
14
11
21
17

Sample Output

4

Hint

Before removing any rocks, the shortest jump was a jump of 2 from 0 (the start) to 2. After removing the rocks at 2 and 14, the shortest required jump is a jump of 4 (from 17 to 21 or from 21 to 25).

时间: 2024-10-15 03:14:21

堆,二分,尺取的相关文章

Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 【二分 + 尺取】

任意门:http://codeforces.com/contest/1073/problem/C C. Vasya and Robot time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Vasya has got a robot which is situated on an infinite Cartesian plane, i

hdu 6231 -- K-th Number(二分+尺取)

题目链接 Problem Description Alice are given an array A[1..N] with N numbers. Now Alice want to build an array B by a parameter K as following rules: Initially, the array B is empty. Consider each interval in array A. If the length of this interval is le

CodeForces - 237C Primes on Interval(二分+尺取)

You've decided to carry out a survey in the theory of prime numbers. Let us remind you that a prime number is a positive integer that has exactly two distinct positive integer divisors. Consider positive integers a, a?+?1, ..., b (a?≤?b). You want to

【二分+尺取】HDU 6119 小小粉丝度度熊

http://acm.hdu.edu.cn/showproblem.php?pid=6119 [思路] 首先通过处理交叉的可以处理成不交叉的 然后二分查找答案 如何判断一个长度是否可行? 双指针O(n)扫一次就可以,要处理区间和问题,所以先求前缀和 时间复杂度O(nlogn) [AC] 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #inclu

UVALive - 2678 二分/尺取

题意:求最小的长度L满足该长度上的元素和大于等于S 最近dp做多了总有一种能用dp解决一切的错觉 二分长度解决 #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<string> #include<vector> #include&

POJ3061 Subsequence 尺取or二分

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

POJ-3061 Subsequence 二分或尺取

题面 题意:给你一个长度为n(n<100000)的数组,让你找到一个最短的连续子序列,使得子序列的和>=m  (m<1e9) 题解: 1 显然我们我们可以二分答案,然后利用前缀和判断是否可行,这样是O(nlgn)的   注意没有答案 ans输出0 1 #include<cstdio> 2 #include<cstdlib> 3 #include<iostream> 4 #include<cstring> 5 using namespace

HDU 5178 pairs【二分】||【尺取】

<题目链接> 题目大意: 给定一个整数序列,求出绝对值小于等于k的有序对个数. 解题分析: $O(nlong(n))$的二分很好写,这里就不解释了.本题尺取$O(n)$也能做,并且效率很不错. 尺取: #include <bits/stdc++.h> using namespace std; int arr[int(1e5+5)]; int main(){ int T,n,k;scanf("%d",&T); while(T--){ scanf("

POJ 3061 Subsequence ( 二分 || 尺取法 )

题意 : 找出给定序列长度最小的子序列,子序列的和要求满足大于或者等于 S,如果存在则输出最小长度.否则输出 0(序列的元素都是大于 0 小于10000) 分析 : 有关子序列和的问题,都可以考虑采用先构造前缀和的方式来进行接下来的操作 ( 任意子序列的和都能由某两个前缀和的差表示 ). 二分做法 ==> 我们枚举起点,对于每一个起点 St 二分查找看看 St 后面有没有前缀和是大于或者等于 [ St的前缀和 ] + S 的,如果有说明从当前起点开始有一个终点使得起终之和是大于或者等于 S 的,

Codeforces Round #116 (Div. 2, ACM-ICPC Rules) E. Cubes (尺取)

题目链接:http://codeforces.com/problemset/problem/180/E 给你n个数,每个数代表一种颜色,给你1到m的m种颜色.最多可以删k个数,问你最长连续相同颜色的序列的长度是多少. 将相同颜色的下标存到对应颜色的容器中,比如ans[a[i]].push_back(i)就是将下标为i颜色为a[i]的数存到ans[a[i]]容器中. 对于每种颜色序列,尺取一下 在差距小于k的情况下取能取到的最大长度. 1 //#pragma comment(linker, "/S