头条笔试代码分享
/// test 1
#include <bits/stdc++.h>
using namespace std;
map<int,int >mmp;
map<int,int >vis;
int a[1000005];
int main(){
int n,k;
while(~scanf("%d %d",&n,&k)){
mmp.clear();
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
sort(a+1,a+n+1);
n = unique(a+1,a+n+1)-(a+1);
for(int i=1;i<=n;i++){
mmp[a[i]] = 1;
}
int ans = 0;
for(int i=1;i<=n;i++){
if(mmp[a[i]-k]){
ans++;
}
}
printf("%d\n",ans);
}
return 0;
}
/// test 2
#include <bits/stdc++.h>
using namespace std;
map<int,map<int,int> >mmp;
int main(){
int n;
while(~scanf("%d",&n)){
int s = 1,m = 1;mmp.clear();
queue<pair<pair<int,int>,int > >q;
q.push({{s,m},0});
mmp[1][1] = 1;
int ans = 0;
while(!q.empty()){
int ss = q.front().first.first;
int mm = q.front().first.second;
int step = q.front().second;
q.pop();
if(ss == n){
ans = step;
break;
}
if(ss > n) continue;
if(!mmp.count(ss+mm) || !mmp[ss+mm].count(mm)){
q.push({{ss+mm,mm},step+1});
mmp[ss+mm][mm] = 1;
}
if(!mmp.count(ss+ss) || !mmp[ss+ss].count(ss)){
q.push({{ss+ss,ss},step+1});
mmp[ss+ss][ss] = 1;
}
}
printf("%d\n",ans);
}
return 0;
}
/// test 3
nn = [
[
'66666',
'6...6',
'6...6',
'6...6',
'66666'
],[
'....6',
'....6',
'....6',
'....6',
'....6'
],[
'66666',
'....6',
'66666',
'6....',
'66666'
],[
'66666',
'....6',
'66666',
'....6',
'66666'
],[
'6...6',
'6...6',
'66666',
'....6',
'....6'
],[
'66666',
'6....',
'66666',
'....6',
'66666'
],[
'66666',
'6....',
'66666',
'6...6',
'66666'
],[
'66666',
'....6',
'....6',
'....6',
'....6'
],[
'66666',
'6...6',
'66666',
'6...6',
'66666'
],[
'66666',
'6...6',
'66666',
'....6',
'66666'
]
]
if __name__ == '__main__':
n = int(input())
for _ in range(n):
s = input()
ans = eval(s)
ans = str(ans)
s = [[],[],[],[],[]]
for i in ans:
x = int(i)
s[0].append(nn[x][0])
s[1].append(nn[x][1])
s[2].append(nn[x][2])
s[3].append(nn[x][3])
s[4].append(nn[x][4])
for i in range(0,5):
print('..'.join(s[i]));
/// test 4
只有70分 不贴代码了
都排序
比较平均值大小
if(totA/lenA 比较 totB/lenB) 转化为 if(totA*lenB = totB*lenA)了
然后大的给小的转
从小到大遍历
如果这个可以就ans++;
感觉可以的呀,但是为什么只有70分呀....5555
/// test 5
#include<bits/stdc++.h>
using namespace std;
const int N = 2000000+5;
const int INF = 0x3fffffff;
int dp[N];
int a,n,k,h;
int vis[N];
int solve(){
dp[0]=0;
for(int i=0; i<N/2; i++)for(int j=i+1; j<=i+h; j++)
if(vis[j])dp[i+2*(j-i)]=min(dp[i+2*(j-i)],dp[i]+1);
int ans=0;
for(int i=0; i<N; i++) if(dp[i]<=k)
ans=i;
return ans;
}
int main(){
scanf("%d%d%d",&n,&k,&h);
memset(dp,INF,sizeof(dp));
for(int i=0; i<n; i++){
scanf("%d",&a);
vis[a]++;
}
printf("%d\n",solve());
return 0;
}
事实又一次证明了,我就是一个ACM中的大菜逼,这种垃圾题都不能AK.活该失业.