Educational Codeforces Round 36 (Rated for Div. 2) A-C
2018-01-14 00:43
555 查看
期末考试前
折腾折腾
A:
A. Garden
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Luba thinks about watering her garden. The garden can be represented as a segment of length k. Luba has got n buckets,
the i-th bucket allows her to water some continuous subsegment of garden of length exactly ai each
hour. Luba can't water any parts of the garden that were already watered, also she can't water the ground outside the garden.
Luba has to choose one of the buckets in order to water the garden as fast as possible (as mentioned above, each hour she will water some continuous subsegment of length ai if
she chooses the i-th bucket). Help her to determine the minimum number of hours she has to spend watering the garden. It is guaranteed
that Luba can always choose a bucket so it is possible water the garden.
See the examples for better understanding.
Input
The first line of input contains two integer numbers n and k (1 ≤ n, k ≤ 100)
— the number of buckets and the length of the garden, respectively.
The second line of input contains n integer numbers ai (1 ≤ ai ≤ 100)
— the length of the segment that can be watered by the i-th bucket in one hour.
It is guaranteed that there is at least one bucket such that it is possible to water the garden in integer number of hours using only this bucket.
Output
Print one integer number — the minimum number of hours required to water the garden.
Examples
input
output
input
output
Note
In the first test the best option is to choose the bucket that allows to water the segment of length 3. We can't choose the bucket that allows
to water the segment of length 5 because then we can't water the whole garden.
In the second test we can choose only the bucket that allows us to water the segment of length 1.
【思路】
求因子最大?
int a[MAXN];
int main()
{
int n,k;
cin>>n>>k;
int ans=INF;
int x;
for(int i=1;i<=n;i++)
{
cin>>x;
if(k%x==0)
{
ans=min(ans,k/x);
}
}
cout<<ans<<endl;
return 0;
}
B:
B. Browser
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Luba is surfing the Internet. She currently has n opened tabs in her browser, indexed from 1 to n from
left to right. The mouse cursor is currently located at the pos-th tab. Luba needs to use the tabs with indices from l to r (inclusive)
for her studies, and she wants to close all the tabs that don't belong to this segment as fast as possible.
Each second Luba can either try moving the cursor to the left or to the right (if the cursor is currently at the tab i, then she can
move it to the tab max(i - 1, a) or to the tab min(i + 1, b))
or try closing all the tabs to the left or to the right of the cursor (if the cursor is currently at the tab i, she can close all
the tabs with indices from segment [a, i - 1] or from segment [i + 1, b]).
In the aforementioned expressions a and b denote
the minimum and maximum index of an unclosed tab, respectively. For example, if there were 7 tabs initially and tabs 1, 2 and 7are
closed, then a = 3, b = 6.
What is the minimum number of seconds Luba has to spend in order to leave only the tabs with initial indices from l to r inclusiveopened?
Input
The only line of input contains four integer numbers n, pos, l, r (1 ≤ n ≤ 100, 1 ≤ pos ≤ n, 1 ≤ l ≤ r ≤ n)
— the number of the tabs, the cursor position and the segment which Luba needs to leave opened.
Output
Print one integer equal to the minimum number of seconds required to close all the tabs outside the segment [l, r].
Examples
input
output
input
output
input
output
Note
In the first test Luba can do the following operations: shift the mouse cursor to the tab 2, close all the tabs to the left of it, shift the
mouse cursor to the tab 3, then to the tab 4,
and then close all the tabs to the right of it.
In the second test she only needs to close all the tabs to the right of the current position of the cursor.
In the third test Luba doesn't need to do anything.
【思路】
根据pos 位置分情况? 在里面 在外面?
nt main()
{
int n,pos,l,r;
cin>>n>>pos>>l>>r;
if(l==1&&r==n)
{
cout<<0<<endl;
return 0;
}
else
{
int ans=0;
if(pos>=l&&pos<=r)
{
if(l>1&&r<n)
{
ans= min(fabs(pos-l),fabs(pos-r))+ (r-l)+2;
}
else
{
if(l>1&&r==n)
ans=fabs(pos-l)+1;
else if(r<n&&l==1)
ans=fabs(pos-r)+1;
}
}
else
{
if(l>1&&r<n)
{
ans= max(fabs(pos-l),fabs(pos-r))+2;
}
else
{
if(l>1&&r==n)
{
ans=fabs(pos-l)+1;
}
else if(r<n&&l==1)
{
ans=fabs(pos-r)+1;
}
}
}
cout<<ans<<endl;
}
return 0;
}
C:
C. Permute Digits
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given two positive integer numbers a and b.
Permute (change order) of the digits of a to construct maximal number not exceeding b.
No number in input and/or output can start with the digit 0.
It is allowed to leave a as it is.
Input
The first line contains integer a (1 ≤ a ≤ 1018).
The second line contains integer b (1 ≤ b ≤ 1018).
Numbers don't have leading zeroes. It is guaranteed that answer exists.
Output
Print the maximum possible number that is a permutation of digits of a and is not greater than b.
The answer can't have any leading zeroes. It is guaranteed that the answer exists.
The number in the output should have exactly the same length as number a. It should be a permutation of digits of a.
Examples
input
output
input
output
input
output
【思路】
暴力b每一位 a匹配过来 两种请况 1: <b【】时 2:=b【】时。 = 时 类似于前一种情况,此时前缀 要加上一致的 < 时 就是 暴力枚举所有情况里 最大的又不超过b的
#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define findx(x,b,n) lower_bound(b+1,b+1+n,x)-b
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w",stdout)
#define SHUT ios_base::sync_with_stdio(false); cout.setf(ios::fixed); cout.tie(nullptr); cin.tie(nullptr);
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#define FI(n) IO::read(n)
#define Be IO::begin()
using namespace std;
typedef long long ll;
const double PI=acos(-1);
const int INF=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int MAXN=50005;
const int MOD=1e9+7;
const int mod=1e9+7;
int dir[5][2]={0,1,0,-1,1,0,-1,0};
int num[12];
int main()
{
SHUT;
mem(num,0);
string a;
string b;
string ans;
cin>>a>>b;
if(b.length()>a.length())
{
sort(a.rbegin(),a.rend());
cout<<a<<endl;
}
else
{
for(char c: a)
num[c-'0']++;
string cur="";
for(int i=0;i<b.length();i++)
{
for(int j=(b[i]-'0')-1;j>=0;j--)
if(num[j])
{
string z=cur+ char(j+'0');
num[j]--;
for(int x=9;x>=0;x--)
{
for(int y=0;y<num[x];y++)
z+=char(x+'0');
}
//cout<<z<<endl;
ans=max(ans,z);
num[j]++;
}
if(num[b[i]-'0'])
{
cur+=b[i];
num[b[i]-'0']--;
if(cur.size()==b.size() && cur<=b)
{
ans=max(ans,cur);
}
}else break;
}
cout<<ans<<endl;
}
return 0;
}
1122
折腾折腾
A:
A. Garden
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Luba thinks about watering her garden. The garden can be represented as a segment of length k. Luba has got n buckets,
the i-th bucket allows her to water some continuous subsegment of garden of length exactly ai each
hour. Luba can't water any parts of the garden that were already watered, also she can't water the ground outside the garden.
Luba has to choose one of the buckets in order to water the garden as fast as possible (as mentioned above, each hour she will water some continuous subsegment of length ai if
she chooses the i-th bucket). Help her to determine the minimum number of hours she has to spend watering the garden. It is guaranteed
that Luba can always choose a bucket so it is possible water the garden.
See the examples for better understanding.
Input
The first line of input contains two integer numbers n and k (1 ≤ n, k ≤ 100)
— the number of buckets and the length of the garden, respectively.
The second line of input contains n integer numbers ai (1 ≤ ai ≤ 100)
— the length of the segment that can be watered by the i-th bucket in one hour.
It is guaranteed that there is at least one bucket such that it is possible to water the garden in integer number of hours using only this bucket.
Output
Print one integer number — the minimum number of hours required to water the garden.
Examples
input
3 6 2 3 5
output
2
input
6 7 1 2 3 4 5 6
output
7
Note
In the first test the best option is to choose the bucket that allows to water the segment of length 3. We can't choose the bucket that allows
to water the segment of length 5 because then we can't water the whole garden.
In the second test we can choose only the bucket that allows us to water the segment of length 1.
【思路】
求因子最大?
int a[MAXN];
int main()
{
int n,k;
cin>>n>>k;
int ans=INF;
int x;
for(int i=1;i<=n;i++)
{
cin>>x;
if(k%x==0)
{
ans=min(ans,k/x);
}
}
cout<<ans<<endl;
return 0;
}
B:
B. Browser
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Luba is surfing the Internet. She currently has n opened tabs in her browser, indexed from 1 to n from
left to right. The mouse cursor is currently located at the pos-th tab. Luba needs to use the tabs with indices from l to r (inclusive)
for her studies, and she wants to close all the tabs that don't belong to this segment as fast as possible.
Each second Luba can either try moving the cursor to the left or to the right (if the cursor is currently at the tab i, then she can
move it to the tab max(i - 1, a) or to the tab min(i + 1, b))
or try closing all the tabs to the left or to the right of the cursor (if the cursor is currently at the tab i, she can close all
the tabs with indices from segment [a, i - 1] or from segment [i + 1, b]).
In the aforementioned expressions a and b denote
the minimum and maximum index of an unclosed tab, respectively. For example, if there were 7 tabs initially and tabs 1, 2 and 7are
closed, then a = 3, b = 6.
What is the minimum number of seconds Luba has to spend in order to leave only the tabs with initial indices from l to r inclusiveopened?
Input
The only line of input contains four integer numbers n, pos, l, r (1 ≤ n ≤ 100, 1 ≤ pos ≤ n, 1 ≤ l ≤ r ≤ n)
— the number of the tabs, the cursor position and the segment which Luba needs to leave opened.
Output
Print one integer equal to the minimum number of seconds required to close all the tabs outside the segment [l, r].
Examples
input
6 3 2 4
output
5
input
6 3 1 3
output
1
input
5 2 1 5
output
0
Note
In the first test Luba can do the following operations: shift the mouse cursor to the tab 2, close all the tabs to the left of it, shift the
mouse cursor to the tab 3, then to the tab 4,
and then close all the tabs to the right of it.
In the second test she only needs to close all the tabs to the right of the current position of the cursor.
In the third test Luba doesn't need to do anything.
【思路】
根据pos 位置分情况? 在里面 在外面?
nt main()
{
int n,pos,l,r;
cin>>n>>pos>>l>>r;
if(l==1&&r==n)
{
cout<<0<<endl;
return 0;
}
else
{
int ans=0;
if(pos>=l&&pos<=r)
{
if(l>1&&r<n)
{
ans= min(fabs(pos-l),fabs(pos-r))+ (r-l)+2;
}
else
{
if(l>1&&r==n)
ans=fabs(pos-l)+1;
else if(r<n&&l==1)
ans=fabs(pos-r)+1;
}
}
else
{
if(l>1&&r<n)
{
ans= max(fabs(pos-l),fabs(pos-r))+2;
}
else
{
if(l>1&&r==n)
{
ans=fabs(pos-l)+1;
}
else if(r<n&&l==1)
{
ans=fabs(pos-r)+1;
}
}
}
cout<<ans<<endl;
}
return 0;
}
C:
C. Permute Digits
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given two positive integer numbers a and b.
Permute (change order) of the digits of a to construct maximal number not exceeding b.
No number in input and/or output can start with the digit 0.
It is allowed to leave a as it is.
Input
The first line contains integer a (1 ≤ a ≤ 1018).
The second line contains integer b (1 ≤ b ≤ 1018).
Numbers don't have leading zeroes. It is guaranteed that answer exists.
Output
Print the maximum possible number that is a permutation of digits of a and is not greater than b.
The answer can't have any leading zeroes. It is guaranteed that the answer exists.
The number in the output should have exactly the same length as number a. It should be a permutation of digits of a.
Examples
input
123
222
output
213
input
392110000
output
9321
input
49405000
output
4940
【思路】
暴力b每一位 a匹配过来 两种请况 1: <b【】时 2:=b【】时。 = 时 类似于前一种情况,此时前缀 要加上一致的 < 时 就是 暴力枚举所有情况里 最大的又不超过b的
#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define findx(x,b,n) lower_bound(b+1,b+1+n,x)-b
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w",stdout)
#define SHUT ios_base::sync_with_stdio(false); cout.setf(ios::fixed); cout.tie(nullptr); cin.tie(nullptr);
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#define FI(n) IO::read(n)
#define Be IO::begin()
using namespace std;
typedef long long ll;
const double PI=acos(-1);
const int INF=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int MAXN=50005;
const int MOD=1e9+7;
const int mod=1e9+7;
int dir[5][2]={0,1,0,-1,1,0,-1,0};
int num[12];
int main()
{
SHUT;
mem(num,0);
string a;
string b;
string ans;
cin>>a>>b;
if(b.length()>a.length())
{
sort(a.rbegin(),a.rend());
cout<<a<<endl;
}
else
{
for(char c: a)
num[c-'0']++;
string cur="";
for(int i=0;i<b.length();i++)
{
for(int j=(b[i]-'0')-1;j>=0;j--)
if(num[j])
{
string z=cur+ char(j+'0');
num[j]--;
for(int x=9;x>=0;x--)
{
for(int y=0;y<num[x];y++)
z+=char(x+'0');
}
//cout<<z<<endl;
ans=max(ans,z);
num[j]++;
}
if(num[b[i]-'0'])
{
cur+=b[i];
num[b[i]-'0']--;
if(cur.size()==b.size() && cur<=b)
{
ans=max(ans,cur);
}
}else break;
}
cout<<ans<<endl;
}
return 0;
}
1122
相关文章推荐
- Educational Codeforces Round 36 (Rated for Div. 2) 题解
- Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree(并查集)
- Educational Codeforces Round 36 (Rated for Div. 2)C. Permute Digits(贪心)
- Educational Codeforces Round 36 (Rated for Div. 2) E. Physical Education Lessons
- Educational Codeforces Round 36 (Rated for Div. 2) E. Physical Education Lessons
- 【codeforces 】Educational Codeforces Round 36 (Rated for Div. 2)(C.模拟 D.拓扑排序判断环)
- Educational Codeforces Round 36 (Rated for Div. 2) C 不大于某个数的序列变换
- Educational Codeforces Round 36 (Rated for Div. 2)D. Almost Acyclic Graph(正解!!百度的那些拓扑排序现在都已经TLE!)
- Educational Codeforces Round 33 (Rated for Div. 2)A-F
- Educational Codeforces Round 40 (Rated for Div. 2) E. Water Taps
- Educational Codeforces Round 35 (Rated for Div. 2) F. Tree Destruction(dfs)
- Educational Codeforces Round 39 (Rated for Div. 2)
- Educational Codeforces Round 39 (Rated for Div. 2) A. Partition(水题)
- Educational Codeforces Round 37 (Rated for Div. 2)
- Educational Codeforces Round 34 (Rated for Div. 2) D - Almost Difference
- Educational Codeforces Round 42 (Rated for Div. 2) D - Merge Equals
- Educational Codeforces Round 33 (Rated for Div. 2) A题. Chess For Three
- Educational Codeforces Round 33 (Rated for Div. 2) A - Chess For Three
- Educational Codeforces Round 33 (Rated for Div. 2) A-C题解
- Educational Codeforces Round 39 (Rated for Div. 2) B. Weird Subtraction Process