美团2021校招笔试-编程题(通用编程试题,第10场)

 1.

#include <iostream>
#include<algorithm>
#include<math.h>
using namespace std;
int a[50005];
int main() {
    int n,x,y;
    scanf("%d%d%d",&n,&x,&y);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    sort(a,a+n);
    int m=-1;
    for(int i=1;i<=a[n-1];i++)
    {
        int numx=0;
        int numy=0;
        for(int j=0;j<n;j++)
        {
            if(a[j]<=i)
            {
                numx++;
            }
            else if(a[j]>i)
            {
                numy++;
            }
        }
        if(numx>=x &&numx<=y && numy>=x&&numy<=y)
        {
                 m=i;
                 break;
        }
    }
    printf("%d\n",m);
    return 0;

}
// 64 位输出请用 printf("%lld")

2.

#include <iostream>
#include<algorithm>
using namespace std;
int a[20005];
int main() {
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    sort(a,a+n);
    int num=0;
    for(int i=1;i<=n;i++)
    {
        num+=abs(i-a[i-1]);
    }
    printf("%d\n",num);
    return 0;
}
// 64 位输出请用 printf("%lld")

3.

#include <cstdio>
#include <iostream>
#include<queue>
using namespace std;
char a[500005];
struct node{
	int type;
	int num;
};
struct cmp1{   
    bool operator ()(node &a,node &b){
    	if(a.type==b.type)
    	{
    		return a.num>b.num;
		}
		else
		{
			return a.type>b.type;
		}
        
    }
};



int main() {
    int T;
    scanf("%d",&T);
    while(T)
    {
        int n;
        priority_queue<node,vector<node>,cmp1>que;
priority_queue<node,vector<node>,cmp1>que1;
        scanf("%d",&n);
        getchar();
        for(int i=0;i<n;i++)
        {
            scanf("%c",&a[i]);
            pair<int,int> p;
            if(a[i]=='0')
            {
            	node s[2];
            	s[0].type=a[i]-'0';
            	s[0].num=i+1;
            	que.push(s[0]);
            	
			}
			else if(a[i]=='1')
			{
				node s[2];
            	s[0].type=a[i]-'0';
            	s[0].num=i+1;
            	que1.push(s[0]);
			}
        }
        // for(int i=0;i<n;i++)
        // {
        // 	printf("%c",a[i]);
		// }
        int M;
        scanf("%d",&M);
        getchar();
        for(int i=0;i<M;i++)
        {
            char c;
            scanf("%c",&c);
            if(c=='M')
            {//1优先 
            	if(!que1.empty())
                {
                	int x=que1.top().type;
			        int y=que1.top().num;
			      
			        	que1.pop();
			        	printf("%d\n",y);
				}
				else{
					int x=que.top().type;
			        int y=que.top().num;
			        que.pop();
			         node s[2];
		        	s[0].type=1;
		        	s[0].num=y;
            		que1.push(s[0]);
			        
		        	printf("%d\n",y);
					
				}
		        
                
            }
            else{
            //	0优先 
                if(!que.empty())
                {
                	int x=que.top().type;
			        int y=que.top().num;
			      
			        	que.pop();
			        	  node s[2];
			        	s[0].type=1;
			        	s[0].num=y;
		        		que1.push(s[0]);
			        	printf("%d\n",y);
				}
				else{
					int x=que1.top().type;
			        int y=que1.top().num;
		        	que1.pop();
		        	printf("%d\n",y);
					
				}
		        
			}
        }
        T--;
    }
    
	return 0;
}
// 64 位输出请用 printf("%lld")

4.

#include <iostream>
#include<string.h>
using namespace std;
int vis[305][305][305];
int val[305];
int maxx=9*1e9;

int dfs(int l,int r,int root)
{
    if(l>r)
    {
        return 0;
    }
    if(vis[l][r][root]!=-1)
    {
        return vis[l][r][root];
    }
    int ans=maxx;
    for(int i=l;i<=r;i++)
    {
        int lnum=dfs(l,i-1,i);
        int rnum=dfs(i+1,r,i);
        ans=min(ans,lnum+rnum+val[i]*val[root]);
    }
    vis[l][r][root]=ans;
    return ans;

}
int main() {
      int n;
      scanf("%d",&n);
      for(int i=1;i<=n;i++)
      {
            scanf("%d",&val[i]);
      }
      memset(vis,-1,sizeof(vis));
      int k=dfs(1,n,0);
      printf("%d\n",k);

}

// 64 位输出请用 printf("%lld")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值