解题报告 『活动安排(贪心)』
生活随笔
收集整理的這篇文章主要介紹了
解题报告 『活动安排(贪心)』
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
原題地址
第一次提交WA了兩個(gè)點(diǎn),因此特地寫一篇博客。
?
錯(cuò)誤見代碼:
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (register int i = (a); i <= (b); i++)const int maxn = 1e3 + 5;int n, ans = 0;struct node {int start, end; }work[maxn];struct cmp {inline bool operator()(const node &a, const node &b) {return a.end < b.end;} };int read() {int x = 0, flag = 0;char ch = ' ';while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();if (ch == '-') {flag = 1;ch = getchar();}while (ch >= '0' && ch <= '9') {x = (x << 1) + (x << 3) + ch - '0';ch = getchar();}return flag ? -x : x; }void write(int x) {if (x < 0) {putchar('-');x = -x;}if (x > 9) write(x / 10);putchar(x % 10 + '0'); }int main() {n = read();rep(i, 1, n) work[i].start = read(), work[i].end = read();sort(work + 1, work + n + 1, cmp());int now = -1;rep(i, 1, n) {if (work[i].start > now) {//此處應(yīng)改為work[i].start >= now.條件允許的情況下,一個(gè)活動(dòng)結(jié)束后可以馬上開始下一個(gè)活動(dòng). ans++;now = work[i].end;}}write(ans);return 0; } View Code轉(zhuǎn)載于:https://www.cnblogs.com/Kirisame-Marisa/p/10494104.html
總結(jié)
以上是生活随笔為你收集整理的解题报告 『活动安排(贪心)』的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 取名 该问题是属于哪个板块的?回答: 0
- 下一篇: WinRAR 5.70 下载、安装和广告