9.贪心算法

news2025/3/16 5:30:46

简单贪心

1.P10452 货仓选址 - 洛谷

 

#include<iostream>
#include<algorithm>
using namespace std;

typedef long long LL;
const int N = 1e5+10;
LL a[N];
LL n;

int main()
{
	cin>>n;
	for(int i = 1;i <= n;i++)cin>>a[i];
	sort(a+1,a+1+n);//排序 
	LL sum = 0;
	//for(int i = 1;i <= n;i++)
	//{
	//	sum+=(abs(a[i]-a[(1+n)/2]));
	//}
	
	for(int i = 1;i <= n/2;i++)
	{
		sum += abs(a[i]-a[n+1-i]);
	}
	cout<<sum<<endl;
	return 0;
 } 

2.P1115 最大子段和 - 洛谷

#include<iostream>

using namespace std;

typedef long long LL;

const int N = 2e5+10;
LL a[N];
LL n;

int main()
{
	cin>>n;
	for(int i = 1;i <= n;i++)cin>>a[i];
	
	LL sum = 0;LL ret = -1e5;
	for(int i = 1;i <= n;i++)
	{
		sum+=a[i];
		ret = max(sum,ret);
		if(sum < 0)sum = 0;
	 } 
	 cout<<ret<<endl;
	 return 0;
 } 

舍弃的想法很大胆,也很有风险,但通过证明,就可以表示通过

3. P1094 [NOIP 2007 普及组] 纪念品分组 - 洛谷

#include<iostream>
#include<algorithm>
using namespace std;

int w,n;
const int N = 3e4+10;
int a[N];

int main()
{
	cin>>w>>n;
	for(int i = 1;i <= n;i++)cin>>a[i];
	//排序
	 sort(a+1,a+1+n);
	 int l = 1,r = n,ret = 0;
	 while(l <= r)
	 {
	 	//最小和最大相加小于w,符合 ,同时异位 
	 	if(a[l]+a[r]<=w)l++,r--;
	 	//l待定。r-- 
		 else
	 	{
	 		r--;
		 }
		 ret++;
	 }
	 cout<<ret<<endl;
	 return 0;
	
}

 4.P1056 [NOIP 2008 普及组] 排座椅 - 洛谷

#include<iostream>
#include<algorithm>
using namespace std;

int m, n, k, l, d;
const int N = 1010;

struct  node
{
	int index;//存行列的下标
	int cnt;//存取该行或者该列能隔开多少对同学
}row[N],col[N];

//按照cnt从大到小排列
bool cmp1(node& x, node& y)
{
	return x.cnt > y.cnt;
}
//按照下标从小到大排列
bool cmp2(node& x, node& y)
{
	return x.index < y.index;
}

int main()
{
	cin >> m >> n >> k >> l >> d;
	//初始化数组,赋值index
	for (int i = 1; i <= m; i++)row[i].index = i;
	for (int i = 1; i <= n; i++)col[i].index = i;
	//计算cnt
	while (d--)
	{
		int x, y, p, q; cin >> x >> y >> p >> q;
		if (x == p)col[min(y, q)].cnt++;
		else
			row[min(x, p)].cnt++;
	}
	//通过cnt把大的排在前面-->cmp1
	sort(row + 1, row + 1 + m, cmp1);
	sort(col + 1, col + 1 + n, cmp1);
	//把前k或者l大的按照下表从小到大进行排列
	sort(row + 1, row + 1 + k, cmp2);
	sort(col + 1, col + 1 + l, cmp2);
	//输出
	//行
	for (int i = 1; i <= k; i++)
	{
		cout << row[i].index << " ";
	}
	cout << endl;
	//列
	for (int i = 1; i <= l; i++)
	{
		cout << col[i].index << " ";
	}
	return 0;
}

1.把每一行和每一列可以隔开的同学记录到cnt中

2.按照cnt从大到小进行排列

3.按照index对前k或者l个进行从小到大的排列

4.输出前k 或 l的index下标

4.矩阵消除游戏 

 

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

int calc(int x)
{
	int ret = 0;
	while (x)
	{
		x = x & (x - 1);
		ret++;
	}
	return ret;
}

bool cmp(int x, int y)
{
	return x > y;
}

int n, m,k;
const int N = 100;
int a[N][N];
int col[N];

