Baidu Pinecone Elite Class--oj Competition (Second time)

1. Brother Xiaoma cuts the rope

**Title:** The Cultural Festival is coming soon, and Brother Xiaoma, as a member of the student union, needs to participate in the production of props. Due to being assigned to the fun tug-of-war, Brother Xiaoma needs to cut the rope. There are N ropes, and their lengths are L1,...,Ln.

If K ropes of the same length are cut from them, what is the longest length of each of these K ropes?

/**
	输入格式:第一行两个整数N 和K ;接下来N行,每行一个整数代表每条绳子的长度 Li。
	输出格式:一个整数代表切割后每条绳子的最大长度。
	样例1   输入:4 11
                8
                7
                4
                5
			输出:2
	备注
对于100%的数据:0<Li≤100000,0 <n ≤100000,0<k≤10000 ;切割前后绳子的长度均为整数。
*/         
#include<bits/stdc++.h> 

using namespace std;
const int M = 1e5 + 5;
int L[M], N, K, l, r, mid, ans;
bool check(int num){
    int tmp = 0;
    for(int i = 1; i <= N; i++){
        tmp += L[i] / num;//根据最长的绳子判断出总条数
    }
    if(tmp >= K){//当条数大于等于K条,说明绳子取短了
        return true;
    }else{//当条数小于K条,说明绳子取长了
        return false;
    }
}
int main( )
{
    cin >> N >> K;
    for(int i = 1; i <= N; i++){
        cin >> L[i];
        r = max(r, L[i]);//找到最长的绳子
    }
    while(l <= r){
        mid = l + (r - l) / 2;
        if(check(mid)){
            l = mid + 1;
            ans = mid;//解决最后l和r的取值不明的情况
        }else{
            r = mid - 1;
        }
    }
    cout << ans;
    return 0;
}

2. Brother Xiaoma, coffee taster

**Title:** This day, Brother Xiaoma wants to drink a cup of coffee. Now there are n kinds of condiments, and this cup of coffee can only add m kinds of them. After knowing all the n kinds of condiments, Brother Xiaoma used the computer to calculate the time ci consumed by all the condiments and the delicacy vi of the condiments. Brother Xiaoma wants to drink 求和符vi/求和符cithe largest coffee, that is, the most delicious coffee per unit time.

求和符It means summation, so 求和符vi/求和符ciit means the sum of delicacy divided by the sum of consumption time.

/**
	输入格式:第一行为两个整数n,m ,表示调料种数和能加入的调料数;接下来两行,每行为n个数,第一行第i个整数表示美味度ui ,第二行第i个整数表示时间ci。
	输出格式:一个整数表示小码哥喝的咖啡的最大`求和符vi/求和符ci`,保留三位小数。
	样例1   输入:3 2
                1 2 3
                3 2 1
			输出:1.667
	备注
数据范围:20%: 1≤n ≤5; 50%: 1≤n≤10; 80%: 1≤n≤50; 100%: 1≤n ≤200,1≤m≤n,1≤c[团], v[2]≤10000 ;保证答案不超过1000。
*/         
#include<bits/stdc++.h> 

using namespace std;
int n, m;
double v[205], c[205], t[205];
bool check(double x){
    double sum = 0;
    //v[i] = xc[i] -> v[i] - xc[i] = 0
    for(int i = 1; i <= n; i++){
        t[i] = v[i] - x * c[i];
    }
    sort(t + 1, t + n + 1);//升序
    for(int i = n; i >= n - m + 1; i--){
        sum += t[i];//取最大的m种
    }
    if(sum > 0){
        return true;
    }else{
        return false;
    }
}
int main(){
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        cin >> v[i];
    }
    for(int i = 1; i <= n; i++){
        cin >> c[i];
    }
    double l = 0, r = 1001;//由于答案不超过1000
    while(r - l > 1e-5){//答案取的范围最小为0.0001
        double mid = l + (r - l) / 2.0;
        if(check(mid)){//当v[i] - xc[i] > 0时,mid取小了
            l = mid;
        }else{
            r = mid;
        }
    }
    printf("%.3lf", l);
    return 0;
}

3. Divide the candy evenly

**Title:** Brother Xiaoma has n subordinates, the positions are from 1 to n, they sit in a row, each person has ai candy, and each person can only give the candy to the left and right people, because sitting in 1 and n Warlocks are in the position, and they can pass candies to each other through their skills. Each time each person passes a candy, the cost is 1.

The data guarantees that the candy can be divided evenly, and there will be no decimals.

/**
	输入格式:第一行一个正整数n ≤le3,表示手下的个数;接下来n行,每行一个整数ai,表示第i个手下得到的糖果的颗数。
	输出格式:求使所有人获得均等糖果的最少代价。
	样例1   输入:4
                9
                8 
                17 
                6
			输出:8
	备注
		其中:1≤n ≤1e3,0≤ai ≤ 1e5
*/         
#include<bits/stdc++.h> 

