【字节跳动】2020校园招聘研发岗位第四次在线笔试AC代码

都是2019-9-15 16:00-18:00笔试的兄die啊。

第一题,一开始自己手写二分写挂了,菜的抠脚,后来调库才过的,题目备注说要O(n^2)的复杂度,但是我是用O(N^2*log(n))过得,logN其实可以看成是一个常数23333.

#include<bits/stdc++.h>
#define LL long long
using namespace std;
int n;
LL a[10005],k;
int main()
{
	while(~scanf("%d",&n))
	{
		int ans=0;
		for(int i=0;i<n;i++)
		{
			scanf("%lld",&a[i]);
		}
		scanf("%lld",&k);
		sort(a,a+n);
		for(int i=0;i<n;i++)
		{
			for(int j=i+1;j<n;j++)
			{
				LL sum = a[i]+a[j];
				int tmp=lower_bound(a+j+1,a+n,k-sum)-(a+j+1);
				ans+=tmp;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

第二题一开始模拟只过了70%,考试快结束才100%通过的

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
struct node
{
	long long t,c;
}f[N];
bool cmp(node a,node b)
{
	return a.t<b.t;
}
int n;
queue<node>q;
int main()
{
	cin.sync_with_stdio(false);
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>f[i].t>>f[i].c;
	}
	sort(f+1,f+n+1,cmp);
	long long st=f[1].t,ed=f[1].t;
	long long maxNum=f[1].c;
	q.push(f[1]);
	int tot=2;
	long long nowNum=0;
	while(!q.empty()||tot<=n)
	{
		node tmp=q.front();
		q.pop();
		st=max(st,tmp.t);
		ed=st+tmp.c;
		long long files=tmp.c;
		while(tot<=n&&ed>f[tot].t)
		{
			nowNum+=f[tot].c;
			files=ed-f[tot].t;
			maxNum=max(maxNum,nowNum+files);
			q.push(f[tot++]);
		}
		if(!q.empty())
		{
			nowNum-=q.front().c;
		}
		if(tot<=n&&q.empty())
		{
			q.push(f[tot++]);
			nowNum=0;
		}
		st=ed;
	}
	cout<<ed<<" "<<maxNum<<endl;
	return 0;
}

第三题区间DP

#include<bits/stdc++.h>
using namespace std;
const int maxn=3005;
int a[maxn],dp[maxn][maxn],sum[maxn];
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		sum[i] = sum[i-1]+a[i];
		dp[i][i]=a[i];
	}
	for(int l=2;l<=n;l++)
	{
		for(int i=1;i<=n;i++)
		{
			int j=i+l-1;
			if(j>n) break;
			int t1=(sum[j-1]-sum[i-1])-dp[i][j-1]+a[j];
			int t2=(sum[j]-sum[i])-dp[i+1][j]+a[i];
			dp[i][j]=max(t1,t2);
			
		}
	}
	printf("%d\n",dp[1][n]);
	return 0;
}

第四题背包

#include<bits/stdc++.h>
using namespace std;
const int N=510;
long long a[N],b[N],w[N],tot=0,n,v,x,y,z;
long long dp[N*N];
int main()
{
	long long ans=0,sum=0;
	cin>>n>>v;
	for(int i=1;i<=n;i++)
	{
		cin>>x>>y>>z;
		sum+=z;
		if(y<=(x-y))
		{
			ans+=z;
			v+=x-y-y;
		}
		else 
		{
			a[++tot]=x;
			b[tot]=y-(x-y);
			w[tot]=z;
		}
	}
	for(int i=1;i<=tot;i++)
	{
		for(int j=v;j>=b[i];j--)
		{
			dp[j]=max(dp[j],dp[j-b[i]]+w[i]);
		}
	}
	cout<<ans+dp[v]<<endl;
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值