UVA - 11578(背包类dp)

首先,要对所有的要求,先按时间排序。然后只需对每一步若当前的要求和下一个时间相同,那么把他们两个同时安排,不同,则只安排当下的这个要求,选择有两种1,2

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <set>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef unsigned long long LLU;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rep1(i,n) for(int (i)=1;(i)<=(n);(i)++)
const int maxn = 20100;
int d[maxn][6][6],n,b[maxn];
struct node{
  int t,inc,id;
  int i,j,k,s1,s2;
  node(int i=0,int j=0,int k=0,int s1=0,int s2=0):
      i(i),j(j),k(k),s1(s1),s2(s2){}
  bool operator<(const node& a)const{
    return t<a.t||(t==a.t&&inc<a.inc);
  }
}a[maxn],pa[maxn][6][6];
void print_ans(int i,int j,int k){
  if(i == n+1) return ;
  node& te = pa[i][j][k];
  if(te.i == i+2){
     b[a[i].id] = te.s1; b[a[i+1].id] = te.s2;
  }
  else b[a[i].id] = te.s1;
  print_ans(te.i,te.j,te.k);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        memset(a,0,sizeof(a));
        int res = 15*n;
        rep1(i,n){
          scanf("%d %d",&a[i].t,&a[i].inc);
          a[i].inc/=10;
          a[i].id = i;
        }
        sort(a+1,a+1+n);
        for(int i=n+1;i>=1;i--)
         rep1(j,5) rep1(k,5){
            if(i == n+1){
             d[i][j][k] = (j+k-2)*10;
             continue;
            }
            d[i][j][k] = 1e9;
            int& ans = d[i][j][k];
            node& path=pa[i][j][k];
            if(i+1<=n&&a[i+1].t==a[i].t){
               int ans1 = d[i+2][a[i].inc][a[i+1].inc]+(abs(a[i].inc-j)+abs(a[i+1].inc-k))*10;
               int ans2 = d[i+2][a[i+1].inc][a[i].inc]+(abs(a[i+1].inc-j)+abs(a[i].inc-k))*10;
               if(ans1 <= ans2){
                  ans = ans1; path=node(i+2,a[i].inc,a[i+1].inc,1,2);
               }
               else {
                  ans = ans2; path=node(i+2,a[i+1].inc,a[i].inc,2,1);
               }
            }
            else{
                 int ans1 = d[i+1][a[i].inc][k]+abs(a[i].inc-j)*10;
                 int ans2 = d[i+1][j][a[i].inc]+abs(a[i].inc-k)*10;
               if(ans1<=ans2){
                  path = node(i+1,a[i].inc,k,1); ans = ans1;
              }
              else {
                 path =  node(i+1,j,a[i].inc,2); ans = ans2;
              }
            }
        }
        printf("%d\n",d[1][1][1]+res);
        print_ans(1,1,1);
        rep1(i,n) {
         printf("%d\n",b[i]);
       }
    }
    return 0;
}
/*
1
3
2 40
2 50
1 40
*/


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值