A - Aggressive cows
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
* Lines 2..N+1: Line i+1 contains an integer stall location, xi
Output
Sample Input
5 3 1 2 8 4 9
Sample Output
3
Hint
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
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
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
#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
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
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
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
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
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 ≤ M ≤ N).
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
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
Sample Input
25 5 2 2 14 11 21 17
Sample Output
4
Hint