using namespace std;
int n, a[1005], x[1005], sum, avg, ans; 
int main(){
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        sum += a[i];
    }
    avg = sum / n;
    //等价于一个圈,对于第一位 -> avg = a[1] + xn - x1(自己加上第n位给的减去给第二位的) 
    //可以推导出avg = a[n] + x(n-1) - xn -> xn = a[n] - avg - x(n-1)
    //将给出的xn求和 <==> sum(|xn|) = x(n-1) + avg - a[n]  <==>  花费的代价
    for(int i = 1; i <= n; i++){
        x[i] = x[i - 1] + avg - a[i];
    }
    sort(x + 1, x + 1 + n);//花费代价升序
    数轴距离模型 --> 取中间值时该点到其它点的距离之和最短
    //等价于本题的取排序后中间的人的糖果数,分给其它人后相加的代价最小 
    //x[(n + 1) / 2] -> 向下取整    n / 2 + 1 -> 向上取整
    for(int i = 1; i <= n; i++){
        ans += abs(x[(n + 1) / 2] - x[i]); 
    }
    cout << ans;
    return 0;
}

Four, holding a shield

**Title:**n shield guards were looking for victims on Chernobog, but unfortunately they met a black snake, who attacked the shield guard with her flames, and in a sea of ​​flames, the shield guards decided to line up One column, retreating in this flame storm. The red arrow is the attack direction of the black snake. Obviously, the shield guards (black squares) can receive the least attack when they are lined up. Each shield guard has two attributes of strength and quality. For each shield guard, his risk value is the sum of the quality w of all shield guards in front of him minus his strength s. The sum of the masses does not include his own mass.

Ask how to arrange the order of the shield guards so that the largest risk value among the shield guards is the lowest?

/**
	输入格式:第一行一个正整数n ;第二行到第1+n行,每行两个正整数,表示盾卫的质量和力量。
	输出格式:输出一个使盾卫中最大的风险值最低的排序下,最大的风险值。
	样例1   输入:3
                10 3
                2 5
                3 3
			输出:2
	备注
其中:1≤n≤50000,1≤w≤100000,1 ≤s≤1000000000
*/         
#include<bits/stdc++.h> 

using namespace std;
const int N = 5e4 + 5;
struct NODE{
    int w, s, sum;
}node[N];
//当w1在前面,假设此时排序最优   ...w1|w2  -> max(all - s1, all + w1 - s2)
//当w2在前面  ...w2|w1 -> max(all -s2, all + w2 -s1)   此时w1前 <  w2前
//依次比较可以推断出风险值 -> all + w1 -s2 < all + w2 - s1 -> w1 + s1 < w2 + s2
bool cmp(NODE a, NODE b){
    return a.sum < b.sum;//风险值升序
}
int n, ans = -0x3f3f3f3f, presum;
int main(){
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> node[i].w >> node[i].s;
        node[i].sum += node[i].w + node[i].s;//计算风险总值
    }
    sort(node + 1, node + n + 1, cmp);
    for(int i = 1; i <= n; i++){
        presum += node[i - 1].w;//除去自身重量
        ans = max(ans, presum - node[i].s);
    }
    cout << ans;
    return 0;
}

5. Activities

**Title: **Brother Xiaoma has been assigned to handle matters in the company hall again. The company only has one hall, but there are many activities that need to be held, so reservations need to be made in advance. The company has n departments, and each department will hold a Activity. Each department will report the time interval (a, b) (open interval) they expect to use the hall to Brother Xiaoma. Brother Xiaoma will make arrangements according to the time. Only one event can be held at a time. Once the event starts, it must end to proceed to the next activity.

Brother Xiaoma wants to satisfy as many events as possible, so I ask you to help arrange the event.

/**
	输入格式:第一行一个整数n,代表部门的个数;接下来的n行,每行两个整数a,b,代表时间区间.
	输出格式:输出一个整数代表能够召开的最大活动个数。
	样例1   输入:4
                1 3
                4 6
                2 5
                1 7
			输出:2
	备注
		其中: 1≤n ≤500000,0≤a<b≤10^9
*/         
#include<bits/stdc++.h> 

using namespace std;
const int N = 5e5 + 5;
int n, ans;
struct NODE{
    int l, r;
}node[N];
bool cmp(NODE a, NODE b){
    return a.r < b.r;//按活动结束时间升序
}
int main(){
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> node[i].l >> node[i].r;
    }
    sort(node + 1, node + 1 + n, cmp);
    int temp = 0;//定义一个临时变量来存储可行的结束时间
    for(int i = 1; i <= n; i++){
        //下一个活动的开始时间大于上一个活动的结束时间
        if(node[i].l >= temp){
            temp = node[i].r;
            ans++;
        }
    }
    cout << ans;
    return 0;
}

