Codeforces-1090A & 1090M & 1088A & 1060A & 1061A

Codeforces-1090A & 1090M & 1088A & 1060A & 1061A

  • Codeforces-1090A - Company Merging
  • Codeforces-1090M - The Pleasant Walk
  • Codeforces-1088A - Ehab and another construction problem
  • Codeforces-1060A - Phone Numbers
  • Codeforces-1061A - Coins

Codeforces-1090A- Company Merging

题目链接
题目大意

就是给你n个公司,每一个公司m个人,然后要将这n个公司两个两个的合并,每两个公司合并有一个要求: 这两个公司的员工中工资最高的人的工资要相等。

现在每次合并都可以给某个公司的人加工资,使得某两个公司可以合并,两个两个的合,问最少的需要加的工资?
在这里插入图片描述

解析

题目不难,就是将每个公司的员工最高工资抽取出来,按照这个工资排序,然后相邻求下所需要加的钱即可。

这是当时写的代码,稍微有点多余(好像没有必要排序)的是对按照每个公司最高员工的工资来进行对vectort数组的一遍排序,然后求结果。

#include <bits/stdc++.h>

const int MAX = 2*100000 + 1;
typedef long long ll;

class Pair{
public:
    ll sz;
    ll mx;
    Pair(ll sz, ll mx){ 
        this->sz = sz;
        this->mx = mx;
    }
};

bool cmp(const Pair& pa, const Pair& pb){ 
    return pa.mx < pb.mx;
}

int main(int argc, char const** argv)
{ 
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    ll n, m, mx, maxx;
    std::vector<Pair>vt;
    std::cin >> n; 
    for(int i = 0; i < n; i++){ 
        std::cin >> m;
        maxx = 0;
        for(int j = 0; j < m; j++){ 
            std::cin >> mx;
            maxx = std::max(maxx, mx);
        }
        vt.push_back(Pair(m, maxx));
    }
    std::sort(vt.begin(), vt.end(), cmp); //sorted by max salary
    ll res = 0;
    for(int i = 1; i < vt.size(); i++){ 
        res += (vt[i].mx - vt[i-1].mx)*vt[i-1].sz;
        vt[i].sz += vt[i-1].sz; // remember to update the number of employee
    }
    std::cout << res << std::endl;
    return 0;
}


Codeforces-1090M - The Pleasant Walk

题目链接
题目大意

给你一排颜色(用不同的数字区分),要你求最长的每个相邻颜色都不同的子数组长度。
在这里插入图片描述

解析

简单的动态规划题。dp[i]代表的是当前0~i之间的最长答案。

代码:(上面是一维dp数组代码,下面是滚动优化代码)

#include <bits/stdc++.h>

const int MAX = 100001;

int main(int argc, char const** argv)
{ 
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n, k, arr[MAX+1], dp[MAX+1] ;
    std::cin >> n >> k;
    for(int i = 0; i < n; i++)
        std::cin >> arr[i];
    int res = 1;
    dp[0] = 1; // remember this
    for(int i = 1; i < n; i++){
        dp[i] = 1;
        if(arr[i] != arr[i-1])
            dp[i] = dp[i-1] + 1;
        res = std::max(res, dp[i]);
    }
    std::cout << res << std::endl;
    return 0;
}

#if 0
// roll optimize 
int main(int argc, char const** argv)
{ 
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n, k, arr[MAX+1] ;
    std::cin >> n >> k;
    for(int i = 0; i < n; i++)
        std::cin >> arr[i];
    int res = 1, roll = 1;
    for(int i = 1; i < n; i++){
        if(arr[i] != arr[i-1])
            roll += 1;
        else 
            roll = 1;
        res = std::max(res, roll);
    }
    std::cout << res << std::endl;
    return 0;
}
#endif


Codeforces-1088A - Ehab and another construction problem

题目链接
题目大意

给你x,就是要你求满足下列要求ab
在这里插入图片描述

解析

水题,可以枚举,也可以套特殊值。

#include <bits/stdc++.h>

int main(int argc, char const** argv)
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int x; 
    std::cin >> x;

    for(int a = 1; a <= x; a++){ 
        for(int b = 1; b <= x; b++){ 
            if(a % b == 0 && a*b > x && a/b < x){ 
                std::cout << a << " " <<  b << std::endl;
                return 0;
            }
        }
    }
    std::cout << -1 << std::endl;
    return 0;
}
#include <bits/stdc++.h>

int main(int argc, char const** argv)
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int x; 
    std::cin >> x;
    if(x == 1)
        std::cout << -1 << std::endl;
    else 
        std::cout << x << " " << x << std::endl;
    return 0;
}

Codeforces-1060A - Phone Numbers

题目链接
题目大意

给一个n,和n个字符,问你这串字符串可以组成多少个电话号码,电话号码有2个要求: ①有11个数;②第一个数字为8
在这里插入图片描述

解析

水题。

#include <bits/stdc++.h>

const int MAX = 101;

int main(int argc, char const **argv)
{ 
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n;
    char str[MAX];
    std::cin >> n >> str;
    int eightNum = 0;
    for(int i = 0; i < n; i++)
        eightNum = str[i] == '8' ? eightNum+1 : eightNum;
    std::cout << (eightNum > n/11 ? n/11 : eightNum) << std::endl;
    return 0;
}


Codeforces-1090A- Company Merging

题目链接
题目大意

给你1~n的硬币权值,和一个数值S,请你用最少的硬币凑成这个钱数。
在这里插入图片描述

解析

水题。

#include <bits/stdc++.h>

int main(int argc, char const **argv)
{ 
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n, S;
    std::cin >> n >> S;
    std::cout << (S%n ? S/n+1 : S/n) << std::endl; 
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值