字节跳动2021秋招开发工程师笔试
头条校招
时间限制: 3000MS
内存限制: 589824KB
题目描述:
头条的2017校招开始了!为了这次校招,我们组织了一个规模宏大的出题团队,每个出题人都出了一些有趣的题目,而我们现在想把这些题目组合成若干场考试出来,在选题之前,我们对题目进行了盲审,并定出了每道题的难度系统。一场考试包含3道开放性题目,假设他们的难度从小到大分别为a,b,c,我们希望这3道题能满足下列条件: a<=b<=c b-a<=10 c-b<=10 所有出题人一共出了n道开放性题目。现在我们想把这n道题分布到若干场考试中(1场或多场,每道题都必须使用且只能用一次),然而由于上述条件的限制,可能有一些考试没法凑够3道题,因此出题人就需要多出一些适当难度的题目来让每场考试都达到要求,然而我们出题已经出得很累了,你能计算出我们最少还需要再出几道题吗?
输入描述
输入的第一行包含一个整数n,表示目前已经出好的题目数量。 第二行给出每道题目的难度系数d1,d2,…,dn。 数据范围 对于30%的数据,1 ≤ n,di ≤ 5; 对于100%的数据,1 ≤ n ≤ 10^5,1 ≤ di ≤ 100。 在样例中,一种可行的方案是添加2个难度分别为20和50的题目,这样可以组合成两场考试:(20 20 23)和(35,40,50)。
输出描述
输出只包括一行,即所求的答案。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int problem[] = new int[n];
for(int i=0;i<n;i++){
problem[i] = scanner.nextInt();
}
Arrays.sort(problem);
int cnt = 0;
int exam = 1;
for(int i=1;i<n;i++){
int diff = problem[i] - problem[i - 1];
if(exam == 0){
exam++;
}
else if(diff >10){
if(exam==1 && diff <=20){
cnt++;
exam = 0;
} else if (exam == 1) {
cnt+=2;
exam = 1;
} else if (exam == 2) {
cnt++;
exam = 1;
}
}else {
exam ++;
}
if(exam==3)exam = 0;
}
cnt += 3-exam;
System.out.println(cnt);
}
}
异或
时间限制: 3000MS
内存限制: 589824KB题目描述:
给定整数m以及n各数字A1,A2,…An,将数列A中所有元素两两异或,共能得到n(n-1)/2个结果,请求出这些结果中大于m的有多少个。
输入描述
第一行包含两个整数n,m.
第二行给出n个整数A1,A2,…,An。
数据范围
对于30%的数据,1 <= n, m <= 1000
对于100%的数据,1 <= n, m, Ai <= 10^5
输出描述
输出仅包括一行,即所求的答案
import java.util.*;
public class Main{
static int n,m;
class Tree{
int num;
Tree[] child;
public Tree(){
num=0;
child=new Tree[2];
}
}
public static Tree root;
public static void main(String[] args){
Scanner in=new Scanner(System.in);
n=in.nextInt();
m=in.nextInt();
long ans=0;
int[] arr=new int[n];
root=new Main().new Tree();
for(int i=0;i<n;i++){
arr[i]=in.nextInt();
insert(arr[i]);
}
for(int i=0;i<n;i++)
ans+=query(root,arr[i],19);
System.out.println(ans/2);
}
public static void insert(int x){
int len=0;
int[] stat=new int[20];
while(x>0){
stat[len++]=x&1;
x>>=1;
}
Tree now=root;
for(int i=19;i>=0;i--){
int t=stat[i];
if(now.child[t]==null)
now.child[t]=new Main().new Tree();
now=now.child[t];
now.num++;
}
}
public static long query(Tree root,int x,int index){
if(root==null)
return 0;
int res=0;
Tree now=root;
for(int i=index;i>=0;i--){
int a=x>>i&1;
int b=m>>i&1;
if(a==1 && b==1){
if(now.child[0]==null)
return 0;
now=now.child[0];
} else if(a==0 && b==1){
if(now.child[1]==null)
return 0;
now=now.child[1];
} else if(a==1 && b==0){
long p=query(now.child[1],x,i-1);
long q=now.child[0]==null?0:now.child[0].num;
return p+q;
} else{
long p=query(now.child[0],x,i-1);
long q=now.child[1]==null?0:now.child[1].num;
return p+q;
}
}
return 0;
}
}