6. Dessert supply

**Title:** Brother Xiaoma's C subordinates have different sweet tooth preferences, and for a sweet tooth, a number vi can be used to represent his sweetness. Brother Xiaoma's subordinates expressed their preference for desserts with a certain range of sweetness. For a subordinate numbered i, his dessert favorite range is [li, ri], but if the dessert exceeds or falls below this range, it means he hates it and affects his mood at work that day. In order to maximize the work efficiency at work, Brother Xiaoma purchased a batch of desserts. There are L kinds of desserts, and there are numi desserts with a sweetness of vi.

May I ask how many people you can eat your favorite dessert at most?

/**
	输入格式:第1行:两个空格分隔的整数:C和L;第2…C+1 行:每行用两个整数描述手下i的甜度要求:li和ri;第C+2·….C+L+1行:每行两个整数描述某一种甜品的甜度和个数: vi和numi 。
	输出格式:带有整数的单行,表示可以使最多的手下吃到他们喜欢的甜品的人数。
	样例1   输入3 2
                3 10
                2 5
                1 5
                6 2
                4 1
			输出:2
	备注
提示:其中:1≤L,C ≤2500,1 ≤li, ri, vi, numi≤10^5
*/         
#include<bits/stdc++.h> 

using namespace std;
const int N = 1e5 + 5;
struct PEOPLE{
    int l, r;
    bool operator<(const PEOPLE &a) const{
        if(l == a.l){//当最低甜度相同时,最大甜度先的排前
            return r < a.r;
        }
        return l < a.l;//以最低甜度升序
    }
}people[N];
struct SWEET{
    int v, num;
    bool operator>(const SWEET &a) const{
        return v > a.v;//甜度降序
    }
}sweet[N];
int C, L, cnt[N], ans;
//定义小根堆,根据甜度来构造小根堆
priority_queue<SWEET, vector<SWEET>, greater<SWEET>> q;
int main(){
    cin >> C >> L;
    for(int i = 1; i <= C; i++){
        cin >> people[i].l >> people[i].r;
    }
    sort(people + 1, people + 1 + C);//根据开始时间排序
    for(int i = 1; i <= L; i++){
        cin >> sweet[i].v >> sweet[i].num;
        cnt[sweet[i].v] += sweet[i].num;
        q.push(sweet[i]);//将甜品入小根堆
    }
    for(int i = 1; i <= C; i++){
        //将小根堆的甜度,从小依次出堆和每一个手小的最小甜度比较
        //若甜度小于手下的最小甜度就将该甜品出堆
        while(!q.empty() && q.top().v < people[i].l){
            q.pop();
        }
        //当甜度大于手下最小的甜度&甜度小于手下的最大甜度
        if(!q.empty() && q.top().v <= people[i].r){
            ans++;//满足人数加一
            cnt[q.top().v]--;//甜品数量减一
            if(cnt[q.top().v] == 0){
                q.pop();//甜品分完出堆
            }
        }
    }
    cout << ans;
    return 0;
}

Seven, the combination of Fibonacci sequence

**Title:**Given a positive integer n, please write a function MinFibonacciNumbers that returns the minimum number of Fibonacci numbers whose sum is n.

The Fibonacci sequence F1=1 F2=1 Fn= Fn-1+Fn-2,n > 2guarantees that there must be a solution.

/**
	输入格式:第一行输入正整数n。
	输出格式:输出满足要求的数字的最少数目。
	样例1   输入:19
			输出:3
	备注
		其中: 1≤n≤109;
		样例:19=1+5+13,所以至少需要3个数字。
*/         
#include<bits/stdc++.h> 

using namespace std;
int MinFibonacciNumbers(int k){
    vector<int> v;
    v.push_back(1);
    v.push_back(1);
    while(1){//将斐波那契数列1e9中的数字全部入容器
        if(v[v.size() - 1] + v[v.size() - 2] > 1e9){
            break;
        }
        v.push_back(v[v.size() - 1] + v[v.size() - 2]);
    }
    int cnt = 0;
    for(int i = v.size() - 1; i >= 0; i--){
        if(k >= v[i]){
            cnt += k / v[i];//看最大的数有几个符合相加起来
            k -= k / v[i] * v[i];//原来的数减去向下取整的数
        }
        if(k == 0){
            break;
        }
    }
    return cnt;
}
int main(){
    int n; 
    cin >> n;
    cout << MinFibonacciNumbers(n);
    return 0;
}

8. Brother Xiaoma's formation command

**Title:** Brother Xiaoma has n troops stationed on a road, which can be regarded as an x-axis. Brother Xiaoma’s command is at the origin, and the i-th army is at a[i] Position, now rearrange the position of the troops, set the position after arrangement as b[i], require b[i]≥a[i], and the distance between each unit is greater than or equal to X.