int main()
{
	cin >> n >> m >> k;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cin >> a[i][j];
		}
	}
	int sum = 0, ret = 0;
	//暴力枚举所有的第一行
	for (int st = 0; st < (1 << n); st++)
	{
		sum = 0;
		int num_1 = calc(st);
		//超过就不要了
		if (num_1 > k)continue;

		memset(col, 0, sizeof(col));
		for (int i = 0; i < n; i++)
		{
			//加上当前行的数字
			for (int j = 0; j < m; j++)
			{
				if (((st >> i) & 1) == 1)
				{
					sum += a[i][j];
				}
				else
				{
					col[j] += a[i][j];
				}
			}
		}
		//对列进行从大到小排序,取前k - num_1个
		int remain = k - num_1;
		sort(col, col + m, cmp);
		for (int i = 0; i < remain; i++)sum += col[i];
		ret = max(ret, sum);
	}
	cout << ret << endl;
	return 0;
}

推公式

1.在确定好的顺序序列中,拿出相邻的两个元素
2.交换这两个元素,对前面和后面确定好顺序的序列的结果不造成影响
3.根据这两个原色交换前后的结果推导出排序的规

1.P1012 [NOIP 1998 提高组] 拼数 - 洛谷

#include<iostream>

#include<algorithm>
using namespace std;

int n;
const int N = 25;
string st[N];

bool cmp(string& x, string& y)
{
	return x + y > y + x;
}

int main()
{
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> st[i];

	sort(st, st+n, cmp);
	for (int i = 0; i < n; i++)
		cout << st[i];
	return 0;
}

 比较方法:两两元素相拼,

2.P2878 [USACO07JAN] Protecting the Flowers S - 洛谷

很震惊!! 

1.在确定好的顺序序列中,拿出相邻的两个元素
2.交换这两个元素,对前面和后面确定好顺序的序列的结果不造成影响
3.根据这两个原色交换前后的结果推导出排序的

#include<iostream>
#include<algorithm>
using namespace std;

typedef long long LL;

LL n;
const int N = 1e5+10;
struct node
{
	LL t;//时间
	LL d;//吃草速度 
}a[N]; 

bool cmp(node& x,node&y)
{
	return x.t*y.d < x.d*y.t;
}

int main()
{
	cin>>n;
	for(int i = 1;i <= n;i++)
	{
		cin>>a[i].t>>a[i].d;
	}
	sort(a+1,a+n+1,cmp);
	LL ret = 0,t = 0;
	for(int i = 1;i <= n;i++)
	{
		ret += a[i].d*t;
		t += 2*a[i].t;
	}
	cout<<ret<<endl;
 } 

3. P1842 [USACO05NOV] 奶牛玩杂技 - 洛谷

#include<iostream>
#include<algorithm>

using namespace std;

typedef long long LL;

const int N = 5e4+10;
int n;

struct node
{
	LL w;
	LL s;	
}a[N];
//推公式得到,把max中较小的放在前面,会让总体压力值变得较小 
bool cmp(node&x,node&y)
{
	return max(-x.s,x.w-y.s) < max(-y.s,y.w-x.s);
}

int main()
{
	cin>>n;
	for(int i = 1;i <= n;i++)
	{
		cin>>a[i].w>>a[i].s;
	}
	sort(a+1,a+1+n,cmp);
	
	LL w = 0;
	LL ret = -1e5;
	for(int i = 1;i <= n;i++)
	{
		ret = max(ret,w - a[i].s);
		w+=a[i].w;
	}
	cout<<ret<<endl;
	return 0;
	
 } 

哈夫曼树 

1.P1090 [NOIP 2004 提高组] 合并果子 - 洛谷 

#include<iostream>
#include<queue>
#include<vector>
using namespace std;

typedef long long LL; 

priority_queue<LL,vector<LL>,greater<LL>>heap;

LL n;
int main()
{
	cin>>n;
	for(int i = 1;i <= n;i++)
	{
		LL x;cin>>x;
		heap.push(x);
	}
	LL sum = 0;
	while(heap.size()>1)
	{
		LL x = heap.top();heap.pop();
		LL y = heap.top();heap.pop();
		sum+=(x+y);
		heap.push(x+y);
	 } 
	 cout<<sum<<endl;
}

 区间问题

这种题⽬的解决⽅式⼀般就是按照区间的左端点或者是右端点排序,然后在排序之后的区间上,根据 题⽬要求,制定出相应的贪⼼策略,进⽽得到最优解。

具体是根据左端点还是右端点排序?升序还是降序?⼀般是假设⼀种排序⽅式,并且制定贪⼼策略, 当没有明显的反例时,就可以尝试去写代码。

1.P1803 凌乱的yyy / 线段覆盖 - 洛谷

#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e6+10;
int n;
struct node
{
	int s;
	int e;
}a[N];

