生活随笔
收集整理的這篇文章主要介紹了
2020年牛客算法入门课练习赛1【完结】
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
感覺題挺不錯(cuò)的,自己也覺得做過很多題了,但是做這一套題還是有的題有些許不會。
目錄
- 第k小數(shù)【難度: 簡單 / 快排】
- 不平行的直線【難度: 簡單 / 數(shù)學(xué)】
- 丟手絹【難度: 一般 / 取尺法 雙指針】
- 二分【難度: 中 / 知識點(diǎn): 差分】
- 交換【難度: 中 / 求環(huán)】
第k小數(shù)【難度: 簡單 / 快排】
https://ac.nowcoder.com/acm/contest/5773/A
#include<bits/stdc++.h>
using namespace std
;
const int N
=1e7+10;
int a
[N
],t
,n
,k
;
inline int read(){int x
= 0, f
= 1;char ch
= getchar();while(ch
< '0' || ch
> '9'){if (ch
== '-')f
= -1;ch
= getchar();}while(ch
>= '0' && ch
<= '9'){x
= (x
<<1) + (x
<<3) + (ch
^48);ch
= getchar();}return x
* f
;
}
int main(void)
{t
=read();;while(t
--){n
=read(); k
=read();for(int i
=1;i
<=n
;i
++) a
[i
]=read();sort(a
+1,a
+n
+1);printf("%d\n",a
[k
]);}return 0;
}
不平行的直線【難度: 簡單 / 數(shù)學(xué)】
https://ac.nowcoder.com/acm/contest/5773/B
就是求不同的斜率的數(shù)量。
#include<bits/stdc++.h>
using namespace std
;
typedef pair
<int,int> PII
;
int gcd(int a
,int b
) {return b
?gcd(b
,a
%b
):a
;}
vector
<PII
>ve
;
int n
;
set
<PII
>st
;
int main(void)
{cin
>>n
;for(int i
=0;i
<n
;i
++){int a
,b
; cin
>>a
>>b
;ve
.push_back({a
,b
});}for(int i
=0;i
<ve
.size();i
++){for(int j
=i
+1;j
<ve
.size();j
++){int a
=ve
[i
].first
,b
=ve
[i
].second
;int c
=ve
[j
].first
,d
=ve
[j
].second
;int s1
=c
-a
,s2
=d
-b
; int temp
=gcd(s1
,s2
);st
.insert({s1
/temp
,s2
/temp
});}}cout
<<st
.size()<<endl
;return 0;
}
丟手絹【難度: 一般 / 取尺法 雙指針】
https://ac.nowcoder.com/acm/contest/5773/C
尺取法,用雙指針維護(hù),對于一個(gè)點(diǎn)我們的最大結(jié)果一定是盡可能的平分。
#include<iostream>
using namespace std
;
int a
[100010] = {0};int main(){int n
;cin
>> n
;int l
= 0, sum
= 0;for(int i
= 0; i
< n
; i
++){cin
>> a
[i
];sum
+= a
[i
];}int temp
= 0;int ans
= 0, jin
;for(int i
= 0; i
< n
; i
++){while(temp
< sum
/ 2){temp
+= a
[l
% n
];l
++;}jin
= min(temp
, sum
- temp
);ans
= max(ans
, jin
);temp
-= a
[i
];}cout
<< ans
<< endl
;return 0;
}
二分【難度: 中 / 知識點(diǎn): 差分】
https://ac.nowcoder.com/acm/contest/5773/D
居然是差分,太秒了。數(shù)據(jù)范圍很大,故直接用map來映射
#include<bits/stdc++.h>
using namespace std
;
const int N
=1e5+10;
const int inf
=INT_MAX
;
map
<int,int>mp
;
int n
=0;
void insert(int l
,int r
)
{mp
[l
]++;mp
[r
+1]--;
}
int main(void)
{cin
>>n
;for(int i
=0;i
<n
;i
++){int a
; char b
;cin
>>a
>>b
;if(b
=='.') insert(a
,a
);if(b
=='+') insert(-inf
,a
-1);if(b
=='-') insert(a
+1,inf
-1); }int ans
=-1e9,sum
=0;for(auto i
=mp
.begin();i
!=mp
.end();i
++){sum
+=i
->second
;ans
=max(ans
,sum
);}cout
<<ans
<<endl
;return 0;
}
交換【難度: 中 / 求環(huán)】
https://ac.nowcoder.com/acm/contest/5773/E
數(shù)據(jù)范圍很大故先離散化一下,再進(jìn)行。
很經(jīng)典的一個(gè)問題,我們可以用圖論的思路來解決。
我們的最終目的是n個(gè)自環(huán),cnt用來當(dāng)前有幾個(gè)環(huán),n-cnt就是我們還需要的環(huán)數(shù),即為答案。
#include<bits/stdc++.h>
using namespace std
;
const int N
=200010;
int s
[N
],temp
[N
];
int vis
[N
];
int n
,cnt
;
map
<int,int>mp
;
int main()
{cin
>>n
;for(int i
=1;i
<=n
;i
++) scanf("%d",&s
[i
]),temp
[i
]=s
[i
];sort(temp
+1,temp
+n
+1);for(int i
=1;i
<=n
;i
++) mp
[temp
[i
]]=i
;for(int i
=1;i
<=n
;i
++) s
[i
]=mp
[s
[i
]];for(int i
=1;i
<=n
;i
++){if(!vis
[s
[i
]]){for(int j
=s
[i
];!vis
[j
];j
=s
[j
]) vis
[j
]=1;cnt
++;}}cout
<<n
-cnt
;return 0;
}
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀
總結(jié)
以上是生活随笔為你收集整理的2020年牛客算法入门课练习赛1【完结】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。