Codeforces Round #377 (Div. 2) A B C D 水/贪心/贪心/二分

A. Buy a Shovel
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarp urgently needs a shovel! He comes to the shop and chooses an appropriate one. The shovel that Policarp chooses is sold for k burles. Assume that there is an unlimited number of such shovels in the shop.

In his pocket Polycarp has an unlimited number of "10-burle coins" and exactly one coin of r burles (1 ≤ r ≤ 9).

What is the minimum number of shovels Polycarp has to buy so that he can pay for the purchase without any change? It is obvious that he can pay for 10 shovels without any change (by paying the requied amount of 10-burle coins and not using the coin of r burles). But perhaps he can buy fewer shovels and pay without any change. Note that Polycarp should buy at least one shovel.

Input

The single line of input contains two integers k and r (1 ≤ k ≤ 1000, 1 ≤ r ≤ 9) — the price of one shovel and the denomination of the coin in Polycarp's pocket that is different from "10-burle coins".

Remember that he has an unlimited number of coins in the denomination of 10, that is, Polycarp has enough money to buy any number of shovels.

Output

Print the required minimum number of shovels Polycarp has to buy so that he can pay for them without any change.

Examples
Input
117 3
Output
9
Input
237 7
Output
1
Input
15 2
Output
2
Note

In the first example Polycarp can buy 9 shovels and pay 9·117 = 1053 burles. Indeed, he can pay this sum by using 10-burle coins and one 3-burle coin. He can't buy fewer shovels without any change.

In the second example it is enough for Polycarp to buy one shovel.

In the third example Polycarp should buy two shovels and pay 2·15 = 30 burles. It is obvious that he can pay this sum without any change.

题意:

题解:

/******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^    ^ ^
 O      O
******************************/
#include<bits/stdc++.h>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
#define  A first
#define B second
const int mod=1000000007;
const int MOD1=1000000007;
const int MOD2=1000000009;
const double EPS=0.00000001;
//typedef long long ll;
typedef __int64 ll;
const ll MOD=1000000007;
const int INF=1000000010;
const ll MAX=1ll<<55;
const double eps=1e-5;
const double inf=~0u>>1;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
int k,r;
int main()
{
    scanf("%d %d",&k,&r);
        k=k%10;
       int ans=1000000000;
    for(int i=0;i<=10000;i++)
    {
        if(((i*k)%10)==r)
        {
            ans=i;
            break;
        }
    }
    for(int i=1;i<=10000;i++)
    {
        if((i*k)%10==0)
        {
            ans=min(ans,i);
            break;
        }
    }
    cout<<ans<<endl;
    return 0;
}
B. Cormen — The Best Friend Of a Man
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Recently a dog was bought for Polycarp. The dog's name is Cormen. Now Polycarp has a lot of troubles. For example, Cormen likes going for a walk.

Empirically Polycarp learned that the dog needs at least k walks for any two consecutive days in order to feel good. For example, if k = 5 and yesterday Polycarp went for a walk with Cormen 2 times, today he has to go for a walk at least 3 times.

Polycarp analysed all his affairs over the next n days and made a sequence of n integers a1, a2, ..., an, where ai is the number of times Polycarp will walk with the dog on the i-th day while doing all his affairs (for example, he has to go to a shop, throw out the trash, etc.).

Help Polycarp determine the minimum number of walks he needs to do additionaly in the next n days so that Cormen will feel good during all the n days. You can assume that on the day before the first day and on the day after the n-th day Polycarp will go for a walk with Cormen exactly k times.

Write a program that will find the minumum number of additional walks and the appropriate schedule — the sequence of integers b1, b2, ..., bn (bi ≥ ai), where bi means the total number of walks with the dog on the i-th day.

Input

The first line contains two integers n and k (1 ≤ n, k ≤ 500) — the number of days and the minimum number of walks with Cormen for any two consecutive days.

The second line contains integers a1, a2, ..., an (0 ≤ ai ≤ 500) — the number of walks with Cormen on the i-th day which Polycarp has already planned.

Output

In the first line print the smallest number of additional walks that Polycarp should do during the next n days so that Cormen will feel good during all days.

In the second line print n integers b1, b2, ..., bn, where bi — the total number of walks on the i-th day according to the found solutions (ai ≤ bi for all i from 1 to n). If there are multiple solutions, print any of them.

Examples
Input
3 5
2 0 1
Output
4
2 3 2
Input
3 1
0 0 0
Output
1
0 1 0
Input
4 6
2 4 3 5
Output
0
2 4 3 5

 题意:

 题解:

 1 /******************************
 2 code by drizzle
 3 blog: www.cnblogs.com/hsd-/
 4 ^ ^    ^ ^
 5  O      O
 6 ******************************/
 7 #include<bits/stdc++.h>
 8 #include<map>
 9 #include<set>
