原题:
E. Post Lamps
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Adilbek’s house is located on a street which can be represented as the OX axis. This street is really dark, so Adilbek wants to install some post lamps to illuminate it. Street has n positions to install lamps, they correspond to the integer numbers from 0 to n−1 on the OX axis. However, some positions are blocked and no post lamp can be placed there.
There are post lamps of different types which differ only by their power. When placed in position x, post lamp of power l
illuminates the segment [x;x+l]. The power of each post lamp is always a positive integer number.
The post lamp shop provides an infinite amount of lamps of each type from power 1 to power k. Though each customer is only allowed to order post lamps of exactly one type. Post lamps of power l cost ai each.
What is the minimal total cost of the post lamps of exactly one type Adilbek can buy to illuminate the entire segment
[0;n] of the street? If some lamps illuminate any other segment of the street, Adilbek does not care, so, for example, he may place a lamp of power 3 in position n−1 (even though its illumination zone doesn’t completely belong to segment
[0;n]).
Input
The first line contains three integer numbers n, m and k (1≤k≤n≤10^6, 0≤m≤n) — the length of the segment of the street Adilbek wants to illuminate, the number of the blocked positions and the maximum power of the post lamp available.
The second line contains m integer numbers s1,s2,…,sm (0≤s1
#include
using namespace std;
typedef long long ll;
const int maxn=1000001;
const ll inf=1e18;
int n,m,k;
bool block[maxn];
ll cost[maxn];
int road[maxn];
int main()
{
ios::sync_with_stdio(false);
while(cin>>n>>m>>k)
{
memset(block,0,sizeof(block));
memset(road,0,sizeof(road));
ll res;
for(int i=1;i<=m;i++)
{
cin>>res;
block[res]=1;
}
for(int i=1;i<=k;i++)
cin>>cost[i];
if(block[0])
{
cout<<-1<continue;
}
int st=-1;
for(int i=0;i<=n;i++)
{
if(block[i])
road[i]=road[i-1]+1;
st=max(st,road[i]);
}
ll ans=inf;
res=0;
int flag,tmp;
for(int i=st+1;i<=k;i++)
{
tmp=flag=res=0;
int j=0;
while(jif(block[j])
j-=road[j];
res++;
if(tmp>0&&j<=0)
{
flag=1;
break;
}
j+=i;
tmp++;
}
if(!flag)
ans=min(ans,cost[i]*res);
}
if(ans>=inf)
cout<<-1<else
cout<return 0;
}
思路:
看到数据范围的时候一惊,非得小于nlogn不可~
考虑动态规划
设置 dp[i] d p [ i ] 表示照亮到第i点时候的最小花费, len[i] l e n [ i ] 表示第i个灯的照明范围, cost[i] c o s t [ i ] 表示第i个灯的价格。
转移方程 dp[i]=min(dp[j]+cost[k])(k=i−j)且j+1可以放置路灯 d p [ i ] = m i n ( d p [ j ] + c o s t [ k ] ) ( k = i − j ) 且 j + 1 可 以 放 置 路 灯
然而时间复杂度是o(n^2),抓耳挠腮想不出怎么优化来,看了题解发现,原来题目中有一句话“ each customer is only allowed to order post lamps of exactly one type.(每次只能使用一种灯) ”没看见-_-
那么这道题就是暴力的解法了,求解的时候要先经过预处理,时间复杂度是n/1+n/2+..+n/2=nlogn