招行提前批笔试
第一题:1-n个人排队,现在站队为数组num1;每个人都可以往左走任意个位置,问最小需要走几个人,可以走成目标数组的样子来拍照?
例子:6
4 6 3 2 1 5
4 2 3 5 6 1
输出3次;
1 #include <bits/stdc++.h> 2 using namespace std; 3 int main() { 4 int length; 5 cin>> length; 6 int answer = 0; 7 set<int>ans; 8 stack<int> stack1,stack2; 9 for(int i= 0;i<length;i++) { 10 int num; 11 cin >> num; 12 stack1.push(num); 13 } 14 for(int i= 0;i<length;i++) { 15 int num; 16 cin >> num; 17 stack2.push(num); 18 } 19 //用两个栈来保存两个队伍;stack2为需排成的队伍; 20 while(!stack1.empty()&&!stack2.empty()){ 21 int numofs1 = stack1.top(); 22 int numofs2 = stack2.top(); 23 //不需要移动,出栈 24 if(numofs1 == numofs2){ 25 stack1.pop(); 26 stack2.pop(); 27 } 28 else{ 29 //已经移动过了,在最合适的位置 30 if(ans.count(numofs2)) stack2.pop(); 31 else { 32 //没移动过,需要移动到合适位置,计数加1,并且从比较栈出栈。 33 answer += 1; 34 ans.insert(numofs1); 35 stack1.pop(); 36 } 37 } 38 } 39 cout << ans.size(); 40 return 0; 41 }
第二题:摆棋子游戏,一共N(0<=N<100000)个棋子,放在棋盘(a*a(0<a<=1000)上,当棋子上左右有棋子而下无棋子时,是不合规则的,需要添加棋子;使其合法。每加一个棋子,要输出目前需要加入棋子让它合法的最小个数。如果合法,就输出0;注意:每次加入的额外的棋子可以超出棋盘边界即不需要满足(0<=x<1000),且对下一个棋子加入的情况不影响,只对本回合的棋盘有影响)
输入:第一行是棋子个数N,接下来的N行是棋子的x,y位置,用空格隔开;
输出:每个棋子落下后要使棋盘符合规则的最小额外加入棋子个数。
例子:
1 9 2 0 1 3 1 0 4 1 1 5 1 2 6 2 1 7 2 2 8 3 1 9 3 2 10 4 1
输出:
0 0 0 1 0 0 1 2 4
代码:
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int dx[4] ={0,0,-1,1};//左右上下 4 const int dy[4] = {-1,1,0,0}; 5 6 //注意这里只能值传递,不要引用传递,因为上个回合不能影响现在的回合; 7 int findmin(queue<pair<int,int>> chessboard,set<pair<int,int>> set1){ 8 int ans =0; 9 while(!chessboard.empty()){ 10 int f = 0; 11 pair<int,int> temp = chessboard.front(); 12 chessboard.pop(); 13 //拿出这个结点的上下左右; 14 int x0 = temp.first+dx[0];int x1 = temp.first+dx[1]; int x2 = temp.first+dx[2];int x3 = temp.first+dx[3]; 15 int y0 = temp.second+dy[0];int y1 = temp.second+dy[1];int y2 = temp.second+dy[2];int y3 = temp.second+dy[3]; 16 vector<int> xx ={x0,x1,x2,x3}; 17 vector<int> yy ={y0,y1,y2,y3}; 18 int cn0 = set1.count({x0,y0});int cn1 = set1.count({x1,y1}); 19 int cn2 = set1.count({x2,y2});int cn3 = set1.count({x3,y3}); 20 int sum = cn0+cn1+cn2+cn3; 21 //判断是不是有三个棋子都在; 22 if(sum==3) 23 { 24 ans++; 25 for(int i=0;i<4;i++) { 26 if(!set1.count({xx[i],yy[i]})) { 27 chessboard.push({xx[i], yy[i]}); 28 set1.insert({xx[i], yy[i]}); 29 } 30 } 31 32 } 33 } 34 return ans; 35 } 36 37 38 int main() { 39 int n; 40 cin >>n; 41 //用于存现在的棋盘的棋子的位置 42 queue<pair<int,int>> chessboard; 43 //用于查找现在棋盘上合不合法; 44 set<pair<int,int>> set1; 45 while(n-->0){ 46 int x,y; 47 cin >>x>>y; 48 chessboard.push({x,y}); 49 set1.insert({x,y}); 50 //返回每次加棋子的结果 51 int res = findmin(chessboard,set1); 52 cout << res ; 53 } cout <<endl; 54 return 0; 55 }
2021第一题:
1.欢迎入营
高中数学的组合题目,不是很难。
1 #include<bits/stdc++.h> 2 using namespace std; 3 static bool cmp(int a,int b){ 4 return a>b; 5 } 6 int main(){ 7 const int mod = 100000007; 8 int n; 9 cin >>n; 10 long long ans = 1; 11 vector<long long > a; 12 vector<long long > b; 13 int m = n; 14 while(m-->0){ 15 long long num; 16 cin >>num; 17 a.emplace_back(num); 18 } 19 while(n-->0){ 20 long long num; 21 cin >>num; 22 b.emplace_back(num); 23 } 24 sort(a.begin(),a.end(),cmp); 25 sort(b.begin(),b.end()); 26 for(int i =0;i< a.size();i++){ 27 int lownum = lower_bound(b.begin(),b.end(),a[i])-b.begin(); 28 int countnum = a.size()-lownum; 29 ans = ans*(countnum-i); 30 ans %= mod; 31 } 32 cout << ans <<endl; 33 return 0; 34 }
第二题:
第三题:
例子: //输入 5 2 20 3 16 9 5 //输出 3
分析:对以每个体重为队伍基准体重,算出能够在满足条件下,最多有多少人,为队1的人数;然后选满足条件的队2的人数,相加的最大值,即为结果。
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int main() { 5 int n,k; 6 cin >> n>>k; 7 //放每个人的体重 8 vector<int> wight; 9 int m = n; 10 while(m-->0){ 11 int w = 0; 12 cin >>w; 13 wight.emplace_back(w); 14 } 15 sort(wight.begin(),wight.end()); 16 vector<int> peoplenum(n,0); 17 for(int i = 0;i<n;i++){ 18 int index = upper_bound(wight.begin(),wight.end(),wight[i]+k)-wight.begin(); 19 peoplenum[i] = index-i; 20 } 21 int res = 0; 22 for(int i = 0;i<n;i++){ 23 //在wight[i]这个体重下,有多少人能参赛 24 int que1 = peoplenum[i]; 25 //【wight[i]+people[i],6000)最大的能参赛的人数; 26 int que2 = *max_element(peoplenum.begin()+i+peoplenum[i],peoplenum.end()); 27 res = max(res,que2+que1); 28 } 29 cout <<res <<endl; 30 return 0; 31 }