博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
堆,二分,尺取
阅读量:4353 次
发布时间:2019-06-07

本文共 11390 字,大约阅读时间需要 37 分钟。

                   

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 312849

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
=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
z) { y=z+(x-z+1)/2; if(pd(y)==1) { z=y; } else { x=y-1; } } printf("%d\n",z);}

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

210 155 1 3 5 10 7 4 9 2 85 111 2 3 4 5

Sample Output

23
#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
s) { b=b-a[q];q++;continue; }*/ if(b>=s&&q
=n&&b

                   

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

51 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
s; for(i=0;i
count; while(1) { while(h

                   

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

 

GETPUT msg1 10 5PUT msg2 10 4GETGETGET

 

Sample Output

 

EMPTY QUEUE!msg2 10msg1 10EMPTY QUEUE!
#include"stdio.h"#include"vector"#include
#include
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
q; // priority_ queue
.greater
>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); } }}

                   

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 3I 1I 2I 3QI 5QI 4Q

Sample Output

123

Hint

Xiao  Ming  won't  ask  Xiao  Bao  the  kth  great  number  when  the  number  of  the  written number is smaller than k. (1=
<=n<=1000000).
#include"stdio.h"#include"vector"#include"iostream"#include
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
q; for(i=0;i
>b; //scanf("%c",&b); if(b=='I') { scanf("%lld",&s); if(q.size()
q.top().a) { temp.a=s; q.pop();q.push(temp); } } } else { printf("%lld\n",q.top().a); } } } return 0;}

              

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

                   

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 2214112117

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

你可能感兴趣的文章
python类的特殊成员变量
查看>>
sublime text3最新版本注册码(build 3143)
查看>>
linux使用技巧
查看>>
必背公式及常数
查看>>
利用CSS、JavaScript及Ajax实现图片预加载的三大方法
查看>>
js时间戳转时间格式
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>
Linux的用户态和内核态
查看>>
JavaScript原生错误及检测
查看>>
(原创) cocos2d-x 3.0+ lua 学习和工作(4) : 公共函数(3): 深度克隆clone()
查看>>
为什么写作
查看>>
整数子数组求最大和添加验证
查看>>
使用kubeadm安装Kubernetes
查看>>
Principal Component Analysis 主元分析
查看>>
JDBC原生态代码
查看>>
韩版可爱小碎花创意家居收纳挂袋
查看>>
计算机基础之硬件
查看>>
python操作mysql ------- SqlAchemy正传
查看>>
如何使用 JSP JSTL 显示/制作树(tree) 菜单
查看>>
12.5号
查看>>