1 区间问题
1.1 区间选点和最大不相交区间数量
/*
学acwing的算法基础课学来的,喜欢的话多多支持呀。
*/
//区间选点和最大不相交区间数量
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
struct Range//定义一个结构体
{
int l,r;
bool operator<(const Range &W)const//重载操作符,排序时按照r右端点来排
{
return r<W.r;
}
}range[N];
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
int l,r;
cin>>l>>r;
range[i]={l,r};
}
sort(range,range+n);//将其按照右端点来排
int res=0,ed=-2e9;//res记录答案
for(int i=0;i<n;i++)//枚举每一个
if(range[i].l>ed)//假如左端点大于尾部
{
res++;//则区间数增多一个
ed=range[i].r;//更新一下新的尾部
}
cout<<res<<endl;
return 0;
}
/*到达胜利之前无法回头*/
1.2 区间分组
/*
学acwing的算法基础课学来的,喜欢的话多多支持呀。
*/
//区间分组
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
struct Range//定义一个结构体
{
int l,r;
bool operator<(const Range &W)const//重载操作符,排序时按照l左端点来排
{
return l<W.l;
}
}range[N];
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
int l,r;
scanf("%d%d",&l,&r);
range[i]={l,r};
}
sort(range,range+n);//将其按照左端点来排
priority_queue<int,vector<int>,greater<int>>heap;//定义一个小根堆
for(int i=0;i<n;i++)//枚举每一个
{
auto r=range[i];
if(heap.empty()||heap.top()>=r.l) heap.push(r.r);//max_r>=r.l时,则放进当前组中
else
{
heap.pop();//反之,不存在这样的组
heap.push(r.r);//开新组在放进去
}
}
printf("%d\n",heap.size());
return 0;
}
/*到达胜利之前无法回头*/
1.3 区间覆盖
/*
学acwing的算法基础课学来的,喜欢的话多多支持呀。
*/
//区间覆盖
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
struct Range//定义一个结构体
{
int l,r;
bool operator<(const Range &W)const//重载操作符,排序时按照l左端点来排
{
return l<W.l;
}
}range[N];
int main()
{
int st,ed;
scanf("%d%d",&st,&ed);
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
int l,r;
scanf("%d%d",&l,&r);
range[i]={l,r};
}
sort(range,range+n);//将其按照左端点来排
int res=0;
bool success=false;//标记有没有完全覆盖
for(int i=0;i<n;i++)
{
int j=i,r=-2e9;
while(j<n&&range[j].l<=st)//找小于st并且长度最大的点
{
r=max(r,range[j].r);
j++;
}
if(r<st) break;//假如中间有个点连不上,有缝隙,则覆盖失败
res++;
if(r>=ed)//反之到最后一个点,则覆盖成功
{
success=true;
break;
}
st=r;//更新一下新的st
i=j-1;//i也更新一下
}
if(!success) res=-1;//假如有缝隙,输出-1
printf("%d\n",res);
return 0;
}
/*到达胜利之前无法回头*/
2 Huffman数(哈夫曼数) 合并果子
/*
学acwing的算法基础课学来的,喜欢的话多多支持呀。
*/
//哈夫曼数 合并果子所需体力最小,每次只能合并其中两个为一个,求体力最小
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
scanf("%d",&n);
priority_queue<int,vector<int>,greater<int>>heap;//定义一个小根堆
while(n--)
{
int x;
scanf("%d",&x);
heap.push(x);
}
int res=0;
while(heap.size())//假如堆不为空
{
int a=heap.top();heap.pop();//取出堆顶也就是最小的数,在弹出来
int b=heap.top();heap.pop();//取出堆顶也就是第二小的数,在弹出来
res+=a+b;//把最小两个数相加,也就是所需的体力
heap.push(a+b);//把合并的数在放进堆中
}
printf("%d\n",res);
return 0;
}
/*到达胜利之前无法回头*/
3 排序不等式 排队打水
/*
学acwing的算法基础课学来的,喜欢的话多多支持呀。
*/
//排队打水
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e5+10;
LL a[N];
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++) scanf("%lld",&a[i]);
sort(a,a+n);//从小开始贪
LL res=0,t=0;
for(int i=0;i<n;i++) res+=t,t+=a[i];//算时间
printf("%lld\n",res);//输出答案
return 0;
}
/*到达胜利之前无法回头*/
4 绝对值不等式 货仓选址
/*
学acwing的算法基础课学来的,喜欢的话多多支持呀。
*/
//货仓选址 选距离每个货仓的最短距离
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e5+10;
LL a[N];
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++) scanf("%lld",&a[i]);
sort(a,a+n);//从小开始贪
LL res=0;
for(int i=0;i<n;i++) res+=abs(a[i]-a[n/2]);//根据贪心,则最短距离是中间,也就是中位数
printf("%lld\n",res);
return 0;
}
/*到达胜利之前无法回头*/
5 推公式 耍杂技的牛
/*
学acwing的算法基础课学来的,喜欢的话多多支持呀。
*/
//耍杂技的牛 每头牛有一个W(重量)和S(承受力),每头牛进行叠罗汉,求每头牛的风险值最大的最小
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;//定义一个PII存两个值
const int N=5e4+10;
PII cow[N];
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
int w,s;
scanf("%d%d",&w,&s);
cow[i]={w+s,w};//first存w+s,second存w
}
sort(cow,cow+n);//排序有first进行排序,也就是w+s从小到大排序贪心
int res=-2e9,sum=0;
for(int i=0;i<n;i++)
{
int w=cow[i].second,s=cow[i].first-w;
res=max(res,sum-s);//rea记录风险值的最大值
sum+=w;
}
printf("%d\n",res);//输出答案
return 0;
}
/*到达胜利之前无法回头*/