bool cmp(node&x,node&y)
{
	return x.s < y.s;
 } 

int main()
{
	cin>>n;
	for(int i = 1;i <= n;i++)
	{
		cin>>a[i].s;
		cin>>a[i].e;
	}
	
	sort(a+1,a+1+n,cmp);//按照起点开始由小到大的顺序排列
	int ret = 1;
	int r = a[1].e;
	for(int i = 2;i <= n;i++)
	{
		int right = a[i].e,left = a[i].s;
		if(left < r)//重叠了,不能参加,如果重叠的右端比前面那一个还小,那就贪,覆盖前面哪一个 
		{
			r = min(r,right);
		}
		else
		{
			ret++;//没有重叠,可以参加
			r = right;//更新较小的r 
		 } 
	}
	cout<<ret<<endl;
	return 0;
	 
} 

 

2.UVA1193 Radar Installation - 洛谷

按照左端点排序,互相重叠的区间是连续的 

二维问题转化成一维问题 

证明: 按照左端点排序,互相重叠的区间是连续的 

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;

const int N = 1e3+10;

int n,d;
struct node
{
	double l;
	double r;
}a[N];

bool cmp(node&x,node&y)
{
	return x.l < y.l;
}
int main()
{
	int cnt = 1;
	while(cin>>n>>d&&(n&&d))
	{
		int flag = 1;
		for(int i = 1;i <=		 n;i++)
		{
			int x,y;cin>>x>>y;
			if(y > d)flag = -1;
			//把二维映射到一维上去
			double l =  sqrt(d*d - y*y);
			a[i].l = x - l;
			a[i].r = x + l;
		}
		sort(a+1,a+1+n,cmp);
	int ret = 1;
	int r = a[1].r; 
	cout<<"Case "<<cnt<<": ";
	cnt++; 
	for(int i = 2;i <= n;i++)
	{
		int left = a[i].l,right = a[i].r;
		if(left<=r)//等于也可以扫到 
		{
			//扫描通过
			r =  min(r,right);
		}else
		{
			ret++;
			r = right;
		}
	}
	
	cout<<ret<<endl;
}
	return 0;
 } 

3.P2887 [USACO07NOV] Sunscreen G - 洛谷

 

#include<iostream>
#include<algorithm> 
using namespace std;

const int N = 3e3+10;

int n,l;
struct node
{
	int l;//表示奶牛耐受的最小值//防晒霜的防晒值 
	int r;//奶牛耐受的最大值// 防晒霜的数量 
}a[N],b[N]; 

bool cmp(node&x,node&y)
{
	return x.l > y.l;
}
int main()
{
	cin>>n>>l;
	for(int i = 1;i <= n;i++)
	{
		cin>>a[i].l>>a[i].r;//输入奶牛耐受值 
	}
	for(int i = 1;i <= l;i++)
	{
		cin>>b[i].l>>b[i].r;//输入防晒霜的防晒值和数量 
	}
	//按照奶牛奶牛左端从大到小进行排序
	sort(a+1,a+1+n,cmp);
	//按照防晒霜防晒值从大到小进行排序
	sort(b+1,b+1+l,cmp);
	int ret = 0;
	for(int i = 1;i <= n;i++)
	{
		//选择一种防晒霜
		for(int j = 1;j <= l;j++)	
		{
			if(b[j].r == 0)continue;
			if(b[j].l<=a[i].r&&b[j].l>=a[i].l)
			{
				//符合条件,ret++,数量-- 
				ret++;
				b[j].r--;
				break;//选完一个就直接除去,免得后面的都没了 
			}
		}
	} 
	cout<<ret<<endl;
	return 0; 
}

 4.P2859 [USACO06FEB] Stall Reservations S - 洛谷

#include<iostream>
#include<queue> 
#include<algorithm>
using namespace std;
const int N = 5e4 + 10;
int n;
struct node
{
	int l;//牛牛的开始//该牛棚的结束时间 
	int r;//牛牛的结束 //该牛棚的编号 
	int num;//这只牛的编号 

