顺序题单之外的贪心

P1094
分组要最少,而且不能超过一定价格,那就从大的向小的看,可以就加不可以就跳过,记得每次都可以解决掉最后一个元素

#include<iostream>
#include<algorithm> 
using namespace std;
int a[100000];
int main()
{
	int mon,ge;
	cin>>mon>>ge;
	int end=ge,st=1,cont=0;
	for(int i=1;i<=ge;i++)
	{
		cin>>a[i];
	}
	sort(a+1,a+ge+1);
	while(st<=end)
	{
	
		if(a[end]+a[st]<=mon)
		{
			cont++;
			end--;
			st++;
		}
		else
		{
			cont++;
			end--;
		}
	}
cout<<cont;
 } 

P1208
这是贪心,就先整取,当整取不了的时候,再按照分取

#include<iostream>break!!!!
#include<algorithm>
using namespace std;
struct Niu
{
	int dan;
	int nai;
}a[1000089];
bool cmp(struct Niu x,struct Niu y)
{
   return x.dan <y.dan ;
}
int main()
{
	int xu,ti,ans=0;
	cin>>xu>>ti;
	for(int i=1;i<=ti;i++)
	{
		cin>>a[i].dan >>a[i].nai ;
	}
	sort(a+1,a+ti+1,cmp);
	for(int i=1;i<=ti;i++)
	{
		if(xu>=a[i].nai )
		{
			xu-=a[i].nai;
			ans+=a[i].dan *a[i].nai ;
		}
		else
		{
			ans+=a[i].dan*xu;
			break;
		}
	}
	cout<<ans;
	return 0; 
 } 

P1223 排队接水
一个有用的技巧不用开结构体就是把时间放大很多倍,之后就可以直接排序了

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
double sum;
long int n;
long long int t[1001];
double ans;
int main(int argc, const char * argv[])
{
    cin>>n;
    int x;
    for(int i=1;i<=n;i++)
    {
        cin>>x;
        t[i]=x*10002+i;
    }
    sort(t+1,t+1+n);
    for(int j=1;j<=n;j++)
    {
        cout<<t[j]%10002<<" ";
        sum+=t[j]/10002*(n-j);
    }
    cout<<endl;
    ans=sum/n;
    printf("%0.2lf",ans);
    return 0;
}

P1803
排序排的是结束时间,只有结束时间的排序有意义,然后在这样排序的基础之上,然后一个一个向下去看开始时间,如果满足下一个的开始时间大于它的,那么就可以参加,并且把结束时间改为当前这场比赛的

#include<iostream>
#include<algorithm>
using namespace std;
struct Node
{
	int st;
	int end; 
}a[100007];
bool cmp(struct Node x,struct Node y)
{
	return x.end<y.end ;
}
int main()
{
	int m,cur,cont=1;
	cin>>m;
	for(int i=1;i<=m;i++)
	{
		cin>>a[i].st >>a[i].end ;
	}
	sort(a+1,a+m+1,cmp);
	cur=a[1].end ;
	for(int i=1;i<=m;i++)
	{
		
		if(a[i+1].st >=cur )
		{
			cont++;
			cur=a[i+1].end;
		}
	}
	cout<<cont;
	return 0;
	
}

P1031
那么必定是从右到左一一地满足啊

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
	int m,a[10000],sum,aver,s=0;
	cin>>m;
	for(int i=1;i<=m;i++)
	{
		cin>>a[i];
		sum+=a[i];
	}
	aver=sum/m;
	for(int i=1;i<m;i++)
	{
		if(a[i]!=aver)
		{a[i+1]-=aver-a[i];
		s++;
		}
		
	}
	cout<<s;
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当涉及到背包问题时,贪心算法是一种常用的解决方法。在C语言中,可以使用贪心算法来解决背包问题。下面是一个简单的C语言贪心算法背包问题的代码示例: ```c #include <stdio.h> // 定义物品结构体 typedef struct { int weight; // 物品重量 int value; // 物品价值 } Item; // 贪心算法背包问题函数 void knapsack(Item items[], int n, int capacity) { // 计算物品的性价比(价值除以重量) float ratios[n]; for (int i = 0; i < n; i++) { ratios[i] = (float) items[i].value / items[i].weight; } // 按照性价比排序物品 for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (ratios[j] < ratios[j + 1]) { // 交换物品的位置 float tempRatio = ratios[j]; ratios[j] = ratios[j + 1]; ratios[j + 1] = tempRatio; Item tempItem = items[j]; items[j] = items[j + 1]; items[j + 1] = tempItem; } } } // 装入背包 int currentWeight = 0; float totalValue = 0; for (int i = 0; i < n; i++) { if (currentWeight + items[i].weight <= capacity) { // 将物品放入背包 currentWeight += items[i].weight; totalValue += items[i].value; } else { // 物品只能部分放入背包 int remainingWeight = capacity - currentWeight; totalValue += ratios[i] * remainingWeight; break; } } // 输出结果 printf("背包中物品的总价值为: %.2f\n", totalValue); } int main() { // 创建物品数组 Item items[] = { {10, 60}, {20, 100}, {30, 120} }; int n = sizeof(items) / sizeof(items[0]); // 物品数量 int capacity = 50; // 背包容量 // 调用贪心算法背包问题函数 knapsack(items, n, capacity); return ; } ``` 这段代码实现了一个简单的贪算法背包问题。它首先计算每个物品性价比,然后按照性价比对物品进行排序。接下来,它从性价比最高的物品开始,依次将物品放入背包,直到背包装满或者所有物品都放入背包。最后,输出背包中物品的总价值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值