期末考试前
折腾折腾
A:
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.
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.
Print one integer number — the minimum number of hours required to water the garden.
3 6 2 3 5
2
6 7 1 2 3 4 5 6
7
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:
【思路】
根据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:
【思路】
暴力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