10 #include<cmath>
11 #include<queue>
12 #include<bitset>
13 #include<math.h>
14 #include<vector>
15 #include<string>
16 #include<stdio.h>
17 #include<cstring>
18 #include<iostream>
19 #include<algorithm>
20 #pragma comment(linker, "/STACK:102400000,102400000")
21 using namespace std;
22 #define  A first
23 #define B second
24 const int mod=1000000007;
25 const int MOD1=1000000007;
26 const int MOD2=1000000009;
27 const double EPS=0.00000001;
28 //typedef long long ll;
29 typedef __int64 ll;
30 const ll MOD=1000000007;
31 const int INF=1000000010;
32 const ll MAX=1ll<<55;
33 const double eps=1e-5;
34 const double inf=~0u>>1;
35 const double pi=acos(-1.0);
36 typedef double db;
37 typedef unsigned int uint;
38 typedef unsigned long long ull;
39 int n,k;
40 int a[505];
41 int main()
42 {
43     scanf("%d %d",&n,&k);
44     for(int i=1; i<=n; i++)
45         scanf("%d",&a[i]);
46     int sum=0;
47     for(int i=1; i<n; i++)
48     {
49         if(a[i]+a[i+1]<k)
50         {
51             sum=sum+k-a[i]-a[i+1];
52             a[i+1]=a[i+1]+k-a[i]-a[i+1];
53 
54         }
55     }
56     cout<<sum<<endl;
57     for(int i=1; i<=n; i++)
58         cout<<a[i]<<" ";
59     cout<<endl;
60     return 0;
61 }
C. Sanatorium
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasiliy spent his vacation in a sanatorium, came back and found that he completely forgot details of his vacation!

Every day there was a breakfast, a dinner and a supper in a dining room of the sanatorium (of course, in this order). The only thing that Vasiliy has now is a card from the dining room contaning notes how many times he had a breakfast, a dinner and a supper (thus, the card contains three integers). Vasiliy could sometimes have missed some meal, for example, he could have had a breakfast and a supper, but a dinner, or, probably, at some days he haven't been at the dining room at all.

Vasiliy doesn't remember what was the time of the day when he arrived to sanatorium (before breakfast, before dinner, before supper or after supper), and the time when he left it (before breakfast, before dinner, before supper or after supper). So he considers any of these options. After Vasiliy arrived to the sanatorium, he was there all the time until he left. Please note, that it's possible that Vasiliy left the sanatorium on the same day he arrived.

According to the notes in the card, help Vasiliy determine the minimum number of meals in the dining room that he could have missed. We shouldn't count as missed meals on the arrival day before Vasiliy's arrival and meals on the departure day after he left.

Input

The only line contains three integers b, d and s (0 ≤ b, d, s ≤ 1018,  b + d + s ≥ 1) — the number of breakfasts, dinners and suppers which Vasiliy had during his vacation in the sanatorium.

Output

Print single integer — the minimum possible number of meals which Vasiliy could have missed during his vacation.

Examples
Input
3 2 1
Output
1

Input
1 0 0
Output
0

Input
1 1 1
Output
0

Input
1000000000000000000 0 1000000000000000000
Output
999999999999999999


Note

In the first sample, Vasiliy could have missed one supper, for example, in case he have arrived before breakfast, have been in the sanatorium for two days (including the day of arrival) and then have left after breakfast on the third day.

In the second sample, Vasiliy could have arrived before breakfast, have had it, and immediately have left the sanatorium, not missing any meal.

In the third sample, Vasiliy could have been in the sanatorium for one day, not missing any meal.

题意:

题解:

 1 /******************************
 2 code by drizzle
 3 blog: www.cnblogs.com/hsd-/
 4 ^ ^    ^ ^
 5  O      O
 6 ******************************/
 7 #include<bits/stdc++.h>
 8 #include<map>
 9 #include<set>
10 #include<cmath>
11 #include<queue>
12 #include<bitset>
13 #include<math.h>
14 #include<vector>
15 #include<string>
16 #include<stdio.h>
17 #include<cstring>
18 #include<iostream>
19 #include<algorithm>
20 #pragma comment(linker, "/STACK:102400000,102400000")
21 using namespace std;
22 #define  A first
23 #define B second
24 const int mod=1000000007;
25 const int MOD1=1000000007;
26 const int MOD2=1000000009;
27 const double EPS=0.00000001;
28 //typedef long long ll;
29 typedef __int64 ll;
30 const ll MOD=1000000007;
31 const int INF=1000000010;
32 const ll MAX=1ll<<55;
33 const double eps=1e-5;
34 const double inf=~0u>>1;
35 const double pi=acos(-1.0);
36 typedef double db;
37 typedef unsigned int uint;
38 typedef unsigned long long ull;
39 ll a[4];
40 int main()
41 {
42     scanf("%I64d %I64d %I64d",&a[0],&a[1],&a[2]);
43     sort(a,a+3);
44     ll x=0;
45     printf("%I64d
",max(x,a[2]-a[0]-1)+max(x,a[2]-a[1]-1));
46     return 0;
47 }
D. Exams
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasiliy has an exam period which will continue for n days. He has to pass exams on m subjects. Subjects are numbered from 1 to m.

About every day we know exam for which one of m subjects can be passed on that day. Perhaps, some day you can't pass any exam. It is not allowed to pass more than one exam on any day.

On each day Vasiliy can either pass the exam of that day (it takes the whole day) or prepare all day for some exam or have a rest.

About each subject Vasiliy know a number ai — the number of days he should prepare to pass the exam number i. Vasiliy can switch subjects while preparing for exams, it is not necessary to prepare continuously during ai days for the exam number i. He can mix the order of preparation for exams in any way.

Your task is to determine the minimum number of days in which Vasiliy can pass all exams, or determine that it is impossible. Each exam should be passed exactly one time.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 105) — the number of days in the exam period and the number of subjects.

