2024年Codeforces Global Round 14(A~D)_a,C C++大厂高级面试题灵魂100问

img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以戳这里获取

using namespace std;
const int N = 1e5 + 50;
#define reset(x) memset(x, 0, sizeof(x))
#define Q_in_out ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> P;
bool check(int n){
int tmp=sqrt(n);
return tmp*tmpn;
}
int solve()
{
int n;
cin>>n;
if(n%2
0&&check(n/2)) cout<<“YES”;
else if(n%4==0&&check(n/4)) cout<<“YES”;
else cout<<“NO”;
return 0;
}
int main()
{
Q_in_out
int t;
cin >> t;
while (t–)
{
solve();
cout<<endl;
}
return 0;
}


C. Phoenix and Towers  
 time limit per test2 seconds  
 memory limit per test256 megabytes  
 inputstandard input  
 outputstandard output  
 Phoenix has n blocks of height h1,h2,…,hn, and all hi don’t exceed some value x. He plans to stack all n blocks into m separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than x.


Please help Phoenix build m towers that look beautiful. Each tower must have at least one block and all blocks must be used.


Input  
 The input consists of multiple test cases. The first line contains an integer t (1≤t≤1000) — the number of test cases.


The first line of each test case contains three integers n, m, and x (1≤m≤n≤105; 1≤x≤104) — the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively.


The second line of each test case contains n space-separated integers (1≤hi≤x) — the heights of the blocks.


It is guaranteed that the sum of n over all the test cases will not exceed 105.


Output  
 For each test case, if Phoenix cannot build m towers that look beautiful, print NO. Otherwise, print YES, followed by n integers y1,y2,…,yn, where yi (1≤yi≤m) is the index of the tower that the i-th block is placed in.


If there are multiple solutions, print any of them.


Example  
 inputCopy  
 2  
 5 2 3  
 1 2 3 1 2  
 4 3 3  
 1 1 2 3  
 outputCopy  
 YES  
 1 1 1 2 2  
 YES  
 1 2 2 3  
 Note  
 In the first test case, the first tower has height 1+2+3=6 and the second tower has height 1+2=3. Their difference is 6−3=3 which doesn’t exceed x=3, so the towers are beautiful.


In the second test case, the first tower has height 1, the second tower has height 1+2=3, and the third tower has height 3. The maximum height difference of any two towers is 3−1=2 which doesn’t exceed x=3, so the towers are beautiful.


题意:给定三个数n,m,x和一个长度为n的数组a[i],每一个元素表示一个高度,其中m代表总共有m座塔要修建,问你要如何分配这个数组  
 ,使得m座塔相邻两座之间高度差的绝对值不大于x。输出是一个长度为n的序列,序列第i位的值(1~m之间)表示a[i]的位置  
 每一次决策都贪心选择当前状态下高度最低的塔即可(可用set或者优先队列模拟这个过程);最终遍历一遍塔的高度,若符合要求输出结果序列  
 代码:



#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 50;
#define reset(x) memset(x, 0, sizeof(x))
#define Q_in_out ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> P;
int a[N];
int solve()
{
reset(a);
int m,n,x;
cin>>n>>m>>x;
set

res; //m座塔
for(int i=1;i<=m;i++)
res.insert(pair<int,int>(0,i));//初始化,first表示高度,i表示位置
vectorans; //存放结果序列
for(int i=0;i<n;i++){
cin>>a[i];
auto tmp=*res.begin(); //取高度最小的塔,将a[i]放在塔顶,修改塔的高度后再插入res中
res.erase(tmp);
ans.push_back(tmp.second);
tmp.first+=a[i];
res.insert(tmp);
}
int f=0;
auto it=res.begin();
int cmp=it->first;
it++;
for(;it!=res.end();it++){
if(abs(it->first-cmp)>x){
f=1;
break;
}
cmp=it->first;
}
if(f) cout<<“NO”;
else{
cout<<“YES”<<endl;
for(int i=0;i<ans.size();i++)
cout<<ans[i]<<’ ';
}
return 0;
}
int main()
{
Q_in_out
int t;
cin >> t;
// t=1;
while (t–)
{
solve();
cout<<endl;
}
return 0;
}


D. Phoenix and Socks  
 time limit per test2 seconds  
 memory limit per test256 megabytes  
 inputstandard input  
 outputstandard output  
 To satisfy his love of matching socks, Phoenix has brought his n socks (n is even) to the sock store. Each of his socks has a color ci and is either a left sock or right sock.


Phoenix can pay one dollar to the sock store to either:


recolor a sock to any color c′ (1≤c′≤n)  
 turn a left sock into a right sock  
 turn a right sock into a left sock  
 The sock store may perform each of these changes any number of times. Note that the color of a left sock doesn’t change when it turns into a right sock, and vice versa.


