都是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;
}