堆,二分,尺取

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).

 

转载于:https://www.cnblogs.com/lch316/p/6768118.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值