	bool operator<(const node& y)const
	{
		return l > y.l;//创建小根堆 
	}
}a[N];
bool cmp(node& x, node& y)
{
	return x.l < y.l;
}
int res[N];//记录每只牛进入的牛棚顺序 
priority_queue<node> heap;//建议一个关于牛棚结束时间的小根堆,找出当前技术时间最早的,拉出来 
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i].l >> a[i].r;
		a[i].num = i;
	}
	//按照左端点从小到大排列
	sort(a + 1, a + 1 + n, cmp);
	int ret = 1;//记录牛棚状态
	heap.push({ a[1].r,1 });
	res[a[1].num] = 1; //一号牛进一号棚 
	for (int i = 2; i <= n; i++)
	{
		int l = a[i].l, r = a[i].r;
		int ete = heap.top().l;
		int num_peng = heap.top().r;
		if (ete >= l)//如果最短结束时间都>这只牛的开始的起始时间,那么就必须新开一个牛棚 
		{
			ret++;
			heap.push({ r,ret });
			res[a[i].num] = ret;
		}
		else//可以拿下 
		{
			heap.pop();//结束不要了 
			heap.push({ r,num_peng });//把这只牛推入彭中 
			res[a[i].num] = num_peng;
		}
	}
	cout << ret << endl;
	for (int i = 1; i <= n; i++)cout << res[i] << endl;
}

P2859 [USACO06FEB] Stall Reservations S - 洛谷

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2315808.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

大模型训练全流程深度解析

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到网站。https://www.captainbed.cn/north 文章目录 1. 大模型训练概览1.1 训练流程总览1.2 关键技术指标 2. 数据准备2.1 数据收集与清洗2.2 数据…

每日一题---单词搜索(深搜)

单词搜索 给出一个二维字符数组和一个单词&#xff0c;判断单词是否在数组中出现&#xff0c; 单词由相邻单元格的字母连接而成&#xff0c;相邻单元指的是上下左右相邻。同一单元格的字母不能多次使用。 数据范围&#xff1a; 0 < 行长度 < 100 0 < 列长度 <…

插入排序c++

插入排序的时间复杂度为O&#xff08;N^2&#xff09;&#xff0c;和冒泡排序的时间复杂度相同&#xff0c;但是在某些情况下插入排序会更优。 插入排序的原理是&#xff1a;第1次在0~0范围内排序&#xff0c;第2次在0~1范围内排序&#xff0c;第3次在0~2范围内排序……相当于…

Swagger 从 .NET 9 中删除:有哪些替代方案

微软已经放弃了对 .NET 9 中 Swagger UI 包 Swashbuckle 的支持。他们声称该项目“不再由社区所有者积极维护”并且“问题尚未得到解决”。 这意味着当您使用 .NET 9 模板创建 Web API 时&#xff0c;您将不再拥有 UI 来测试您的 API 端点。 我们将调查是否可以在 .NET 9 中使用…

嵌入式八股ARM篇

前言 ARM篇主要介绍一下寄存器和中断机制,至于汇编这一块…还请大家感兴趣自行学习 1.寄存器 R0 - R3 R4 - R11 寄存器 R0 - R3一般用作函数传参 R4 - R11用来保存程序运算的中间结果或函数的局部变量 在函数调用过程中 注意在发生异常的时候 cortex-M0架构会自动将R0-R3压入…

使用DeepSeek和墨刀AI,写PRD文档、画原型图的思路、过程及方法

使用DeepSeek和墨刀AI&#xff0c;写PRD文档、画原型图的思路、过程及方法 现在PRD文档要如何写更高效、更清晰、更完整&#xff1f; 还是按以前的思路写PRD&#xff0c;就还是以前的样子。 现在AI这么强大&#xff0c;产品经理如何使用DeepSeek写PRD文档&#xff0c;产品经…

【VUE2】第五期——VueCli创建项目、Vuex多组件共享数据、json-server——模拟服务端api

黑马程序员视频地址&#xff1a;091-vuex的基本认知_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1HV4y1a7n4?vd_source0a2d366696f87e241adc64419bf12cab&spm_id_from333.788.videopod.episodes&p91 目录 1 VueCli 自定义创建项目 2 Eslint代码规范 2.1 规…

rpmlib(SetVersions) is needed by can-uilts-v2019.00.0-alt1.aarch64

在我在Linux中安装离线CAN工具时&#xff0c;出现了一个问题&#xff0c; rootwanghuo:~# rpm -ivh can-uilts-v2019.00.0-alt1.aarch64.rpm error: Failed dependencies:rpmlib(SetVersions) is needed by can-uilts-v2019.00.0-alt1.aarch64 意思是尝试安装 can-uilts-v20…

CNN 稠密任务经典结构

FCN UNet FPN FCNUNETFPNpadding无&#xff08;逐渐变小&#xff09; 有&#xff08;左右对称&#xff09;上采样 双线性双线性 最近邻跳跃链接 相加 Cropcat 1x1卷积相加 三个网络差不多&#xff0c;UNet名字最直观&#xff0c;后续流传…

算法刷题整理合集(二)

