字节跳动2018校招测试开发方向(第二批)

真题链接:https://www.nowcoder.com/test/8537269/summary

第一题:

 1 #include"iostream"
 2 #include"algorithm"
 3 #include"stdio.h"
 4 using namespace std;
 5 const int MAXN=300005;
 6 struct node
 7 {
 8     int value;
 9     int index;
10 };
11 node users[MAXN];
12 int  n,q;
13 
14 bool Cmp(const node &a,const node &b)
15 {
16     if(a.value!=b.value)
17         return a.value<b.value;
18     return a.index<b.index;
19 }
20 
21 int BinarySearch(int l,int r,int k)
22 {
23     while(l<=r)
24     {
25         int mid=(l+r)/2;
26         if(users[mid].value==k)
27             return mid;
28         if(users[mid].value<k)
29             l=mid+1;
30         else
31             r=mid-1;
32     }
33     return -1;
34 }
35 
36 int CountKInRange(int l,int r,int k)
37 {
38     int kIndex=BinarySearch(1,n,k);
39     if(kIndex==-1)
40         return 0;
41     int result=0,tempIndex=kIndex;
42     while(tempIndex>=1&&users[tempIndex].value==k)
43     {
44         if(users[tempIndex].index>=l&&users[tempIndex].index<=r)
45             result++;
46         tempIndex--;
47     }
48     tempIndex=kIndex+1;
49     while(tempIndex<=n&&users[tempIndex].value==k)
50     {
51         if(users[tempIndex].index>=l&&users[tempIndex].index<=r)
52             result++;
53         tempIndex++;
54     }
55     return result;
56 }
57 int main()
58 {
59     while(scanf("%d",&n)==1)
60     {
61         for(int i=1;i<=n;i++)
62         {
63             scanf("%d",&users[i].value);
64             users[i].index=i;
65         }
66         sort(users+1,users+1+n,Cmp);
67 
68 //        for(int i=1;i<=n;i++)
69  //           cout<<users[i].value<<' '<<users[i].index<<endl;
70         cin>>q;
71         int l,r,k;
72         while(q--)
73         {
74             scanf("%d%d%d",&l,&r,&k);
75             cout<<CountKInRange(l,r,k)<<endl;
76         }
77     }
78     return 0;
79 }
View Code

 第二题:

 1 #include"iostream"
 2 #include"stdio.h"
 3 #include"string.h"
 4 #include"math.h"
 5 using namespace std;
 6 
 7 int color[55][10005];
 8 int colorIndex[55];
 9 int n,m,c;
10 
11 int Min(int a,int b)
12 {
13     return a<b?a:b;
14 }
15 
16 int GetMinDistance(int i,int j)
17 {
18     return Min(abs(i-j),n-abs(i-j));
19 }
20 
21 int CountInvalidColoor()
22 {
23     int result=0;
24     for(int i=1;i<=c;i++)
25     {
26         for(int j=0;j<colorIndex[i];j++)
27         {
28             if(GetMinDistance(color[i][j],color[i][(j+1)%colorIndex[i]])<=(m-1))
29             {
30                 result++;
31                 break;
32             }
33         }
34     }
35     return result;
36 }
37 int main()
38 {
39 
40     while(scanf("%d%d%d",&n,&m,&c)==3)
41     {
42         memset(colorIndex,0,sizeof(colorIndex));
43         for(int i=0;i<n;i++)
44         {
45             int numI,iColor;
46             scanf("%d",&numI);
47             for(int j=0;j<numI;j++)
48             {
49                 cin>>iColor;
50                 color[iColor][colorIndex[iColor]++]=i;
51             }
52         }
53         /*
54         for(int i=0;i<=c;i++)
55         {
56             for(int j=0;j<colorIndex[i];j++)
57                 cout<<color[i][j]<<' ';
58             cout<<endl;
59         }
60         */
61         cout<<CountInvalidColoor()<<endl;
62     }
63     return 0;
64 }
View Code

第三题: