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
,就是要你求满足下列要求a
、b
。
解析
水题,可以枚举,也可以套特殊值。
#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;
}