Problem Statement
We have
N
logs of lengths
A
1
,
A
2
,
⋯
A
N
.
We can cut these logs at most
K
times in total. When a log of length
L
is cut at a point whose distance from an end of the log is
t
(
0
<
t
<
L
)
, it becomes two logs of lengths
t
and
L
−
t
.
Find the shortest possible length of the longest log after at most
K
cuts, and print it after rounding up to an integer.
Constraints
1
≤
N
≤
2
×
10
5
0
≤
K
≤
10
9
1
≤
A
i
≤
10
9
All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
K
A
1
A
2
⋯
A
N
Output
Print an integer representing the answer.
Sample Input 1
Copy
2 3
7 9
Sample Output 1
Copy
4
First, we will cut the log of length
7
at a point whose distance from an end of the log is
3.5
, resulting in two logs of length
3.5
each.
Next, we will cut the log of length
9
at a point whose distance from an end of the log is
3
, resulting in two logs of length
3
and
6
.
Lastly, we will cut the log of length
6
at a point whose distance from an end of the log is
3.3
, resulting in two logs of length
3.3
and
2.7
.
In this case, the longest length of a log will be
3.5
, which is the shortest possible result. After rounding up to an integer, the output should be
4
.
Sample Input 2
Copy
3 0
3 4 5
Sample Output 2
Copy
5
Sample Input 3
Copy
10 10
158260522 877914575 602436426 24979445 861648772 623690081 433933447 476190629 262703497 211047202
Sample Output 3
Copy
292638192
#include
using namespace std;
const int N=2e5+10;
int n,k;
int a[N];
int up(int x,int y)
{
if (x%y==0) return x/y-1;
else return (x/y);
}
bool check(int len)//check函数
{
int tot=0;
for (int i=1;i<=n;i++) tot+=up(a[i],len);//每次加上每个a被切的刀数!
if (tot<=k) return true;
return false;
}
int main()
{
cin>>n>>k;
for (int i=1;i<=n;i++) cin>>a[i];
int l=1,r=1e9;
while(l<r)
{
int mid=l+r>>1;
if(check(mid)) r=mid;
else l=mid+1;
}
cout<< r <<endl;
return 0;
}
D - Alter Altar
Problem Statement
An altar enshrines
N
stones arranged in a row from left to right. The color of the
i
-th stone from the left
(
1
≤
i
≤
N
)
is given to you as a character
c
i
; R stands for red and W stands for white.
You can do the following two kinds of operations any number of times in any order:
Choose two stones (not necessarily adjacent) and swap them.
Choose one stone and change its color (from red to white and vice versa).
According to a fortune-teller, a white stone placed to the immediate left of a red stone will bring a disaster. At least how many operations are needed to reach a situation without such a white stone?
Constraints
2
≤
N
≤
200000
c
i
is R or W.
Input
Input is given from Standard Input in the following format:
N
c
1
c
2
.
.
.
c
N
Output
Print an integer representing the minimum number of operations needed.
Sample Input 1
Copy
4
WWRR
Sample Output 1
Copy
2
For example, the two operations below will achieve the objective.
Swap the
1
-st and
3
-rd stones from the left, resulting in RWWR.
Change the color of the
4
-th stone from the left, resulting in RWWW.
Sample Input 2
Copy
2
RR
Sample Output 2
Copy
0
It can be the case that no operation is needed.
Sample Input 3
Copy
8
WRWWRWRR
Sample Output 3
Copy
3
#include
#include
#include
#include
using namespace std;
string s;
int main()
{
int n,cnt=0;
cin>>n>>s;
for(int i=0,j=n-1;i<j;i++,j--)
{
while(s[i]!='W' && i<j) i++;
while(s[j]!='R' && i<j) j--;
if(i<j) cnt++;
}
cout<< cnt <<endl;
return 0;
}
C - Repsept /
Problem Statement
Takahashi loves the number
7
and multiples of
K
.
Where is the first occurrence of a multiple of
K
in the sequence
7
,
77
,
777
,
…
? (Also see Output and Sample Input/Output below.)
If the sequence contains no multiples of
K
, print -1 instead.
Constraints
1
≤
K
≤
10
6
K
is an integer.
Input
Input is given from Standard Input in the following format:
K
Output
Print an integer representing the position of the first occurrence of a multiple of
K
. (For example, if the first occurrence is the fourth element of the sequence, print 4.)
Sample Input 1
Copy
101
Sample Output 1
Copy
4
None of
7
,
77
, and
777
is a multiple of
101
, but
7777
is.
Sample Input 2
Copy
2
Sample Output 2
Copy
-1
All elements in the sequence are odd numbers; there are no multiples of
2
.
Sample Input 3
Copy
999983
Sample Output 3
Copy
999982
#include
using namespace std;
int main()
{
int k;
cin>>k;
if(k%2==0 || k%5==0)
{
cout << -1 << endl;
return 0;
}
int v=0;
for(int i=1;;i++)
{
v=(v*10+7)%k;
if(!v)
{
cout<< i <<endl;
return 0;
}
}
return 0;
}