不可重复最优分解问题、会场安排问题、非单位时间任务安排问题

不可重复最优分解问题

#include <iostream>

using namespace std;

/*
贪心选择策略: 可以将n分解成从2开始的连续自然数的和。
证明:如果a+b=n,则|a-b|越小,那么a*b越大。
样例:
n = 7: 分成2,3。还剩2,从后往前进行n次加法,每次加1,直到将剩余的用完。2+1,3+1。
n = 10: 分成2,3,4。还剩1,从后往前加。2,3,4+1。
*/

const int N = 110;
int n;
int a[N];

int main()
{
    cin >> n;
    
    int k = 0;
    for(int i = 2; n >= i; i ++)
    {
        a[k++] = i;
        n -= i;
    }
    
    int t = k-1;
    while(n!=0)
    {
        a[t--] ++;
        n --;
    }

    int res = 1;
    for(int i = 0;i < k; i++) res *= a[i];
    
    cout << res << endl;
    
    return 0;
}

会场安排问题

 数据范围1000也可以过

#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdio>

using namespace std;

const int N = 100010;

int n;
struct Range
{
    int l, r;
    bool operator< (const Range &W)const
    {
        return l < W.l;
    }
}range[N];

int main()
{
    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 ++ )
    {

        if (heap.empty() || heap.top() >= range[i].l){
            heap.push(range[i].r);
        }
        else {
            heap.pop();
            heap.push(range[i].r);
        }
    }

    printf("%d\n", heap.size());

    return 0;
}

非单位时间任务安排问题

#include <iostream>
#include <algorithm> 

using namespace std;

const int N = 5010;
int n;
int p[N][N];

struct task{
	int t, d, w;
	bool operator< (const task &W)const
    {
        return d < W.d;
    }
}a[N]; 

int main()
{
	cin>>n;
	
	for(int i = 1; i <= n; i++)
		cin >> a[i].t >> a[i].d >> a[i].w;
		
	sort(a+1, a+n+1); //按照截止时间非减排序 
	
	// 第一个任务
	for(int i=0; i<=a[n].d; i++)
    {
        if(i<a[1].t) p[1][i]=a[1].w;//罚
        else p[1][i] = 0;//做第一个任务 
    }
    // 第2~n个任务 
    for(int i=2; i<=n; i++)
		for(int j=0; j<=a[n].d; j++)
		{
			if (j<a[i].t) p[i][j] = p[i-1][j] + a[i].w;//当前任务无法做 
			else p[i][j] = min(p[i-1][j] + a[i].w, p[i-1][min(j, a[i].d)- a[i].t]);//不做,做。选最优 
   		 }
	cout<<p[n][a[n].d]<<endl;
	return 0;
}

 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

就是搞笑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值