百度2021校招C++/PHP研发工程师笔试卷(9月3日)编程题解题报告
2020.9.3
序言
- 单选题有15道,不定项选择题有5道(3份),选择题涉及的知识面很广,包括计网,Linux命令(df/du), AVL,二叉树,操作系统(生产者-消费者问题),数据结构,C++虚函数,java代码…
- 编程题有3道,每道20份
第一题,铺地毯
题目描述:
T组数据,T<=1000, 在长度为L的走廊铺地毯,铺n块,每块的范围是[Left_i,Right_i]), n<=1000, 问最后每个位置的地板上有多少张地毯铺在上面。
思路:差分。[left,right]铺地板,那么差分数组a[left]++,a[right+1]–
#include <bits/stdc++.h>
using namespace std;
const int N = 2000;
int a[N],b[N];
int main(void) {
int T,L,n,x,y;
cin>>T;
while(T--) {
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
scanf("%d%d",&L,&n);
while(n--) {
scanf("%d%d",&x,&y);
a[x]+=1;
a[y+1]-=1;
}
for(int i=1;i<=L;++i) {
b[i] = b[i-1]+a[i];
}
for(int i=1;i<L;++i)
printf("%d ",b[i]);
printf("%d\n"