洛谷P3052 [USACO12MAR]Cows in a Skyscraper【模拟退火+DP】

时空限制 1000ms / 128MB

题目描述

A little known fact about Bessie and friends is that they love stair climbing races. A better known fact is that cows really don’t like going down stairs. So after the cows finish racing to the top of their favorite skyscraper, they had a problem. Refusing to climb back down using the stairs, the cows are forced to use the elevator in order to get back to the ground floor.

The elevator has a maximum weight capacity of W (1 <= W <= 100,000,000) pounds and cow i weighs C_i (1 <= C_i <= W) pounds. Please help Bessie figure out how to get all the N (1 <= N <= 18) of the cows to the ground floor using the least number of elevator rides. The sum of the weights of the cows on each elevator ride must be no larger than W.

给出n个物品,体积为w[i],现把其分成若干组,要求每组总体积<=W,问最小分组。(n<=18)

输入格式:
  • Line 1: N and W separated by a space.

  • Lines 2…1+N: Line i+1 contains the integer C_i, giving the weight of one of the cows.

输出格式:
  • A single integer, R, indicating the minimum number of elevator rides needed.

one of the R trips down the elevator.


题目分析

其实正解是状压来着
如果假设需要分组的物品必须连续,那么一个 n 2 n^2 n2的dp很容易想到

d p [ i ] dp[i] dp[i]表示将前 i i i个物品分组的最小组数
初始化 d p [ 0 ] = 0 dp[0]=0 dp[0]=0,其余全为INF
状态转移方程 d p [ i ] = m i n ( d p [ i ] , d p [ j ] + 1 ) ( s u m [ i ] − s u m [ j − 1 ] &lt; = m ) dp[i]=min(dp[i],dp[j]+1)(sum[i]-sum[j-1]&lt;=m) dp[i]=min(dp[i],dp[j]+1)(sum[i]sum[j1]<=m)其中sum为前缀和

那么现在物品可以不连续呢
我们可以模拟退火随机打乱原序列

当然这题的数据真的hin毒瘤,感觉故意卡退火啊
最后不得已采用了将询问区间小范围变动的方法才A

附赠双倍经验 CodeVS 4228 小猫爬山


#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
typedef double dd;
typedef long long lt;
#define T 1000
#define eps 1e-8
#define delta 0.98

lt read()
{
    lt f=1,x=0;
    char ss=getchar();
    while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
    while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
    return f*x;
}

const int maxn=50;
int n,m;
lt a[maxn],sum[maxn];
int dp[maxn],ans;

int DP()
{
    memset(dp,67,sizeof(dp)); dp[0]=0;
    for(int i=1;i<=n;++i) sum[i]=sum[i-1]+a[i];
    for(int i=1;i<=n;++i)
    for(int j=0;j<i;++j)
    if(sum[i]-sum[j]<=m)
    dp[i]=min(dp[i],dp[j]+1);
    return dp[n];
}

void SA()
{
    dd t=T; 
    while(t>eps)
    {
    	int x=0,y=0;
    	while(x==y) x=rand()%n+1,y=rand()%n+1;
    	swap(a[x],a[y]);
    	int tt=DP();
    	int dE=ans-tt;
    	if(dE>=0) ans=tt;
    	else if( exp(dE/t) > (dd)rand()/(dd)RAND_MAX ) ;
    	else swap(a[x],a[y]);
    	t*=delta;
    }
}

int main()
{
    srand(19260817);
    n=read();m=read()*1.05;
    for(int i=1;i<=n;++i) a[i]=read();
    
    ans=DP();
    for(int i=1;i<=5;++i) SA();
    printf("%d",ans);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值