例题
P3545 [POI 2012] HUR-Warehouse Store
显然是个反悔贪心。
考虑先按吹这个气球的最晚爆炸时间排序,即 ,以计算下一个气球的吹起时间。在计算途中,如果当前的时间已经超过吹气球的最晚时间了,就要反悔,去掉浪费海拔最大的气球,这可以用大根堆维护。
#include<bits/stdc++.h>
using namespace std ;
#define int long long
int n ;
struct node
{
int D , L ;
bool operator < (const node &a) const
{
return L < a.L ;
}
}a[250005] ;
priority_queue<int> pq ;
void solve()
{
cin >> n ;
for( int i = 1 ; i <= n ; i++ )
{
cin >> a[i].L >> a[i].D ;
a[i].L += a[i].D ;
}
sort(a + 1 , a + n + 1) ;
int now = 0 ;
for( int i = 1 ; i <= n ; i++ )
{
now += a[i].D ;
pq.push(a[i].D) ;
if(now > a[i].L)
{
now -= pq.top() ;
pq.pop() ;
}
}
cout << pq.size() ;
}
signed main()
{
ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
int oT_To = 1 ;
// cin >> oT_To ;
while(oT_To--) solve() ;
return 0 ;
} P1012 [NOIP 1998 提高组] 拼数
只要 字符串 在 字符串的前面,如果 在 的前面。那么 肯定也在 的前面。
#include<bits/stdc++.h>
using namespace std ;
#define int long long
int n ;
string s[25] ;
bool cmp(string x , string y)
{
return x + y > y + x ;
}
void solve()
{
cin >> n ;
for( int i = 1 ; i <= n ; i++ )
{
cin >> s[i] ;
}
sort(s + 1 , s + n + 1 , cmp) ;
for( int i = 1 ; i <= n ; i++ ) cout << s[i] ;
}
signed main()
{
ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
int oT_To = 1 ;
// cin >> oT_To ;
while(oT_To--) solve() ;
return 0 ;
} P4823 [TJOI2013] 拯救小矮人
这道题考虑先把所有人都叠起来, 大的先出去,如果出不去就可以找一个 最大的且已经出去的垫在下面。
#include<bits/stdc++.h>
using namespace std ;
#define int long long
int n , h ;
struct node
{
int a , b ;
}a[2005] ;
bool cmp(node x , node y)
{
return x.a + x.b < y.a + y.b ;
}
priority_queue<int> Q ;
void solve()
{
cin >> n ;
for( int i = 1 ; i <= n ; i++ )
{
cin >> a[i].a >> a[i].b ;
}
cin >> h ;
sort(a + 1 , a + n + 1 , cmp) ;
int sum = 0 ;
for( int i = 1 ; i <= n ; i++ ) sum += a[i].a ;
int ans = 0 ;
for( int i = 1 ; i <= n ; i++ )
{
if(sum + a[i].b >= h)
{
sum -= a[i].a ;
Q.push(a[i].a) ;
ans++ ;
}
else if(!Q.empty() && Q.top() > a[i].a)
{
sum += Q.top() - a[i].a ;
Q.pop() ;
Q.push(a[i].a) ;
}
}
cout << ans ;
}
signed main()
{
ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
int oT_To = 1 ;
// cin >> oT_To ;
while(oT_To--) solve() ;
return 0 ;
} P3045 [USACO12FEB] Cow Coupons G
先贪心的考虑用优惠卷买最便宜的 个优惠。
然后考虑其他的,能直接买就直接买,否则的话只要有一个之前 小于现在的 ,那么就可以看看是不是可以增加一个牛。
#include<bits/stdc++.h>
using namespace std ;
#define int long long
int n , k , m ;
struct node
{
int p , c ;
}a[50005] ;
bool cmp(node x , node y)
{
return x.c < y.c ;
}
struct P
{
int val , id ;
bool operator < (const P &a) const
{
return val > a.val ;
}
};
priority_queue<int , vector<int> , greater<int> > que ;
priority_queue<P> Q_c , Q_p ;
bool vis[50005] ;
void solve()
{
cin >> n >> k >> m ;
for( int i = 1 ; i <= n ; i++ )
{
cin >> a[i].p >> a[i].c ;
}
sort(a + 1 , a + n + 1 , cmp) ;
int sum = 0 ;
for( int i = 1 ; i <= k ; i++ )
{
sum += a[i].c ;
if(sum > m)
{
cout << i - 1 ;
return ;
}
que.push(a[i].p - a[i].c) ;
}
for( int i = k + 1 ; i <= n ; i++ )
{
Q_c.push({a[i].c , i}) ;
Q_p.push({a[i].p , i}) ;
}
for( int i = k + 1 ; i <= n ; i++ )
{
while(vis[Q_c.top().id]) Q_c.pop() ;
while(vis[Q_p.top().id]) Q_p.pop() ;
int p1 = Q_c.top().id , p2 = Q_p.top().id ;
int tmp1 = Q_c.top().val , tmp2 = Q_p.top().val ;
tmp1 += que.top() ;
if(tmp1 < tmp2)
{
sum += tmp1 ;
Q_c.pop() ;
que.pop() ;
que.push(a[p1].p - a[p1].c) ;
vis[p1] = 1 ;
}
else
{
sum += tmp2 ;
Q_p.pop() ;
vis[p2] = 1 ;
}
if(sum > m)
{
cout << i - 1 ;
return ;
}
}
cout << n ;
}
signed main()
{
ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
int oT_To = 1 ;
// cin >> oT_To ;
while(oT_To--) solve() ;
return 0 ;
}