Explanation: Troops are processed sequentially from the first one. Once processed, the position does not change. So you need to make an optimal solution for each unit under the current situation (the current situation refers to the situation after the unit with the serial number before him has been updated), that is, the closer the unit is to the little code brother under the above conditions the better.

/**
	输入格式:第一行两个整数n,X ;第二行n个整数,表示a[i]。
	输出格式:一行,输出b[列],空格分开。
	样例1   输入:4 11
				1 21 11 7
			输出:1 21 32 43
    样例2   输入:4 10
                1 21 11 7
                输出:1 21 11 31
	备注
		其中: 1≤n ≤1000,1 ≤x, a[i], b[i]≤10^6
*/         
#include<bits/stdc++.h> 

using namespace std;
#define N 1005
int n, X, a[N], b[N];
int main(){
    cin >> n >> X;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    b[1] = a[1];//题目要求从第一个按顺序处理
    for(int i = 2; i <= n; i++){
        //插入排序,将数组a依次判断做升序处理
        sort(a, a + i);
        //这里的插入排序是从前面开始依次比较
        for(int j = 1; j < i; j++){
            //依次与前面的对比位置,距离小了
            if(fabs(a[i] - a[j]) < X){
                a[i] = a[j] + X;
                b[i] = a[i];
            }else{//距离符合从新给b[i]数组赋值即可
                b[i] = a[i];
            }
        }
    }
    for(int i = 1; i <= n; i++){
        cout << b[i] << " ";
    }
    return 0;
}

9. Activities group

**Title:** Xiaoma brother organizes a team building activity, with a total of n people participating. The activity is divided into two projects, A and B, and each project requires two people to participate in a team. Assuming that each person has two ability values ​​a and b represent the proficiency in A and B respectively. In order to make the activity go smoothly, Brother Xiaoma hopes that the sum of A ability values ​​of two people in each group is equal to the sum of B ability values. and.

Please help to calculate whether there is a possibility of grouping to satisfy Brother Xiaoma's idea.

/**
	输入格式:输入共三行,第一行一个偶数n∈[2,1×10^5]表示总人数;第二行n个正整数表示每个人A项目的能力值;第三行n个正整数表示每个人B项目的能力值。
	输出格式:一行,如果存在这样的分组输出YES,否则输出NO
	样例1   输入:6
                1 2 3 4 5 6
                6 5 4 3 2 1
			输出:YES
	备注
    第一个和第六个一组,第二个和第五个一组,第三个和第四个一组,这样每组的A项目能力值之和均为7,B项目能力值之和均为7。
*/         
#include<bits/stdc++.h> 

using namespace std;
const int N = 1e5 + 5;
int n, a[N], b[N], c[N];
int main(){
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    for(int i = 1; i <= n; i++){
        cin >> b[i];
    }
    //a[i] + a[j] = b[i] + b[j]
    //a[i] - b[i] = b[j] - a[j]
    //(a[i] - b[i]) + (a[j] - b[j]) = 0
    for(int i = 1; i <= n; i++){
        c[i] = a[i] - b[i];//求两项目的能力差值
    }
    sort(c + 1, c + n + 1);//能力差值升序
    //推导出最高位加最低位等于0
    for(int i = 1; i <= n; i++){
        //只需要考虑异常情况即可
        if(c[i] + c[n - i + 1] != 0){
            cout << "NO";
            return 0;
        }
    }
    cout << "YES";
    return 0;
}

10. Food Delivery

**Title: **Brother Xiaoma has been assigned to build a food delivery station again. The living places ai of all residents are on the same x-axis. Location. Brother Xiaoma, please help to calculate where to build the station so that the sum of the distances from the station to each resident is minimized?

Note: It is not guaranteed that the residence location ai of the residents is unique, that is, two residents may have the same residence location, and the takeaway station is allowed to be located in the residence location.

/**
	输入格式:第一行一个整数n,表示居民的居住地点个数;第二行n个整数a1~an。
	输出格式:输出一个整数,表示距离之和的最小值。
	样例1   输入:4
				6 2 9 1
			输出:12
	备注
其中:对于100%的数据: 1≤n ≤100000, abs(ai)≤10000000
*/         
#include<bits/stdc++.h> 

using namespace std;
#define ll long long
const int N = 1e5;
ll n, a[N], ans;
int main(){
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    sort(a + 1, a + 1 + n);//将房子位置升序
    //数轴距离模型,取中间值,到各点的距离之和最短
    for(int i = 1; i <= n; i++){
        ans += abs(a[(n + 1) / 2] - a[i]);
    }
    cout << ans;
    return 0;
}

记录每一个学习瞬间

Guess you like

Origin blog.csdn.net/qq_51601665/article/details/130141337