本文主要是介绍cf----2019-11-20( Anastasia and pebbles,Mice problem,Oleg and shares),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
思绪不断阻挡着回忆播放,盲目的追寻仍然空空荡荡,灰蒙蒙的夜晚睡意又不知躲到哪去,一转身孤单已躺在身旁。
Anastasia loves going for a walk in Central Uzhlyandian Park. But she became uninterested in simple walking, so she began to collect Uzhlyandian pebbles. At first, she decided to collect all the pebbles she could find in the park.
She has only two pockets. She can put at most k pebbles in each pocket at the same time. There are n different pebble types in the park, and there are wi pebbles of the i-th type. Anastasia is very responsible, so she never mixes pebbles of different types in same pocket. However, she can put different kinds of pebbles in different pockets at the same time. Unfortunately, she can't spend all her time collecting pebbles, so she can collect pebbles from the park only once a day.
Help her to find the minimum number of days needed to collect all the pebbles of Uzhlyandian Central Park, taking into consideration that Anastasia can't place pebbles of different types in same pocket.
Input
The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 109) — the number of different pebble types and number of pebbles Anastasia can place in one pocket.
The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 104) — number of pebbles of each type.
Output
The only line of output contains one integer — the minimum number of days Anastasia needs to collect all the pebbles.
Examples
input
Copy
3 2 2 3 4output
Copy
3input
Copy
5 4 3 1 8 9 7output
Copy
5Note
In the first sample case, Anastasia can collect all pebbles of the first type on the first day, of second type — on the second day, and of third type — on the third day.
Optimal sequence of actions in the second sample case:
- In the first day Anastasia collects 8 pebbles of the third type.
- In the second day she collects 8 pebbles of the fourth type.
- In the third day she collects 3 pebbles of the first type and 1 pebble of the fourth type.
- In the fourth day she collects 7 pebbles of the fifth type.
- In the fifth day she collects 1 pebble of the second type.
#include <iostream> #include <cstdio> #include <algorithm> #include <string> #include <cstring> #include <cstdlib> #include <cmath> #include <stack> #include <queue> #include <set> #include <map> #include <vector> #include <ctime> #include <cctype> #include <bitset> #include <utility> #include <sstream> #include <complex> #include <iomanip> #define inf 0x3f3f3f3f typedef long long ll; using namespace std; ll n,k,x; int main() {while(cin>>n>>k){ll sum=0;for(int i=0;i<n;i++){cin>>x;sum+=(x+k-1)/k;}cout<<(sum+1)/2<<endl;}return 0; }
Igor the analyst fell asleep on the work and had a strange dream. In the dream his desk was crowded with computer mice, so he bought a mousetrap to catch them.
The desk can be considered as an infinite plane, then the mousetrap is a rectangle which sides are parallel to the axes, and which opposite sides are located in points (x1, y1) and (x2, y2).
Igor wants to catch all mice. Igor has analysed their behavior and discovered that each mouse is moving along a straight line with constant speed, the speed of the i-th mouse is equal to (vix, viy), that means that the x coordinate of the mouse increases by vix units per second, while the y coordinates increases by viy units. The mousetrap is open initially so that the mice are able to move freely on the desk. Igor can close the mousetrap at any moment catching all the mice that are strictly inside the mousetrap.
Igor works a lot, so he is busy in the dream as well, and he asks you to write a program that by given mousetrap's coordinates, the initial coordinates of the mice and their speeds determines the earliest time moment in which he is able to catch all the mice. Please note that Igor can close the mousetrap only once.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the number of computer mice on the desk.
The second line contains four integers x1, y1, x2 and y2 (0 ≤ x1 ≤ x2 ≤ 100 000), (0 ≤ y1 ≤ y2 ≤ 100 000) — the coordinates of the opposite corners of the mousetrap.
The next n lines contain the information about mice.
The i-th of these lines contains four integers rix, riy, vix and viy, (0 ≤ rix, riy ≤ 100 000, - 100 000 ≤ vix, viy ≤ 100 000), where (rix, riy) is the initial position of the mouse, and (vix, viy) is its speed.
Output
In the only line print minimum possible non-negative number t such that if Igor closes the mousetrap at t seconds from the beginning, then all the mice are strictly inside the mousetrap. If there is no such t, print -1.
Your answer is considered correct if its absolute or relative error doesn't exceed 10 - 6.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .
Examples
input
Copy
4 7 7 9 8 3 5 7 5 7 5 2 4 3 3 7 8 6 6 3 2output
Copy
0.57142857142857139685input
Copy
4 7 7 9 8 0 3 -5 4 5 0 5 4 9 9 -1 -6 10 5 -7 -10output
Copy
-1Note
Here is a picture of the first sample
Points A, B, C, D - start mice positions, segments are their paths.
Then, at first time when all mice will be in rectangle it will be looks like this:
Here is a picture of the second sample
#include <iostream> #include <cstdio> #include <algorithm> #include <string> #include <cstring> #include <cstdlib> #include <cmath> #include <stack> #include <queue> #include <set> #include <map> #include <vector> #include <ctime> #include <cctype> #include <bitset> #include <utility> #include <sstream> #include <complex> #include <iomanip> #define inf 0x3f3f3f3f typedef long long ll; using namespace std; int n,x1,y1,x2,y2; double l=0,r=1e11; void gx(int dd,int L,int R,int v) {if(v==0){if(dd>L&&dd<R)return;r=-1;}double t1=(double)(L-dd)/v,t2=(double)(R-dd)/v;if(t1>t2)swap(t1,t2);l=max(l,t1);r=min(r,t2); }int main() {scanf("%d",&n);scanf("%d%d%d%d",&x1,&y1,&x2,&y2);for(int i=1; i<=n; i++){int x,y,xx,yy;scanf("%d%d%d%d",&x,&y,&xx,&yy);gx(x,x1,x2,xx);gx(y,y1,y2,yy);}if(r-l>=1e-11&&r>0)printf("%.20f\n",l);elseprintf("-1\n");return 0; }
Oleg the bank client checks share prices every day. There are n share prices he is interested in. Today he observed that each second exactly one of these prices decreases by k rubles (note that each second exactly one price changes, but at different seconds different prices can change). Prices can become negative. Oleg found this process interesting, and he asked Igor the financial analyst, what is the minimum time needed for all n prices to become equal, or it is impossible at all? Igor is busy right now, so he asked you to help Oleg. Can you answer this question?
Input
The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 109) — the number of share prices, and the amount of rubles some price decreases each second.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the initial prices.
Output
Print the only line containing the minimum number of seconds needed for prices to become equal, of «-1» if it is impossible.
Examples
input
Copy
3 3 12 9 15output
Copy
3input
Copy
2 2 10 9output
Copy
-1input
Copy
4 1 1 1000000000 1000000000 1000000000output
Copy
2999999997Note
Consider the first example.
Suppose the third price decreases in the first second and become equal 12 rubles, then the first price decreases and becomes equal 9 rubles, and in the third second the third price decreases again and becomes equal 9 rubles. In this case all prices become equal 9 rubles in 3 seconds.
There could be other possibilities, but this minimizes the time needed for all prices to become equal. Thus the answer is 3.
In the second example we can notice that parity of first and second price is different and never changes within described process. Thus prices never can become equal.
In the third example following scenario can take place: firstly, the second price drops, then the third price, and then fourth price. It happens 999999999 times, and, since in one second only one price can drop, the whole process takes 999999999 * 3 = 2999999997 seconds. We can note that this is the minimum possible time.
#include <iostream> #include <cstdio> #include <algorithm> #include <string> #include <cstring> #include <cstdlib> #include <cmath> #include <stack> #include <queue> #include <set> #include <map> #include <vector> #include <ctime> #include <cctype> #include <bitset> #include <utility> #include <sstream> #include <complex> #include <iomanip> #define inf 0x3f3f3f3f typedef long long ll; using namespace std; ll n, k; vector<ll> jg; int main() {while (cin >> n >> k){ll ans = 0;ll mi = INT_MAX;jg.clear();for (ll i = 0; i < n; i++){ll ls;cin >> ls;jg.push_back(ls);if (ls < mi)mi = ls;}for (int i = 0; i < n; i++){if ((jg[i] - mi) % k){ans = -1;break;}elseans += ((jg[i] - mi) / k);}cout << ans << endl;}return 0; }
这篇关于cf----2019-11-20( Anastasia and pebbles,Mice problem,Oleg and shares)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!