The second line contains n integers d1, d2, ..., dn (0 ≤ di ≤ m), where di is the number of subject, the exam of which can be passed on the day number i. If di equals 0, it is not allowed to pass any exams on the day number i.

The third line contains m positive integers a1, a2, ..., am (1 ≤ ai ≤ 105), where ai is the number of days that are needed to prepare before passing the exam on the subject i.

Output

Print one integer — the minimum number of days in which Vasiliy can pass all exams. If it is impossible, print -1.

Examples
Input
7 2
0 1 0 2 1 0 2
2 1
Output
5
Input
10 3
0 0 1 2 3 0 2 0 1 2
1 1 4
Output
9
Input
5 1
1 1 1 1 1
5
Output
-1
Note

In the first example Vasiliy can behave as follows. On the first and the second day he can prepare for the exam number 1 and pass it on the fifth day, prepare for the exam number 2 on the third day and pass it on the fourth day.

In the second example Vasiliy should prepare for the exam number 3 during the first four days and pass it on the fifth day. Then on the sixth day he should prepare for the exam number 2 and then pass it on the seventh day. After that he needs to prepare for the exam number 1 on the eighth day and pass it on the ninth day.

In the third example Vasiliy can't pass the only exam because he hasn't anough time to prepare for it.

题意:

题解:

 1 /******************************
 2 code by drizzle
 3 blog: www.cnblogs.com/hsd-/
 4 ^ ^    ^ ^
 5  O      O
 6 ******************************/
 7 #include<bits/stdc++.h>
 8 #include<map>
 9 #include<set>
10 #include<cmath>
11 #include<queue>
12 #include<bitset>
13 #include<math.h>
14 #include<vector>
15 #include<string>
16 #include<stdio.h>
17 #include<cstring>
18 #include<iostream>
19 #include<algorithm>
20 #pragma comment(linker, "/STACK:102400000,102400000")
21 using namespace std;
22 #define  A first
23 #define B second
24 const int mod=1000000007;
25 const int MOD1=1000000007;
26 const int MOD2=1000000009;
27 const double EPS=0.00000001;
28 //typedef long long ll;
29 typedef __int64 ll;
30 const ll MOD=1000000007;
31 const int INF=1000000010;
32 const ll MAX=1ll<<55;
33 const double eps=1e-5;
34 const double inf=~0u>>1;
35 const double pi=acos(-1.0);
36 typedef double db;
37 typedef unsigned int uint;
38 typedef unsigned long long ull;
39 int gg[100010],he[100010],kk[100010],ha[100010];
40 int n,m;
41 bool  fun(int x)
42 {
43     ll mm,oo;
44     memset(ha,0,sizeof(ha));
45     memset(kk,0,sizeof(kk));
46     for(int i=1;i<=x;i++){
47         ha[gg[i]]=i;
48     }
49     oo=0;
50     for(int i=1;i<=m;i++){
51         kk[ha[i]]=he[i]+1;
52         oo+=he[i];
53     }
54     mm=0;
55     for(int i=1;i<=x;i++){
56         mm+=kk[i];
57         if(mm>i){
58           return false;
59         }
60     }
61     oo+=m;
62     if(mm!=oo)
63         return false;
64     else
65         return true;
66 
67 }
68 int main()
69 {
70 
71     scanf("%d%d",&n,&m);
72     for(int i=1;i<=n;i++){
73         scanf("%d",&gg[i]);
74     }
75     for(int i=1;i<=m;i++){
76         scanf("%d",&he[i]);
77     }
78    int l=1,r=n,mid;
79    //cout<<fun(1)<<endl;
80    //cout<<fun(2)<<endl;
81    //cout<<fun(3)<<endl;
82    while(l<r)
83    {
84        mid=(l+r)/2;
85        if(fun(mid))
86            r=mid;
87        else
88           l=mid+1;
89    }
90    //cout<<l<<endl;
91    if(fun(l))
92    cout<<l<<endl;
93    else
94     cout<<"-1"<<endl;
95    return 0;
96 }
原文地址:https://www.cnblogs.com/hsd-/p/5975614.html