最优时间表问题(sche)

算法:DP

分析:本题算法同尼克的任务一样,都是用f[i]来表示i分钟开始之后能获得的最大空闲时间,因此最后的n-f[1]即为最小工作时间。
            先是去寻找起始时间在i之后或等于i的,对于起始时间等于i的,那么就要不断更新取最大值。
            直到到起始时间位于i之后的第一个任务,如果它的下一个任务不起始于i,那么i就能获得一个空闲时间。

program sche;

const
 maxn=10000;

var
 n,k:longint;
 f,st,last:array [0..maxn] of longint;
 
procedure init;
var
 i:longint;
begin
 readln(n,k);
 for i:=1 to k do readln(st[i],last[i]);
end;

procedure main;
var
 i,j:longint;
begin
 j:=k;
 for i:=n downto 1 do
  begin
   while (st[j]>=i) and (j>=1) do
    begin
     if st[j]=i then if f[i+last[j]]>f[i] then f[i]:=f[i+last[j]];
     dec(j);
    end;
   if st[j+1]<>i then f[i]:=f[i+1]+1;
  end;
end;

begin
 assign(input,'sche.in'); reset(input);
 assign(output,'sche.out'); rewrite(output);
 
 init;
 main;
 writeln(n-f[1]);
 
 close(input); close(output);
end.


 

private int ScheDataWorkingFileToDbOneHour(String yyyymmdd, int hh, String sche, int spos, int stratPos, int startMin, int minSize, int pos_N) { int i; String cp = "2"; String Gcp; int count = 0; int cShift = -1; int oShift = -1; for (i = 0; i < minSize; i++) { Gcp = sche.substring(i, i + 1); cShift = GetShift(stratPos + i); if (m_StartHour == hh) { //시작시간과 같은 시간대 비교 if (pos_N >= 24) { // 24시간을 넘어가는 경우 if (!cp.equals(Gcp) || oShift != cShift) { cp = Gcp; dbtemp[spos + count].use = true; dbtemp[spos + count].yyyymmdd = yyyymmdd; dbtemp[spos + count].hh = hh; dbtemp[spos + count].st = startMin + i * IntervalMin; dbtemp[spos + count].shift = 5; //dbtemp[spos + count].shift = cShift; if (cp.equals("1")) { dbtemp[spos + count].rs = "1"; } else { dbtemp[spos + count].rs = "2"; } oShift = cShift; count++; } dbtemp[spos + count - 1].et = startMin + (i + 1) * IntervalMin - 1; } else { if (m_StartMin <= (startMin + i * IntervalMin)) { if (!cp.equals(Gcp) || oShift != cShift) { cp = Gcp; dbtemp[spos + count].use = true; dbtemp[spos + count].yyyymmdd = yyyymmdd; dbtemp[spos + count].hh = hh; dbtemp[spos + count].st = startMin + i * IntervalMin; dbtemp[spos + count].shift = cShift; if (cp.equals("1")) { dbtemp[spos + count].rs = "1"; } else { dbtemp[spos + count].rs = "2"; } oShift = cShift; count++; } dbtemp[spos + count - 1].et = startMin + (i + 1) * IntervalMin - 1; } } } else { if (pos_N >= 24) { // 24시간을 넘어가는 경우 if (!cp.equals(Gcp) || oShift != cShift) { cp = Gcp; dbtemp[spos + count].use = true; dbtemp[spos + count].yyyymmdd = yyyymmdd; dbtemp[spos + count].hh = hh; dbtemp[spos + count].st = startMin + i * IntervalMin; dbtemp[spos + count].shift = 5; if (cp.equals("1")) { dbtemp[spos + count].rs = "1"; } else { dbtemp[spos + count].rs = "2"; } oShift = cShift; count++; } dbtemp[spos + count - 1].et = startMin + (i + 1) * IntervalMin - 1; } else { if (!cp.equals(Gcp) || oShift != cShift) { cp = Gcp; dbtemp[spos + count].use = true; dbtemp[spos + count].yyyymmdd = yyyymmdd; dbtemp[spos + count].hh = hh; dbtemp[spos + count].st = startMin + i * IntervalMin; dbtemp[spos + count].shift = cShift; if (cp.equals("1")) { dbtemp[spos + count].rs = "1"; } else { dbtemp[spos + count].rs = "2"; } oShift = cShift; count++; } dbtemp[spos + count - 1].et = startMin + (i + 1) * IntervalMin - 1; } } //dbtemp[spos + count - 1].et = startMin + i; } return count; }
05-29
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值