排队接水
.
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int *a;
bool cmp(int x, int y)
{
return a[x] < a[y];
}
int main()
{
int n;
double ans = 0;
cin >> n;
int *b = new int [n+1];
a = new int [n+1];
for(int i = 1; i <= n; ++i) {
cin >> a[b[i] = i];
}
sort(b+1, b+n+1, cmp);
for(int i = 1; i <= n; ++i) {
cout << b[i] << ' ';
ans += a[b[i]]*(n - i);
}
cout << endl;
printf("%.2f", ans/n);
}
纪念品分组
.
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int w, n;
cin >> w >> n;
int *a = new int [n];
for(int i = 0; i < n; ++i) {
cin >> a[i];
}
sort(a, a + n);
int top = 0, tail = n - 1, ans = 0;
while(top <= tail) {
while(a[top] + a[tail] > w) {
ans++;
tail--;
}
ans++, top++, tail--;
}
cout << ans;
}