Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 1131 | Accepted: 384 |
Description
We consider a set of jobs 1, 2,..., n having processing times t1, t2,...,tn respectively. Job i arrives at time ai and has its deadline at time di. We assume that ti, ai, and di have nonnegative integral values. The jobs have hard deadlines, meaning that each job i can only be executed during its allowed interval Ii=[ai, di]. The jobs are executed by the worker, and the worker executes only one job at a time. Once a job is begun, it must be completed without interruptions. When a job is completed, another job must begin immediately, if one exists to be executed. Otherwise, the worker is idle and begins executing a job as soon as one arrives. You should note that for each job i, the length of Ii, di - ai, is greater than or equal to ti, but less than 2*ti.
Write a program that finds the minimized total amount of time executed by the worker.
Input
Output
Sample Input
3 3 15 0 25 50 0 90 45 15 70 3 15 5 20 15 25 40 15 45 60 5 3 3 6 3 6 10 3 14 19 6 7 16 4 4 11
Sample Output
50 45 15
題意:有一個天平,若干砝碼,要用上所有砝碼,使天平處於平衡狀態,問有幾種不同情況。
當放上去的砝碼不同時,天平會有不同的平衡狀態,設爲j,當j<0時表示左邊重;當j>0時表示右邊重;當j=0時則天平處於平衡。
爲了使數組下標不出現負數情況,要用到狀態轉移(見代碼),所以,當j=7500時爲平衡狀態。
/*完全揹包*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int c,g,hook[25],weight[25];
int dp[25][15050];
//dp[i][j]表示放上第i個後的平衡係數爲j的possobilities。
//爲了使數組下標不出現負數情況,狀態轉移。[-20*15*25 ~ 20*15*25]->[0 ~ 2*20*15*25]即[0 ~ 15000]
//dp[i][7500]是平衡狀態
int main()
{
//freopen("in.txt","r",stdin);
memset(dp,0,sizeof(dp));
scanf("%d%d",&c,&g);
for(int i = 0; i < c; i++)
scanf("%d",&hook[i]);
for(int i= 0; i < g; i++)
scanf("%d",&weight[i]);
dp[0][7500]=1;
for(int i = 0; i < g; i++)
{
for(int j = 0; j <= 15000; j++)
{
if(dp[i][j])
for(int k = 0; k < c; k++)
dp[i + 1][j + weight[i] * hook[k]] += dp[i][j];
}
}
printf("%d\n",dp[g][7500]);
}