列表

详情


NC24795. A Simple Problem

描述

Given an integer array A with length of N. Index starts from 1.
Given M queries, each query has an index interval as [L,R] and a value interval as [X,Y]. 
For every query, if the minimum value MIN of A[L...R] is no larger than Y and no less than X, the answer of this query is MIN, otherwise the answer is 0.
You need to construct an array to make the sum of the answers of all queries as large as possible.
Attention: The  elements in the array A shouldn't exceed 500000 or lower than 1.

输入描述

First line contains two integers N, M
Each of the next 5 lines represents a query. Each lines have four integers L, R, X, Y. The meaning of L, R, X, Y is the same as mentioned above.

输出描述

Print a single integer Z - the maximum possible sum of the answers of all queries.
After that print N integers A[1], A[2], ... , A[n], indicating the i-th number of the array A. If there are multiple solutions, please print any of them.

示例1

输入:

5 5
1 3 5 7 
2 4 7 8
3 5 9 9 
1 4 4 5
2 5 3 10

输出:

35
5 8 10 10 9

说明:

The answer of first query is 7 because 7 is the minimum number in 7, 10, 10(A[1], A[2], A[3]) and 7 >= 5 && 7 <= 7.
The answer of 4-th query is 0 because the minimum number in 7, 10, 10, 8 is 7 but 7 > 5.

原站题解

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

C++11(clang++ 3.9) 解法, 执行用时: 237ms, 内存消耗: 78052K, 提交时间: 2019-04-13 15:01:56

#include<bits/stdc++.h>
#define F first
#define S second
#define mk make_pair
#define pb push_back
using namespace std;
typedef pair<int,int> pii;
const int maxn = 35, maxm = 2010;
int l[maxm], r[maxm], c[maxm], d[maxm], n, m, a[maxn], ans[maxn];
pii mx[maxn][maxn][maxm*2], dp[maxn][maxn][maxm*2];
vector<int> v, vc[maxm*2], vd[maxm*2];

void get_ans(int L=1, int R=n, int val=0){
    if(L>R) return;
    int mxval = mx[L][R][val].S, pos = dp[L][R][mxval].S;
    ans[pos] = v[mxval];
    get_ans(L, pos-1, mxval); 
    get_ans(pos+1, R, mxval); 
}

// c[i]<=  <=d[i]
int main(){
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++){
        scanf("%d%d%d%d",l+i,r+i,c+i,d+i);
        v.pb(c[i]), v.pb(d[i]);
    }
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    for(int i=0;i<m;i++){
        c[i] = lower_bound(v.begin(),v.end(),c[i]) - v.begin();
	    d[i] = lower_bound(v.begin(),v.end(),d[i]) - v.begin();
        vc[c[i]].pb(i);
        vd[d[i]].pb(i);
    }
    int up = v.size();
    for(int L=n;L>=1;L--){
        for(int R=L;R<=n;R++){
            for(int i=L;i<=R;i++) a[i]=0;
            for(int mm=up-1;mm>=0;mm--){
                for(int i: vd[mm]) if(l[i]>=L&&r[i]<=R) a[l[i]]++, a[r[i]+1]--;
                int cnt = 0;
                for(int i=L;i<=R;i++){
                    cnt += a[i];
                    int value = mx[L][i-1][mm].F + mx[i+1][R][mm].F + cnt*v[mm];
                    if(value >= dp[L][R][mm].F) dp[L][R][mm] = mk(value, i);
                    //dp[L][R][mm] = max(dp[L][R][mm],
                    //mk(mx[L][i-1][mm].F+mx[i+1][R][mm].F+cnt*v[mm], i));
                }
                mx[L][R][mm] = max(mx[L][R][mm+1], mk(dp[L][R][mm].F, mm));
                for(int i: vc[mm]) if(l[i]>=L&&r[i]<=R) a[l[i]]--, a[r[i]+1]++;
            }
        }
    }
    get_ans();
    printf("%d\n", mx[1][n][0].F);
    for(int i=1;i<=n;i++) printf("%d%c", ans[i], " \n"[i==n]);
    return 0;
}

上一题