生活随笔
收集整理的這篇文章主要介紹了
【贪心】P1056 排座椅
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
https://www.luogu.com.cn/problem/P1056
考點(diǎn):貪心、排序
題意:
有M行N列的格子,D只長度為2的蟲子(可橫可豎),橫向縱向分別可以切K,L刀,問怎樣切可以切死最多的蟲子。
。。。
其實(shí)原題是用走廊拆開上課交頭接耳的學(xué)生,不知道為啥我覺得翻譯成上面的文字更好理解。
這個(gè)題的主要坑點(diǎn)是這一句:
就是說不僅要最優(yōu)切法,而且要按行列號升序輸出。
解法:
很基礎(chǔ)的貪心+排序。輸入數(shù)據(jù)的時(shí)候,每條“蟲子”都砍一刀,記錄在該位置砍了一刀,最后看看哪個(gè)位置砍的刀數(shù)最多,那么這個(gè)位置就要優(yōu)先選擇。以橫向砍為例,排序后就確定了前K個(gè)要砍的位置,然后給這K個(gè)再按行號升序排序,就可以輸出了。
總共4次排序,數(shù)據(jù)量不大,可以接受。
#include <bits/stdc++.h>
using namespace std
;
using PII
= pair
<int,int>;
int M
,N
,K
,L
,D
;
PII RowCut
[1005];
PII ColCut
[1005];
int main() {cin
>> M
>> N
>> K
>> L
>> D
;for (int i
= 0; i
< D
; i
++) {int a
,b
,c
,d
; cin
>> a
>> b
>> c
>> d
;if (a
== c
) {ColCut
[min(b
, d
)].first
++;} else {RowCut
[min(a
, c
)].first
++;}}for (int i
= 1; i
< M
; i
++) RowCut
[i
].second
= i
;for (int i
= 1; i
< N
; i
++) ColCut
[i
].second
= i
;sort(RowCut
+ 1, RowCut
+ M
, [](PII
&a
, PII
&b
){ return a
.first
!= b
.first
? a
.first
> b
.first
: a
.second
< b
.second
;});sort(ColCut
+ 1, ColCut
+ N
, [](PII
&a
, PII
&b
){ return a
.first
!= b
.first
? a
.first
> b
.first
: a
.second
< b
.second
;});sort(RowCut
+ 1, RowCut
+ 1 + K
, [](PII
&a
, PII
&b
){return a
.second
!= b
.second
? a
.second
< b
.second
: a
.first
> b
.first
;});sort(ColCut
+ 1, ColCut
+ 1 + L
, [](PII
&a
, PII
&b
){return a
.second
!= b
.second
? a
.second
< b
.second
: a
.first
> b
.first
;});for (int i
= 1; i
<= K
; i
++) {if (i
!= K
) cout
<< RowCut
[i
].second
<< " ";else cout
<< RowCut
[i
].second
<< endl
;}for (int i
= 1; i
<= L
; i
++) {if (i
!= L
) cout
<< ColCut
[i
].second
<< " ";else cout
<< ColCut
[i
].second
;}return 0;
}
這題沒有任何難度,卡了是因?yàn)闆]仔細(xì)讀題!!
總結(jié)
以上是生活随笔為你收集整理的【贪心】P1056 排座椅的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。