标签:acm算法
All cities of Lineland are located on the Ox coordinate axis. Thus, each city is associated with its position xi — a coordinate on the Ox axis. No two cities are located at a single point.
Lineland residents love to send letters to each other. A person may send a letter only if the recipient lives in another city (because if they live in the same city, then it is easier to drop in).
Strange but true, the cost of sending the letter is exactly equal to the distance between the sender‘s city and the recipient‘s city.
For each city calculate two values ??mini and maxi, where mini is the minimum cost of sending a letter from the i-th city to some other city, and maxi is the the maximum cost of sending a letter from the i-th city to some other city
The first line of the input contains integer n (2?≤?n?≤?105) — the number of cities in Lineland. The second line contains the sequence of n distinct integers x1,?x2,?...,?xn (?-?109?≤?xi?≤?109), where xi is the x-coordinate of the i-th city. All the xi‘s are distinct and follow in ascending order.
Print n lines, the i-th line must contain two integers mini,?maxi, separated by a space, where mini is the minimum cost of sending a letter from the i-th city, and maxi is the maximum cost of sending a letter from the i-th city.
Sample test(s)
Input
4-5 -2 2 7
Output
3 123 94 75 12
Input
2-1 1
Output
2 22 2
#include<iostream> #include<cstdio> #include<cstring> #include<math.h> #include<algorithm> using namespace std; #define N 500005 typedef long long ll; #include <queue> #include <set> #include <map> #include <stack> int main() { int n,i; while(scanf("%d",&n)!=EOF) { int a[200005]; memset(a,0,sizeof(a)); for(i=0; i<n; i++) { scanf("%d",&a[i]); } sort(a,a+n); ll min,max; if(n==2) { printf("%d %d\n",abs(a[0]-a[1]),abs(a[0]-a[1])); printf("%d %d\n",abs(a[0]-a[1]),abs(a[0]-a[1])); continue; } for(i=0; i<n; i++) { if(i==0) { if(abs(a[n-1]-a[0])>=abs(a[0]-a[1])) { max=abs(a[n-1]-a[0]); min=abs(a[0]-a[1]); } else { min=abs(a[0]-a[1]); max=abs(a[0]-a[1]); } } else if(i==n-1) { if(abs(a[i]-a[n-2])>=abs(a[i]-a[0])) { max=abs(a[i]-a[n-2]); min=abs(a[i]-a[n-2]); } else { min=abs(a[i]-a[n-2]); max=abs(a[i]-a[0]); } } else { if(abs(a[i]-a[0])>=abs(a[i]-a[n-1])) { max=abs(a[i]-a[0]); } else { max=abs(a[i]-a[n-1]); } if(abs(a[i]-a[i+1])>=abs(a[i]-a[i-1])) { min=abs(a[i]-a[i-1]); } else min=abs(a[i]-a[i+1]); } printf("%lld %lld\n",min,max); } } return 0; }
Berland National Library has recently been built in the capital of Berland. In addition, in the library you can take any of the collected works of Berland leaders, the library has a reading room.
Today was the pilot launch of an automated reading room visitors‘ accounting system! The scanner of the system is installed at the entrance to the reading room. It records the events of the form "reader entered room", "reader left room". Every reader is assigned a registration number during the registration procedure at the library — it‘s a unique integer from 1 to 106. Thus, the system logs events of two forms:
The first launch of the system was a success, it functioned for some period of time, and, at the time of its launch and at the time of its shutdown, the reading room may already have visitors.
Significant funds of the budget of Berland have been spent on the design and installation of the system. Therefore, some of the citizens of the capital now demand to explain the need for this system and the benefits that its implementation will bring. Now, the developers of the system need to urgently come up with reasons for its existence.
Help the system developers to find the minimum possible capacity of the reading room (in visitors) using the log of the system available to you.
The first line contains a positive integer n (1?≤?n?≤?100) — the number of records in the system log. Next follow n events from the system journal in the order in which the were made. Each event was written on a single line and looks as "+ ri" or "- ri", where ri is an integer from 1 to 106, the registration number of the visitor (that is, distinct visitors always have distinct registration numbers).
It is guaranteed that the log is not contradictory, that is, for every visitor the types of any of his two consecutive events are distinct. Before starting the system, and after stopping the room may possibly contain visitors.
Print a single integer — the minimum possible capacity of the reading room.
6 + 12001 - 12001 - 1 - 1200 + 1 + 7
3
2 - 1 - 2
2
2 + 1 - 1
1
In the first sample test, the system log will ensure that at some point in the reading room were visitors with registration numbers 1, 1200 and 12001. More people were not in the room at the same time based on the log. Therefore, the answer to the test is 3.
#include<iostream> #include<cstdio> #include<cstring> #include<math.h> #include<algorithm> using namespace std; #define N 500005 typedef long long ll; #include <queue> #include <set> #include <map> #include <stack> set<int>q; set<int>::iterator it,itt; int main() { int n; while(scanf("%d",&n)!=EOF) { char s[101][2]; int x; int ans=0; for(int i=0; i<n; i++) { cin>>s[i]>>x; if(s[i][0]=='-') { if(q.count(x)) q.erase(x); else ans++; } else { q.insert(x); int ans1=q.size(); ans=max(ans,ans1); } } cout<<ans<<endl; } return 0; }
Polycarp loves geometric progressions very much. Since he was only three years old, he loves only the progressions of length three. He also has a favorite integer k and a sequence a, consisting of n integers.
He wants to know how many subsequences of length three can be selected from a, so that they form a geometric progression with common ratio k.
A subsequence of length three is a combination of three such indexes i1,?i2,?i3, that 1?≤?i1?<?i2?<?i3?≤?n. That is, a subsequence of length three are such groups of three elements that are not necessarily consecutive in the sequence, but their indexes are strictly increasing.
A geometric progression with common ratio k is a sequence of numbers of the form b·k0,?b·k1,?...,?b·kr?-?1.
Polycarp is only three years old, so he can not calculate this number himself. Help him to do it.
The first line of the input contains two integers, n and k (1?≤?n,?k?≤?2·105), showing how many numbers Polycarp‘s sequence has and his favorite number.
The second line contains n integers a1,?a2,?...,?an (?-?109?≤?ai?≤?109) — elements of the sequence.
Output a single number — the number of ways to choose a subsequence of length three, such that it forms a geometric progression with a common ratio k.
5 2 1 1 2 2 4
4
3 1 1 1 1
1
10 3 1 2 6 2 3 6 9 18 3 9
6
In the first sample test the answer is four, as any of the two 1s can be chosen as the first element, the second element can be any of the 2s, and the third element of the subsequence must be equal to 4.
#include <cmath> #include <cstring> #include <string> #include <algorithm> #include <iostream> #include <queue> #include <map> #include <cstdio> #include <set> #include <vector> using namespace std; __int64 n,k; __int64 a[200005]; __int64 b[200005]; //记录i位置的数对应 有多少个 a[i]/k 存在 map <int ,__int64 > c; int main() { __int64 i; scanf("%I64d%I64d",&n,&k); for (i=1; i<=n; i++) { scanf("%I64d",&a[i]); if (a[i]%k==0) b[i]=c[a[i]/k]; //记录当前位置 拥有多少个 a[i]/k (等比的前一项) c[a[i]]++; //每次把a[i]个数加一 } // for(i=1; i<=n; i++) // cout<<b[i]<<endl; c.clear(); __int64 ans=0; for (i=1; i<=n; i++) { if (a[i]%k==0) ans+=c[a[i]/k]; //a[i]为第三项,a[i]/k为第二项,c[a[i]/k]存的是(每一个(第二项对应的第一项)的个数)之和 c[a[i]]+=b[i]; //c[a[i]] 存的是 数a[i] 对应的 a[i]/k 的个数 } printf("%I64d\n",ans); return 0; }
Alice and Bob love playing one-dimensional battle ships. They play on the field in the form of a line consisting of n square cells (that is, on a 1?×?n table).
At the beginning of the game Alice puts k ships on the field without telling their positions to Bob. Each ship looks as a 1?×?a rectangle (that is, it occupies a sequence of a consecutive squares of the field). The ships cannot intersect and even touch each other.
After that Bob makes a sequence of "shots". He names cells of the field and Alice either says that the cell is empty ("miss"), or that the cell belongs to some ship ("hit").
But here‘s the problem! Alice like to cheat. May be that is why she responds to each Bob‘s move with a "miss".
Help Bob catch Alice cheating — find Bob‘s first move, such that after it you can be sure that Alice cheated.
The first line of the input contains three integers: n, k and a (1?≤?n,?k,?a?≤?2·105) — the size of the field, the number of the ships and the size of each ship. It is guaranteed that the n, k and a are such that you can put k ships of size a on the field, so that no two ships intersect or touch each other.
The second line contains integer m (1?≤?m?≤?n) — the number of Bob‘s moves.
The third line contains m distinct integers x1,?x2,?...,?xm, where xi is the number of the cell where Bob made the i-th shot. The cells are numbered from left to right from 1 to n.
Print a single integer — the number of such Bob‘s first move, after which you can be sure that Alice lied. Bob‘s moves are numbered from 1 to m in the order the were made. If the sought move doesn‘t exist, then print "-1".
11 3 3 5 4 8 6 1 11
3
5 1 3 2 1 5
-1
5 1 3 1 3
1
#include<cstdlib> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> #include<cstdio> #define maxn 1100000 using namespace std; int bb[maxn],a[maxn],n,m,k,aa; bool check(int x) { int size=0,ans=0; for(int i=1; i<=n; i++) { size++; if(bb[i]<=x&&bb[i])size=0; if(size>=aa) { size=-1; ans++; } } if(ans>=k) return 1; else return 0; } int main() { scanf("%d%d%d%d",&n,&k,&aa,&m); for(int i=1; i<=m; i++) { scanf("%d",&a[i]); bb[a[i]]=i; } int l=1,r=m; while(l<=r) { int mid=(l+r)>>1; if(check(mid))l=mid+1; else r=mid-1; } if(l<=m)printf("%d\n",l); else puts("-1"); return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
Codeforces 567A 567B 567C 567D
标签:acm算法
原文地址:http://blog.csdn.net/sky_miange/article/details/47340569