洛谷P4053 [JSOI2007]建筑抢修
生活随笔
收集整理的這篇文章主要介紹了
洛谷P4053 [JSOI2007]建筑抢修
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
放題解
題目傳送門
?
放代碼
?
#include <bits/stdc++.h>//萬能頭 #define MAXN 150000//最多的建筑數量(數據范圍) using namespace std;inline int read()//快讀 {int ret=0,f=1;char ch=getchar();while (ch<'0'||ch>'9'){if (ch=='-') f=-f;ch=getchar();}while (ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();return ret*f; }int n,T,ans;//n即題中N指建筑總數 T指修復時經過了多長時間 ans即題中S一共能修復的建筑數struct node//儲存每個建筑的信息 {int w; //w為修理這個建筑所用時間 (T1)int t;//t為這個建筑報廢時間 (T2) } a[MAXN];priority_queue<int> Q;//優先隊列()bool cmp (node x, node y)//sort排序規則 {return x.t < y.t;//按t從小到大排序 } int main() {n=read();//快讀讀入建筑總數for(int i = 1; i <= n; i++)//經典循環讀入 {a[i].w=read();// 快讀讀入這個建筑所用時間 (T1)a[i].t=read();//快讀讀入這個建筑報廢時間 (T2) }sort(a + 1, a + n + 1, cmp);//含規則的排序(按t從小到大排序cmp為規則)for(int i = 1; i <= n; i++) {if(T + a[i].w > a[i].t)//如果無法修復此建筑 {if(a[i].w < Q.top())//ai < aj {T -= Q.top();//注意這里要減掉 Q.pop();Q.push(a[i].w);T += a[i].w;}}else//能修復此建筑 {Q.push(a[i].w);ans++;T+=a[i].w;}}cout<<ans<<endl;//輸出答案 return 0; }
放數據
4 | ? |
|---|---|
100 | 200 |
200 | 1300 |
1000 | 1250 |
2000 | 3200 |
?
輸出:3
?
轉載于:https://www.cnblogs.com/liuyuxinblog/p/10792868.html
總結
以上是生活随笔為你收集整理的洛谷P4053 [JSOI2007]建筑抢修的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 最伤感的微信网名2017
- 下一篇: nike回到未来2多少钱