UVa11292 - Dragon of Loowater (贪心+模拟)
生活随笔
收集整理的這篇文章主要介紹了
UVa11292 - Dragon of Loowater (贪心+模拟)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目鏈接
分析:
能力強的騎士開高價是合理的
但是不能大材小用,所以我們直接貪心
把龍和騎士排序之后,
一個一個砍就行了
當(dāng)然,不能砍掉“當(dāng)前需要砍掉的頭”的騎士我們就不用再考慮ta了
tip
成功的砍掉一個頭之后,不要忘了把騎士的指針向后推移一位
附圖:
當(dāng)然,還有一種更優(yōu)美的寫法:
//這里寫代碼片 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm>using namespace std;int n,m; int dragon[20010],knight[20010]; int ans=0;int main() {while (scanf("%d%d",&n,&m)!=EOF&&n&&m){for (int i=1;i<=n;i++) scanf("%d",&dragon[i]);for (int i=1;i<=m;i++) scanf("%d",&knight[i]);if (n>m) {printf("Loowater is doomed!\n");continue;}sort(dragon+1,dragon+1+n);sort(knight+1,knight+1+m);ans=0;int cnt=1;for (int i=1;i<=m;i++)if (knight[i]>=dragon[cnt]){ans+=knight[i];if (++cnt>n) break;}if (cnt<n) printf("Loowater is doomed!\n");else printf("%d\n",ans);}return 0; }轉(zhuǎn)載于:https://www.cnblogs.com/wutongtong3117/p/7673026.html
總結(jié)
以上是生活随笔為你收集整理的UVa11292 - Dragon of Loowater (贪心+模拟)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LoggerOne
- 下一篇: 170329、用 Maven 部署 wa