Minimum Sum(思维+set)

5599: Minimum Sum

时间限制: 2 Sec   内存限制: 256 MB
提交: 141   解决: 47
[ 提交][ 状态][ 讨论版][命题人: admin]

题目描述

One day, Snuke was given a permutation of length N, a1,a2,…,aN, from his friend.

Find the following:

Constraints
1≤N≤200,000
(a1,a2,…,aN) is a permutation of (1,2,…,N).

输入

The input is given from Standard Input in the following format:
N
a1 a2 … aN

输出

Print the answer.
Note that the answer may not fit into a 32-bit integer.

样例输入

3

2 1 3

样例输出

9

提示

来源

AtCoder Grand Contest 005 


题解:记录下每个数的位置,然后按照数的大小排序,对于每一个数字,在set中查找大于它下标的数字的最小值,和小于它下标的最大值,则这个区间就是这个数字的影响区间,然后再将位置加入set集合中。同时注意set的初始化,加入0和n+1两个位置。

#include<stdio.h>
#include <algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define INF 0x3f3f3f3f
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;

inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}

#define it set<int>::iterator
struct node
{
    ll v;
    int indx;
};
const int maxn=200000;

node a[maxn];

int cmp(node a,node b){return a.v<b.v;}
int main()
{
   int n;
   cin>>n;
   for(int i=1;i<=n;i++)
   {
       scanf("%lld",&a[i].v);
       a[i].indx=i;
   }
   sort(a+1,a+1+n,cmp);
   set<int>s;
   s.insert(0);
   s.insert(n+1);
   ll ans=0;
   for(int i=1;i<=n;i++)
   {
        it tail,head;
        tail=s.upper_bound(a[i].indx);
        head=s.upper_bound(a[i].indx);
        head--;
        ans+=a[i].v*(*tail-a[i].indx)*(a[i].indx-*head);
        s.insert(a[i].indx);
   }
   printf("%lld\n",ans);
}

Here's an implementation of the program in C: ```c #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <math.h> // Global variables for storing statistical values double average = 0.0; int minimum = 0; int maximum = 0; double median = 0.0; double std_dev = 0.0; // Struct for passing arguments to worker threads typedef struct { int* numbers; int count; } thread_args; // Worker thread for calculating average void* calculate_average(void* args) { thread_args* targs = (thread_args*) args; int* numbers = targs->numbers; int count = targs->count; double sum = 0.0; for (int i = 0; i < count; i++) { sum += numbers[i]; } average = sum / count; pthread_exit(NULL); } // Worker thread for calculating minimum void* calculate_minimum(void* args) { thread_args* targs = (thread_args*) args; int* numbers = targs->numbers; int count = targs->count; minimum = numbers[0]; for (int i = 1; i < count; i++) { if (numbers[i] < minimum) { minimum = numbers[i]; } } pthread_exit(NULL); } // Worker thread for calculating maximum void* calculate_maximum(void* args) { thread_args* targs = (thread_args*) args; int* numbers = targs->numbers; int count = targs->count; maximum = numbers[0]; for (int i = 1; i < count; i++) { if (numbers[i] > maximum) { maximum = numbers[i]; } } pthread_exit(NULL); } // Worker thread for calculating median void* calculate_median(void* args) { thread_args* targs = (thread_args*) args; int* numbers = targs->numbers; int count = targs->count; int* sorted_numbers = malloc(count * sizeof(int)); for (int i = 0; i < count; i++) { sorted_numbers[i] = numbers[i]; } for (int i = 0; i < count - 1; i++) { for (int j = i + 1; j < count; j++) { if (sorted_numbers[i] > sorted_numbers[j]) { int temp = sorted_numbers[i]; sorted_numbers[i] = sorted_numbers[j]; sorted_numbers[j] = temp; } } } if (count % 2 == 0) { median = (sorted_numbers[count / 2 - 1] + sorted_numbers[count / 2]) / 2.0; } else { median = sorted_numbers[count / 2]; } free(sorted_numbers); pthread_exit(NULL); } // Worker thread for calculating standard deviation void* calculate_std_dev(void* args) { thread_args* targs = (thread_args*) args; int* numbers = targs->numbers; int count = targs->count; double sum = 0.0; for (int i = 0; i < count; i++) { sum += pow(numbers[i] - average, 2); } std_dev = sqrt(sum / count); pthread_exit(NULL); } int main(int argc, char* argv[]) { // Parse command line arguments if (argc < 2) { printf("Usage: %s <number1> <number2> ... <numberN>\n", argv[0]); return 1; } int count = argc - 1; int* numbers = malloc(count * sizeof(int)); for (int i = 0; i < count; i++) { numbers[i] = atoi(argv[i + 1]); } // Create worker threads pthread_t threads[5]; thread_args targs = {numbers, count}; pthread_create(&threads[0], NULL, calculate_average, &targs); pthread_create(&threads[1], NULL, calculate_minimum, &targs); pthread_create(&threads[2], NULL, calculate_maximum, &targs); pthread_create(&threads[3], NULL, calculate_median, &targs); pthread_create(&threads[4], NULL, calculate_std_dev, &targs); // Wait for worker threads to complete for (int i = 0; i < 5; i++) { pthread_join(threads[i], NULL); } // Print results printf("The average value is %.2f\n", average); printf("The minimum value is %d\n", minimum); printf("The maximum value is %d\n", maximum); printf("The median value is %.2f\n", median); printf("The standard deviation is %.2f\n", std_dev); // Clean up free(numbers); return 0; } ``` The program takes a list of numbers as command line arguments, creates five worker threads to calculate statistical values, and then outputs the results once the worker threads have completed their calculations. Note that the median calculation uses a simple sorting algorithm to sort the list of numbers first. This may not be efficient for very large lists of numbers, but it works well enough for small lists. Also note that the standard deviation calculation uses the formula for sample standard deviation, which divides by N-1 instead of N to correct for bias.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值