贪心初步
基础
贪心就是得到一种取法,这样就可以确保每一次只有一个选择,而且选择之后,答案最优。
最主要的,还是这个策略。
例题
P1106 删数问题
我们发现,只要有
注意判前导 0。
#include<bits/stdc++.h>
using namespace std ;
#define int long long
string s ;
int k ;
void solve()
{
cin >> s >> k ;
for( int i = 1 ; i <= k ; i++ )
{
bool fl = 0 ;
for( int j = 0 ; j < s.size() - 1 ; j++ )
{
if(s[j] > s[j + 1])
{
s.erase(j , 1) ;
fl = 1 ;
break ;
}
}
if(!fl) s.erase(s.size() - 1 , 1) ;
}
bool fl1 = 0 , fl2 = 0 ;
for( int i = 0 ; i < s.size() ; i++ )
{
if(!fl1 && s[i] == '0') continue ;
fl1 = 1 ;
fl2 = 1 ;
cout << s[i] ;
}
if(!fl2) cout << 0 ;
}
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 ;
}
P8082 [COCI 2011/2012 #4] KEKS
和上题相反,但是需要优化。
考虑用到单调栈。去掉栈顶的操作就可以看成是删除,记录个数即可。注意只能删掉
#include<bits/stdc++.h>
using namespace std ;
#define int long long
string s ;
int k , n ;
int id[500005] ;
void solve()
{
cin >> n >> k >> s ;
s = ' ' + s ;
int len = n - k ;
int top = 0 ;
for( int i = 1 ; i <= n ; i++ )
{
while(top && k && s[i] > s[id[top]]) k-- , top-- ;
id[++top] = i ;
}
for( int i = 1 ; i <= len ; i++ ) cout << s[id[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 ;
}
CF1718B Fibonacci Strings
这道题可以贪心的想到用大的数字个数,填更大的斐波那契数列的值,因为这样数字个数大于等于斐波那契数列的值才能填写,如果小于就不能填。所以说要这么填。
注意填写时,不能和上一次填的字母一样。
#include<bits/stdc++.h>
using namespace std ;
#define int long long
int n ;
int c[105] ;
int f[55] ;
int sum[55] ;
int id ;
struct node
{
int val , id ;
bool operator < (const node &a) const
{
return val < a.val ;
}
};
void solve()
{
cin >> n ;
int cnt = 0 ;
for( int i = 1 ; i <= n ; i++ )
{
cin >> c[i] ;
cnt += c[i] ;
}
for( int i = 1 ; i <= 45 ; i++ )
{
if(cnt <= sum[i])
{
id = i ;
break ;
}
}
if(cnt != sum[id])
{
cout << "NO\n" ;
return ;
}
priority_queue<node> pq ;
for( int i = 1 ; i <= n ; i++ ) pq.push({c[i] , i}) ;
int lst = 0 ;
for( int i = id ; i >= 1 ; i-- )
{
node now = pq.top() ;
pq.pop() ;
if(now.id == lst)
{
if(pq.empty())
{
cout << "NO\n" ;
return ;
}
node nw = pq.top() ;
pq.pop() ;
pq.push(now) ;
now = nw ;
}
if(now.val < f[i])
{
cout << "NO\n" ;
return ;
}
now.val -= f[i] ;
lst = now.id ;
pq.push(now) ;
}
cout << "YES\n" ;
}
void init()
{
f[1] = f[2] = 1 ;
for( int i = 3 ; i <= 50 ; i++ )
{
f[i] = f[i - 1] + f[i - 2] ;
}
for( int i = 1 ; i <= 45 ; i++ )
{
sum[i] = sum[i - 1] + f[i] ;
}
}
signed main()
{
ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
int oT_To = 1 ;
cin >> oT_To ;
init() ;
while(oT_To--) solve() ;
return 0 ;
}
CF1945F Kirill and Mushrooms
这道题可以使用逆向思维,从数组里一个一个添加元素。当还剩下
#include<bits/stdc++.h>
using namespace std ;
#define int long long
int n ;
int c[200005] ;
int p[200005] ;
void solve()
{
priority_queue<int , vector<int> , greater<int> > pq ;
cin >> n ;
for ( int i = 1 ; i <= n ; i++ ) cin >> c[i] ;
for ( int i = 1 ; i <= n ; i++ ) cin >> p[i] ;
int maxn = 0 , cnt = 0 ;
for ( int k = n ; k >= 1 ; k-- )
{
pq.push(c[p[k]]) ;
while (pq.size() > k) pq.pop() ;
if (pq.size() == k)
{
if (pq.top() * k >= maxn)
{
maxn = pq.top() * k ;
cnt = k ;
}
}
}
cout << maxn << " " << cnt << "\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 ;
}