Codeforces 1326C. Permutation Partitions

Description

You are given a permutation p1,p2,…,pn of integers from 1 to n and an integer k, such that 1≤k≤n. A permutation means that every number from 1 to n is contained in p exactly once.

Let’s consider all partitions of this permutation into k disjoint segments. Formally, a partition is a set of segments {[l1,r1],[l2,r2],…,[lk,rk]}, such that:

1≤li≤ri≤n for all 1≤i≤k;
For all 1≤j≤n there exists exactly one segment [li,ri], such that li≤j≤ri.
Two partitions are different if there exists a segment that lies in one partition but not the other.

Let’s calculate the partition value, defined as ∑i=1kmaxli≤j≤ripj, for all possible partitions of the permutation into k disjoint segments. Find the maximum possible partition value over all such partitions, and the number of partitions with this value. As the second value can be very large, you should find its remainder when divided by 998244353.

Input

The first line contains two integers, n and k (1≤k≤n≤200000) — the size of the given permutation and the number of segments in a partition.

The second line contains n different integers p1,p2,…,pn (1≤pi≤n) — the given permutation.

Output

Print two integers — the maximum possible partition value over all partitions of the permutation into k disjoint segments and the number of such partitions for which the partition value is equal to the maximum possible value, modulo 998244353.

Please note that you should only find the second value modulo 998244353.

Note

In the first test, for k=2, there exists only two valid partitions: {[1,1],[2,3]} and {[1,2],[3,3]}. For each partition, the partition value is equal to 2+3=5. So, the maximum possible value is 5 and the number of partitions is 2.

In the third test, for k=3, the partitions with the maximum possible partition value are {[1,2],[3,5],[6,7]}, {[1,3],[4,5],[6,7]}, {[1,4],[5,5],[6,7]}, {[1,2],[3,6],[7,7]}, {[1,3],[4,6],[7,7]}, {[1,4],[5,6],[7,7]}. For all of them, the partition value is equal to 7+5+6=18.

The partition {[1,2],[3,4],[5,7]}, however, has the partition value 7+3+6=16. This is not the maximum possible value, so we don’t count it.

题目大意

给出一个数组p,要求将这个数组划分为k个区间,value为这k个区间的最大元素和,
输出最大的value以及有多少种划分能够获得这个最大的value(对998244353取模)

思路

对于最大的value,贪心取最大的k个。对于划分种类,我们需要将这k个元素分开,对于两个元素,它俩下标的差就是将它俩分开的种类数,要将k个元素分开,需要分k-1次,每次都是独立的,要累乘。
所以将最大的k个元素的下标从小到大排序,将两两间的下标差相乘就是划分种类数

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=2e5+5;
pair<int,int> a[maxn];
int b[maxn];
bool cmp(pair<int,int> a,pair<int,int> b){
	return a.first>b.first;
}
int main(){
    int n,k;
	scanf("%d %d",&n,&k);
	for(int i=1;i<=n;++i){
		scanf("%d",&a[i].first);
		a[i].second=i;
	}
	sort(a+1,a+n+1,cmp);
	for(int i=1;i<=k;++i){
		b[i]=a[i].second;
	}
	sort(b+1,b+k+1);
	ll ans=0,ans_num=1;
	for(int i=1;i<=k;++i)ans+=a[i].first;
	for(int i=2;i<=k;++i){
		ans_num=(ans_num*(b[i]-b[i-1]))%998244353;
	}
	printf("%lld %lld\n",ans,ans_num);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43984169/article/details/105613170