Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

算法描述

Posted by zhouzhendong at 2017-02-26 11:49:22 on Problem 2393
In Reply To:超短的0MS的代码 Posted by:zhouzhendong at 2017-02-26 11:39:59
    对于这道题,我们先考虑n^2的算法:
  1.对于第i天,如果第j天(i<j<=n)要使用第i天存储的酸奶,那么其单价是:
      p=c[i]+s*(j-i), 把p与c[j]比较,可以刷新c[j],使c[j]形成一个更小的值
  刷新完所有的c[j]之后,把价格累乘即可。
不难写出n^2的代码:

#include <cstdio>
typedef long long ll;
struct AC{
	ll c,y;
	bool flag;
}r[11000];
ll n,s;
int main(){
	scanf("%d%d",&n,&s);
	for (int i=1;i<=n;i++){
		scanf("%d%d",&r[i].c,&r[i].y);
		r[i].flag=false;
	}
	ll ans=0;
	for (int i=1;i<=n;i++){
		ans+=r[i].c*r[i].y;
		if (r[i].flag) 
			continue;
		for (int j=i+1;j<=n;j++)
			if (r[j].c>r[i].c+s*(j-i)){
				r[j].c=r[i].c+s*(j-i);
				r[j].flag=true;
			}
	}
	printf("%lld",ans);
	return 0;
}
注:这个代码没有测过,样例可过,速度未知(2000MS应该可过),但是容易理解。

2.从n^2推O(n)的算法
   后来,我发现,刷新这个最小值,每一个只要刷新一次。详见代码。
   保存一个min值,表示每一天为止的最小花费。思路和之前的类似。

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator