第三节-贪心思想,反悔贪心,模拟费用流,Slope Trick,分治思想,猫树
-
学习情况:
在理论知识上除了 Slope Trick 以外都基本掌握。
AB题好久以前就写了,因此不做记录。
题:P4053 [JSOI2007] 建筑抢修
线性时间安排问题板题。
一个经典的贪心思路:首先对数据按截止时间从小到大排序,然后逐一枚举,不断拓展目前截止时间和更新目前剩余时间,若时间够,直接完成;若不够,将其所需时间与曾经完成的事件中用时最大者做比较,如果用时小于它,直接替换。
代码如下
#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define deap(i,a,b) for(int i=a;i>=b;i--)
#define in(a) a=read()
const int N = 1e5+5e4+5;
const int inf = INT_MAX;
inline int read() {
int x=0,f=1;
char ch=getchar();
while (ch<'0'||ch>'9') {
if (ch=='-') f=-1;
ch=getchar();
}
while (ch>='0'&&ch<='9') {
x=x*10+ch-48;
ch=getchar();
}
return x*f;
}
void fast() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
struct node{
int t,s;
}a[N];
int n,ans,tmp,now;
bool cmp(node x,node y){
return x.s<y.s;
}
priority_queue<int>q;
signed main() {
in(n);
rep(i,1,n)in(a[i].t),in(a[i].s);
sort(a+1,a+n+1,cmp);
rep(i,1,n){
now+=a[i].s-tmp;
tmp=a[i].s;
if(now>=a[i].t){
now-=a[i].t;
q.push(a[i].t);
}else if(!q.empty()){
int t=q.top();
if(a[i].t<t){
q.pop();
q.push(a[i].t);
now=now+t-a[i].t;
}
}
}
cout<<q.size();
return 0;
}题:P2587 [ZJOI2008] 泡泡堂
逆天贪心,被田忌赛马的烂思路卡了好久,难崩。
直接做四个分类讨论进行贪心转移:
- 1、若我方最弱能胜过敌方最弱,就此安排。
- 2、若我方最强能胜过敌方最强,就此安排。
- 3、若我方最弱能与敌方最强打成平手,就此安排
- 4、反之,让我方最弱“送”给敌方最强。
代码实现
#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define deap(i,a,b) for(int i=a;i>=b;i--)
#define in(a) a=read()
const int N = 1e5+5;
const int inf = INT_MAX;
inline int read() {
int x=0,f=1;
char ch=getchar();
while (ch<'0'||ch>'9') {
if (ch=='-') f=-1;
ch=getchar();
}
while (ch>='0'&&ch<='9') {
x=x*10+ch-48;
ch=getchar();
}
return x*f;
}
void fast() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
signed main() {
//fast();
int n;
in(n);
int *a=new int[n+1],*b=new int[n+1];
rep(i,1,n)in(a[i]);
rep(i,1,n)in(b[i]);
sort(a+1,a+n+1);
sort(b+1,b+n+1);
int l1=1,l2=1,r1=n,r2=n,ans=0;
while(l1<=r1&&l2<=r2){
if(a[l1]>b[l2])ans+=2,l1++,l2++;
else if(a[r1]>b[r2])ans+=2,r1--,r2--;
else if(a[l1]==b[r2])ans++,l1++,r2--;
else l1++,r2--;
}
cout<<ans<<' ';
l1=1,l2=1,r1=n,r2=n,ans=0;
while(l1<=r1&&l2<=r2){
if(a[l1]<b[l2])ans+=2,l1++,l2++;
else if(a[r1]<b[r2])ans+=2,r1--,r2--;
else if(a[r1]==b[l2])ans++,l2++,r1--;
else l2++,r1--;
}
cout<<n*2-ans<<' ';
delete[]a;
delete[]b;
return 0;
}
题:P4597 序列 sequence
看老师的思路写的,虽然老师的证明没看懂……
#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define rep(i,a,b) for(long long i=a;i<=b;i++)
#define deap(i,a,b) for(int i=a;i>=b;i--)
#define in(a) a=read()
const int N = 1e1;
const int inf = INT_MAX;
inline int read() {
int x=0,f=1;
char ch=getchar();
while (ch<'0'||ch>'9') {
if (ch=='-') f=-1;
ch=getchar();
}
while (ch>='0'&&ch<='9') {
x=x*10+ch-48;
ch=getchar();
}
return x*f;
}
void fast() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
signed main() {
//fast();
int n;
in(n);
priority_queue<int>q;
long long ans=0;
rep(i,1,n){
int x;
in(x);
q.push(x);
if(x<q.top()){
ans=ans+q.top()-x;
q.pop();
q.push(x);
}
}
cout<<ans;
return 0;
}