1. 程式人生 > >Little Girl and Maximum Sum

Little Girl and Maximum Sum

#include <iostream>
#include <algorithm>
using namespace std;

long  long a[200010],t[200010],ans;
int l,r,n,q;

int main()
{
	int i;
	while(cin>>n>>q)
	{
	   for(i=0;i<n;i++)t[i]=0;
		ans=0;
      for(i=0;i<n;i++)
      cin>>a[i];
	  sort(a,a+n);
	  while(q--)
	  {
		  cin>>l>>r;
		  t[l-1]++;
		  t[r]--;
	  }
	  for(i=1;i<n;i++)
		  t[i]+=t[i-1];
	  sort(t,t+n);
	  for(i=0;i<n;i++)
		  ans+=a[i]*t[i];
	  cout<<ans<<endl;
	}
	return 0;
}

這道題其實覺得演算法還是很精緻的,雖然只是道基礎題。。。我的程式碼是參考別人的寫出來的,不算自己想的,所以屬於轉載╮(╯_╰)╭。。。。一定要用long long 型的。。。

相關推薦

Codeforces 276C Little Girl and Maximum Sum(線段樹的區間更新)

The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array

Little Girl and Maximum Sum

#include <iostream> #include <algorithm> using namespace std; long long a[200010],t[200010],ans; int l,r,n,q; int main() {

Little Girl and Maximum XOR CodeForces - 276D

please exists ati present streams sid pri clas str A little girl loves problems on bitwise operations very much. Here‘s one of them. You

Little Girl and Maximum XOR(區間最大異或值--技巧)-------------C語言——菜鳥級

A little girl loves problems on bitwise operations very much. Here’s one of them. You are given two integers l and r. Let’s consid

Codeforces 276D. Little Girl and Maximum XOR

D. Little Girl and Maximum XOR time limit per test 2 seconds memory limit per test 256

CF276D Little Girl and Maximum XOR(思路題)

不知道該說些什麼。。。 #include <cstdio> #include <cstring> #include <algorithm> #include &

CodeForces - 276E Little Girl and Problem on Trees(線段樹)

題意 現在給你一棵這樣的樹,除了根節點外,其他的所有節點度都為2,也就是說除了根節點之外其他的節點都只能有一個節點,現在有兩種操作: 1。以當前節點為中心,距離為d的範圍內的點全部都加上x。 2。查詢某個點的值 思路* 觀察這樣一張圖,你會發現他們其實都是一條一條的鏈狀的,所以對於更新

Codeforces 454C - Little Pony and Expected Maximum

pre log 如果 tdi precision als pos def ems 454C - Little Pony and Expected Maximum 思路: m面的骰子擲n次,總共有m^n種情況,如果一種情況的最大值是m,那麽它肯定包含m,那我們在所有情況下

little w and Sum 牛客練習賽34

連結:https://ac.nowcoder.com/acm/contest/297/B 來源:牛客網     題解:暴力模擬  程式碼: #include <iostream> using namespace std; typedef

[CF453A] Little Pony and Expected Maximum【數論】

Pro QwQ Sol 如果投n次,最大點數是k,那麼情況共有kn−(k−1)nk^n-(k-1)^nkn−(k−1)n種。 若n次投擲的點數都在1到k內,共有knk^nkn種情況。 若n次投擲的點數都在1到k-1內,共有(k−1)n(k-1)^n(k−1)n

sincerit little w and Sum

連結:https://ac.nowcoder.com/acm/contest/297/B 來源:牛客網 題目描述 小w與tokitsukaze一起玩3ds上的小遊戲,現在他們遇到了難關。 他們得到了一個數列,通關要求為這個數列的和為0,並且只有一次改變一個數的符號的機會(正數變成負數

Maximum sum-動態規劃

ddl border 數組 clear fonts nts input gree img A - Maximum sum Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d

poj 2479 Maximum sum(遞推)

宋體 思路 freopen sim ace [1] clu mat while ?? 題意:給定n個數,求兩段連續不重疊子段的最大和。 思路非常easy。把原串劃為兩段。求兩段的連續最大子串和之和,這裏要先預處理一下,用lmax數組表示1到i的最大連續子串和,用rmax

check whether the subset(no need to be consective) and be sum of X

subsets [] bool else true ret code art size #include "stdafx.h" bool isSubsetSum(int set[], int n, int sum) { bool sumOfSubSet[100][1

Codeforces Round #365 (Div. 2) D - Mishka and Interesting sum(離線樹狀數組)

turn string 之前 algorithm printf ace r++ void contest http://codeforces.com/contest/703/problem/D 題意: 給出一行數,有m次查詢,每次查詢輸出區間內出現次數為偶數次的數字的異

codechef Little Elephant and Bombs題解

except tput 情況 urn only text rom amp pos The Little Elephant from the Zoo of Lviv currently is on the military mission. There are

HDU - 4422-The Little Girl who Picks Mushrooms

pac names pic 兩個 can break hdu tle http 題目鏈接:https://vjudge.net/problem/HDU-4422 題目大意: 自行百度 題目分析: 當n<=3的時候,易得可以得到的最多的蘑菇是1024。 當n>3時

Little Elephant and Elections CodeForces - 258B

clu == ttl 10個 最大值 ant ref 題意 col Little Elephant and Elections CodeForces - 258B 題意:給出m,在1-m中先找出一個數x,再在剩下數中找出6個不同的數y1,...,y6,使得y1到y6中數字

689. Maximum Sum of 3 Non-Overlapping Subarrays

then sub lis .com this phi multiple dice start Max Sum of Subarray with size k, 相當於Easy難度,我說用一個sum array存sum,然後做減法就行。中國小哥說讓優化空間,於是說可以只用兩個

Codeforces 453B Little Pony and Harmony Chest:狀壓dp【記錄轉移路徑】

輸出 兩個 cal bsp tac 可能 pan 才有 line 題目鏈接:http://codeforces.com/problemset/problem/453/B 題意:   給你一個長度為n的數列a,讓你構造一個長度為n的數列b。   在保證b中任意兩數gcd