昇順にソートした状態が最も待ち時間が少なくなる。
「お客さんは 10,000 人以下で 1 人あたりに要する時間は 60 分以下とします。」
とあるのでlong long int で対応。
/******************************
* 2012/07/04 *
* AOJ_Volume1_0112 *
* A_Milk_Shop *
* crane *
*******************************/
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
#define MAX_N 100001
int a[MAX_N];
int main(){
int n;
while(cin >> n,n){
for(int i=0; i<n; i++)
cin >> a[i];
sort(a, a+n);//昇順にソート
long long int sum=0, tmp=0;
for(int i=0; i<n; i++){
sum += tmp;
tmp += a[i];
}
cout << sum << endl;
}
return 0;
}
Amazon
2012年7月4日水曜日
AOJ Volume0 0018: Sorting Five Number
何ヶ月か前にコーディングしたsort()を使わないものと今回コーディングしたsort()
を2つ
※降順注意
・昔の
/********************
* 2012/03/22 *
* crane *
*********************/
//降順に並び替え
#include<iostream>
using namespace std;
int Sorts_desc_order(int input[]);
int main(){
int input[5]; //5つの入力数値
for(int i=0; i<5; i++){
cin >> input[i];
}
Sorts_desc_order(input);
cout << input[0] << " "<<
input[1] << " "<< input[2] << " "<< input[3] << " "<< input[4]<< "\n";
return 0;
}
int Sorts_desc_order(int input[]){
int tmp; //入れ替え退避用
for(int i=4; i>=0; i--){
for(int j=0; j<i; j++){
if(input[j] < input[j+1]){
tmp = input[j+1];
input[j+1] = input[j];
input[j] = tmp;
}
}
}
return 0;
}
・今回の
/***********************
* 2012/07/04 *
* AOJ Volume0 0018 *
* Sorting Five Numbers*
* crane *
************************/
#include<iostream>
#include<functional>
#include<algorithm>
using namespace std;
int main(){
int in[5];
for(int i=0; i<5; i++)
cin >> in[i];
sort(in, in+5, greater<int>());
cout << in[0];
for(int i=1; i<5; i++)
cout << " " << in[i];
cout << endl;
}
を2つ
※降順注意
・昔の
/********************
* 2012/03/22 *
* crane *
*********************/
//降順に並び替え
#include<iostream>
using namespace std;
int Sorts_desc_order(int input[]);
int main(){
int input[5]; //5つの入力数値
for(int i=0; i<5; i++){
cin >> input[i];
}
Sorts_desc_order(input);
cout << input[0] << " "<<
input[1] << " "<< input[2] << " "<< input[3] << " "<< input[4]<< "\n";
return 0;
}
int Sorts_desc_order(int input[]){
int tmp; //入れ替え退避用
for(int i=4; i>=0; i--){
for(int j=0; j<i; j++){
if(input[j] < input[j+1]){
tmp = input[j+1];
input[j+1] = input[j];
input[j] = tmp;
}
}
}
return 0;
}
・今回の
/***********************
* 2012/07/04 *
* AOJ Volume0 0018 *
* Sorting Five Numbers*
* crane *
************************/
#include<iostream>
#include<functional>
#include<algorithm>
using namespace std;
int main(){
int in[5];
for(int i=0; i<5; i++)
cin >> in[i];
sort(in, in+5, greater<int>());
cout << in[0];
for(int i=1; i<5; i++)
cout << " " << in[i];
cout << endl;
}
ラベル:
AOJ,
AOJ_Volume0,
ソート
登録:
投稿 (Atom)