A matching pair of socks is a left and right sock with the same color. What is the minimum cost for Phoenix to make n/2 matching pairs? Each sock must be included in exactly one matching pair.


Input  
 The input consists of multiple test cases. The first line contains an integer t (1≤t≤1000) — the number of test cases.


The first line of each test case contains three integers n, l, and r (2≤n≤2⋅105; n is even; 0≤l,r≤n; l+r=n) — the total number of socks, and the number of left and right socks, respectively.


The next line contains n integers ci (1≤ci≤n) — the colors of the socks. The first l socks are left socks, while the next r socks are right socks.


It is guaranteed that the sum of n across all the test cases will not exceed 2⋅105.


Output  
 For each test case, print one integer — the minimum cost for Phoenix to make n/2 matching pairs. Each sock must be included in exactly one matching pair.


Example  
 inputCopy  
 4  
 6 3 3  
 1 2 3 2 2 2  
 6 2 4  
 1 1 2 2 2 2  
 6 5 1  
 6 5 4 3 2 1  
 4 0 4  
 4 4 4 3  
 outputCopy  
 2  
 3  
 5  
 3  
 Note  
 In the first test case, Phoenix can pay 2 dollars to:


recolor sock 1 to color 2  
 recolor sock 3 to color 2  
 There are now 3 matching pairs. For example, pairs (1,4), (2,5), and (3,6) are matching.  
 In the second test case, Phoenix can pay 3 dollars to:


turn sock 6 from a right sock to a left sock  
 recolor sock 3 to color 1  
 recolor sock 4 to color 1  
 There are now 3 matching pairs. For example, pairs (1,3), (2,4), and (5,6) are matching.  
 题意:给定三个整数n,l,r,其中n表示总共n只袜子(n是偶数),l表示有l只左脚,r表示有r只右脚(袜子分左右的吗…?)然后给定  
 一个数组,长度为n,前l个数是左脚袜子的颜色,后r个数是右脚袜子的颜色,每次操作你可以花费一元钱把左脚换成右脚(或者相反)  
 ,或者花费一元钱改变袜子的颜色(任意颜色),求最少的花费使得你手上所有袜子配对(配对即是左右脚袜子颜色相同,表示颜色的数字范围为1~n)  
 既然要配对,那就先把左右脚的袜子已经配对好的给拿出来,减少后面的计算,假设左脚袜子比右脚多(这个无所谓,左右操作是对称的)  
 然后遍历一遍所有的颜色,由于事先统计好了左脚袜子各种颜色,故在处理颜色i的时候,假设i有x只,并且左脚袜子比右脚多y只(y一定是偶数)  
 我们需要取x/2和y/2的最小值,设为tp,因为最大只需要将y/2只袜子换成右脚,若x/2比y/2大,我们不能将x/2全给换成右脚,然后将左脚袜子的数量减掉tp  
 (这样做其实就等价于从左脚袜子挑出来几双配对好的袜子拿出去),知道最后,左脚还剩l只,右脚还剩r只,且l和r全都不能配对,l也不一定等于r,因为我们上述的  
 操作都是将左脚袜子有两只以上相同颜色的袜子给挑出来了,如果左脚袜子的颜色各不相同,我们也没有急着换成右脚,最终,我们花费加上(l+r)/2和(l-r)/2  
 前者的费用是用来将左脚和右脚颜色换统一,后者费用是将多出来的左脚换成右脚。(光用文字描述可能不好理解,可以用样例边算边理解)  
 代码:



#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 50;
#define reset(x) memset(x, 0, sizeof(x))
#define Q_in_out ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> P;
int a[N],cntl[N],cntr[N];
int solve()
{
reset(a);reset(cntl);reset(cntr);
int n,l,r;
cin>>n>>l>>r;
for(int i=0;i<n;i++){
cin>>a[i];
if(i<l) cntl[a[i]]++;
else cntr[a[i]]++;
}
for(int i=1;i<=n;i++){
int mx=min(cntl[i],cntr[i]);
cntl[i]-=mx;
cntr[i]-=mx;
l-=mx;r-=mx;
}
if(l<r){ //假设左脚袜子多
swap(l,r);
swap(cntl,cntr);
}
int res=0;
for(int i=1;i<=n;i++){
int spl=(l-r)/2; //最多需要换的数量
int cd=cntl[i]/2; //颜色i袜子的数量,若i小于2就不用换
int tp=min(cd,spl);
res+=tp; //tp表示的是有tp只袜子换到右脚了
l-=2*tp; //左脚袜子减掉(tp只换到右脚,左脚也有tp只颜色相同的,把这2*tp只袜子给拿出去了)
}

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

0280764)]
[外链图片转存中…(img-2VaZBVQ0-1715570280764)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值