2019The 44th International Collegiate Programming Contest Asia Shenyang Regional Contest

2019ICPC沈阳重现赛

2019The 44th International Collegiate Programming Contest Asia Shenyang Regional Contest_第1张图片
比赛链接

Problem A. Leftbest

Description

Jack is worried about being single for his whole life, so he begins to use a famous dating app. In this
app, the user is shown single men/women’s photos one by one, and the user may choose between “yes”
and “no”. Choosing “yes” means an invitation while choosing “no” means nothing. The photos would be
shown one by one until the number of rest photos to be shown reaches zero. Of course, efficient and single Jack would always choose “yes”.
When viewing photos, Jack would have a “fake impression point” on every photo, which is not accurate.
To calculate the “true impression point” of one photo, Jack would recall the “fake impression point” of
every previous photo whose “fake impression point” is larger than this photo, and regard the smallest
“fake impression point” of them as the “true impression point” of this photo. Jack would like to sum the
“true impression point” of all photos as the outcome of his effffort.
Note that if such a larger “fake impression point” does not exist, the “true impression point” of this photo
is zero.

Input

The fifirst line contains an integer n (1 ≤ n ≤ 100 000) — the number of photos.
The second line contains n integers a1, a2, . . ., an where ai (0 ≤ ai ≤ 10^8) is the “fake impression point”
of the i-th photo.

Output

Output a single integer — the sum of the “true impression point” of all photos.

2019The 44th International Collegiate Programming Contest Asia Shenyang Regional Contest_第2张图片

题意

给出一个列数,每个数找出其前面的数中比他大的数中最小的一个,求这些值的和。
PS:暴力肯定会超时,所以用了复杂度最低的排序sort,然后再用upper_bound寻找,代码如下:

#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
const int N=1e6+7;
const int INF=0x3f3f3f3f;
int a[N];
int main()
{
	int n;
	cin >> n;
	ll sum = 0;
	for(int i = 0; i < n; i++)
	{
		cin >> a[i];
		if(i == 1) a[1] < a[0] ? sum += a[0] : sum += 0;
		if( i > 1 ) 
		{
			sort(a,a+i);
			int p = upper_bound(a,a+i,a[i]) - a;
			if(p != i)sum += a[p];
		}
	}
	cout << sum << endl;

	return 0;
}

结果还是超时了。。。
看来是每次都排序复杂度仍然太大,此时更好的解决方案:set,自动排序
能AC:

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
const int N=1e6+7;
const int INF=0x3f3f3f3f;
set<int>a;
int b[N],c[N];
int main()
{
	int n;
	cin >> n;
	for(int i=0;i<n;i++)
	{
		cin >> b[i];
	}
	for(int i=0;i<n;i++)
	{
		a.insert(b[i]);
		set<int>::iterator it = a.find(b[i]);
		it++;
		if(it == a.end())
			c[i] = 0;
		else c[i] = *it;
	}
	ll sum = 0;
	for(int i = 0 ; i < n ; i++)
	{
		sum += c[i];
	}
	cout << sum << endl;
}

题解说的也差不多
2019The 44th International Collegiate Programming Contest Asia Shenyang Regional Contest_第3张图片

你可能感兴趣的:(初生菜鸟,比赛)