本篇博客旨在记录自已的算法刷题练习成长&#xff0c;里面注有详细的代码注释以及和个人的思路想法&#xff0c;希望可以给同道之人些许帮助。本人也是算法小白&#xff0c;水平有限&#xff0c;如果文章中有什么错误或遗漏之处&#xff0c;望各位可以在评论区指正出来&#xf…

STM32配套程序接线图

1 工程模板 2 LED闪烁 3LED流水灯 4蜂鸣器 5按键控制LED 6光敏传感器控制蜂鸣器 7OLED显示屏 8对射式红外传感器计次 9旋转编码器计次 10 定时器定时中断 11定时器外部时钟 12PWM驱动LED呼吸灯 13 PWM驱动舵机 14 PWM驱动直流电机 15输入捕获模式测频率 16PWMI模式测频率占空…

Houdini学习笔记

1. Houdini中一次只能显示一个物体 如果要都显示 需要 merge 节点 粉色的是 以参考显示 2.对任意一个节点按F1 可以弹出houdini官方文档 3. 恢复视角 Space H,居中 Space G 居中选中物体

仿Ant Design Vue风格自定义浏览器滚动条样式

仿Ant Design Vue风格自定义浏览器滚动条样式 问题原因 浏览器默认的滚动条样式很丑&#xff0c;无法满足需求&#xff0c;需要自定义滚动条样式&#xff0c;参考ant-design-vue的样式 css修改滚动相关属性可查阅官方文档 选择器介绍 ::webkit-scrollbar&#xff1a;滚动条…

单元测试、系统测试、集成测试、回归测试的步骤、优点、缺点、注意点梳理说明

单元测试、系统测试、集成测试、回归测试的梳理说明 单元测试 步骤&#xff1a; 编写测试用例&#xff0c;覆盖代码的各个分支和边界条件。使用测试框架&#xff08;如JUnit、NUnit&#xff09;执行测试。检查测试结果&#xff0c;确保代码按预期运行。修复发现的缺陷并重新测…

网络安全反渗透 网络安全攻防渗透

网络渗透防范主要从两个方面来进行防范&#xff0c;一方面是从思想意识上进行防范&#xff0c;另一方面就是从技术方面来进行防范。 1.从思想意识上防范渗透 网络攻击与网络安全防御是正反两个方面&#xff0c;纵观容易出现网络安全事故或者事件的公司和个人&#xff0c;在这些…

《GitHub网路访问不稳定:解决办法》:此文为AI自动生成

《GitHub网路访问不稳定&#xff1a;解决办法》&#xff1a;此文为AI自动生成 GitHub 网路访问不稳定初现 在当今数字化时代&#xff0c;软件开发行业蓬勃发展&#xff0c;GitHub 作为全球最大的代码托管平台&#xff0c;已然成为无数开发者不可或缺的 “宝库”。它不仅汇聚了…

G-Star 公益行 | 温暖相约 3.30 上海「开源×AI 赋能公益」Meetup

你是否曾想过&#xff0c;在这个数字化浪潮席卷的时代&#xff0c;公益组织如何突破技术瓶颈&#xff1f;当 AI 成为热门话题&#xff0c;它能为公益事业带来怎样的温度&#xff1f;开源的力量&#xff0c;如何让每一份善意都拥有无限可能&#xff1f; G-Star 公益行&#xff…

docker pull 镜像问题

问题一&#xff1a;pull镜像报错:time out 分析&#xff1a;源问题&#xff0c;网络不稳定&#xff0c;更换加速源&#xff0c;地址&#xff1a;/etc/docker/daemon.json 解决&#xff1a;更换地址&#xff0c;如下&#xff0c;然后敲&#xff1a;docker daemon-reload &&…

STAR Decomposition 一种针对极端事件的信号分解方法 论文精读加复现

STAR 分解&#x1f680; 在时序预测任务中&#xff0c;为了情绪化信号的各种成分&#xff0c;例如趋势信息季节信息等往往都需要对信号进行分解。目前熟知的分解方式有很多种&#xff0c;经验模态分解 EMD 变分模态分解 VMD &#xff0c;还有 集合经验模态分解 EEMD&#xff0c…

基于SpringBoot + Vue 的房屋租赁系统

基于springboot的房屋租赁管理系统-带万字文档 SpringBootVue房屋租赁管理系统 送文档 本项目有前台和后台两部分、多角色模块、不同角色权限不一样 共分三种角色&#xff1a;用户、管理员、房东 管理员&#xff1a;个人中心、房屋类型管理、房屋信息管理、预约看房管理、合…