首页 > 代码库 > [IOI2002] 任务安排

[IOI2002] 任务安排

666的一道题目

考场花了1个半小时写斜率优化然后发现自己太蠢还是先去打暴力 结果没开long long痛失40分

暴力如下

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<string>
 7 #define LL long long
 8 using namespace std;
 9 int n,s;
10 int t[6000],f[6000];
11 LL dp[2010][2010];
12 int main(){
13     freopen ("batch.in","r",stdin);
14     freopen ("batch.out","w",stdout);
15     scanf ("%d%d",&n,&s);
16     int x,y;
17     t[0]=0,f[0]=0;
18     for (int i=1;i<=n;++i){
19         scanf ("%d%d",&x,&y);
20         t[i]=t[i-1]+x;
21         f[i]=f[i-1]+y;
22     }
23     memset(dp,127,sizeof(dp));
24     dp[0][0]=0;
25     for (int i=1;i<=n;++i)
26             for (int j=0;j<i;++j)
27                 for (int k=0;k<=j;++k)
28                 if (dp[j][k]+(f[i]-f[j])*(t[i]+s*(k+1))<=dp[i][k+1]){
29                     dp[i][k+1]=dp[j][k]+(f[i]-f[j])*(t[i]+s*(k+1));
30                 }
31     LL ans=10000000000;
32     for (int i=1;i<=n;++i) ans=min(ans,dp[n][i]);
33     cout<<ans;
34     return 0;
35   }

下午心痛一会儿之后开始看题解发现居然可以倒推qwq 出人意料的 n方在王老师的数据底下居然很快

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<string>
 7 #define LL long long
 8 using namespace std;
 9 int n,s;
10 LL t[6000],f[6000],tt[6000],ff[6000],dp[6010];
11 int main(){
12     freopen ("batch.in","r",stdin);
13     freopen ("batch.out","w",stdout);
14     scanf ("%d%d",&n,&s);
15     int x,y;
16     for (int i=1;i<=n;++i) scanf ("%d%d",&tt[i],&ff[i]);
17         for (int i=n;i>=1;--i) t[i]=t[i+1]+tt[i],f[i]=f[i+1]+ff[i];
18     memset(dp,127,sizeof(dp));
19     dp[n+1]=0;
20     for (int i=n;i>=1;--i)
21         for (int j=i+1;j<=n+1;++j)  
22                     dp[i]=min(dp[i],dp[j]+(t[i]-t[j]+s)*f[i]);
23     cout<<dp[1];
24     return 0;
25   }                

而且这个代码好短阿OvO

 

[IOI2002] 任务安排