列表

详情


NC222151. ChinoWithMinimum

描述

Chino has an array a with  elements. Now she prepares  different arithmetic sequence. The first term of the  arithmetic sequence is , the tolerance is k_i, and the length of the sequence is exactly .

The element of  marked and marked with same position in respective array. The element in in Same position marked . is equal to minus . ( )

For each array , marked Minimum_i is the minimum element in c_i.

That is

Now Chino want to know for each different array b_i, what are the generated Minimum_i.

输入描述

In the first line, input two integer  means the length of the array the numbers of arithmetic sequence.

In the next line, input non-negative integers

In the next  lines, input two integers

输出描述

Output  lines, Represents each Minimum_i.

示例1

输入:

5 3
1 9 2 4 3
10 -1
1 100
403 -100

输出:

0
0
0

说明:

b_1=\{10,9,8,7,6\},c_1= \{9,0,6,3,3\},The minimum value is {0}

b_2=\{1,101,201,301,401\},c_2= \{0,92,199,297,398\},The minimum value is {0}

b_3=\{403,303,203,103,3\},c_3= \{401,294,201,99,0\},The minimum value is {0}

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 61ms, 内存消耗: 7160K, 提交时间: 2021-05-22 17:23:10

#include<bits/stdc++.h>
using namespace std;
const int MAXN=100005;
pair<long long,long long>que[MAXN];///<pos,val>
int head,tail,n,m;
struct node
{
    int id;
    long long a,k;
}query[MAXN];
long long ans[MAXN],a[MAXN];
int main()
{
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;++i)
    {
        scanf("%lld",&a[i]);
    }
    for(int i=1;i<=m;++i)
    {
        scanf("%lld %lld",&query[i].a,&query[i].k);
        query[i].id=i;
    }
    sort(query+1,query+1+m,[](const node&A,const node&B){
         return A.k>B.k;
    });
    for(int i=1;i<=n;++i)
    {
        while(tail>=2&&(a[i]-que[tail].second)*(i-que[tail-1].first)>(a[i]-que[tail-1].second)*(i-que[tail].first))tail--;
        que[++tail]=make_pair(i,a[i]);
    }
    head=1;
    for(int i=1;i<=m;++i)
    {
        while(head<tail&&(que[head].first-1)*query[i].k-que[head].second>=(que[head+1].first-1)*query[i].k-que[head+1].second)head++;
        ans[query[i].id]=query[i].a+(que[head].first-1)*query[i].k-que[head].second;
    }
    for(int i=1;i<=m;++i)
    {
        printf("%lld\n",ans[i]);
    }
    return 0;
}

上一题