五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

合并果子 (貪心+排序應(yīng)用) 提高組

2022-10-24 12:36 作者:信奧賽USACO鄭老師  | 我要投稿

//n==10**7 timeout

#include<bits/stdc++.h>>

using namespace std;


int main(){

? ? std::ios_base::sync_with_stdio(false);

? ? cin.tie(0);? ??

? ? int n;

? ? cin>>n;

? ? multiset<long long> a;

? ? for(int i=0;i<n;i++){

? ? ? ? long long tmp;

? ? ? ? cin>>tmp;

? ? ? ? a.insert(tmp);

? ? }

? ? long long ans=0;

? ? while(a.size()>=2){

? ? ? ? long long x=*a.begin();

? ? ? ? a.erase(a.begin());

? ? ? ? long long y=*a.begin();

? ? ? ? a.erase(a.begin());

? ? ? ? ans+=x+y;

? ? ? ? a.insert(x+y);

? ? }

? ? cout<<ans<<endl;

? ? return 0;

}


//桶排序應(yīng)用,O(n), 兩個有序隊列的歸并O(m+n)

#include <iostream>

#include <queue>

typedef long long ll;

using namespace std;

queue <ll> q1;

queue <ll> q2;

ll to[100005];

signed main() {

? ? std::ios_base::sync_with_stdio(false);

? ? cin.tie(0);? ??

int n;

cin>>n;

for (int i = 1; i <= n; ++i) {

ll a;

cin>>a;

to[a] ++;

}

for (int i = 1; i <= 100000; ++i) {

while(to[i]) {

to[i] --;

q1.push(i);

}

}

ll ans = 0;

for (int i = 1; i < n; ++i) {

ll x , y;

if((q1.front() < q2.front() && !q1.empty()) || q2.empty()) {

x = q1.front();

q1.pop();

}

else {

x = q2.front();

q2.pop();

}

if((q1.front() < q2.front() && !q1.empty()) || q2.empty()) {

y = q1.front();

q1.pop();

}

else {

y = q2.front();

q2.pop();

}

ans += x + y;

q2.push(x + y);

}?

cout<<ans<<endl;

return 0;

}?


//桶排序應(yīng)用,O(n), 兩個有序隊列的歸并O(m+n)

#include <cstdio>

#include <queue>

#define int long long

using namespace std;

queue <int> q1;

queue <int> q2;

int to[100005];

void read(int &x){?

int f=1;x=0;char s=getchar();

while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}

while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}

x*=f;

}

signed main() {

int n;

read(n);

for (int i = 1; i <= n; ++i) {

int a;

read(a);

to[a] ++;

}

for (int i = 1; i <= 100000; ++i) {

while(to[i]) {

to[i] --;

q1.push(i);

}

}

int ans = 0;

for (int i = 1; i < n; ++i) {

int x , y;

if((q1.front() < q2.front() && !q1.empty()) || q2.empty()) {

x = q1.front();

q1.pop();

}

else {

x = q2.front();

q2.pop();

}

if((q1.front() < q2.front() && !q1.empty()) || q2.empty()) {

y = q1.front();

q1.pop();

}

else {

y = q2.front();

q2.pop();

}

ans += x + y;

q2.push(x + y);

}?

printf("%lld" , ans);

return 0;

}?


合并果子 (貪心+排序應(yīng)用) 提高組的評論 (共 條)

分享到微博請遵守國家法律
江津市| 宁武县| 徐州市| 化州市| 门源| 治多县| 双江| 玛沁县| 安吉县| 新巴尔虎右旗| 若羌县| 朝阳县| 塘沽区| 厦门市| 吉木萨尔县| 攀枝花市| 云南省| 黄石市| 常熟市| 广德县| 垦利县| 新泰市| 固阳县| 双柏县| 桓台县| 汕尾市| 高平市| 泰安市| 三台县| 南城县| 宁海县| 永清县| 枣阳市| 西乌| 无棣县| 平江县| 福海县| 兰考县| 怀集县| 云南省| 建始县|