第一题:UVa11292
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int MAX = 20005;
int n, m;
int d[MAX], w[MAX];
int cost = 0;
int main()
{
freopen("in.txt", "r", stdin);
while(cin >> n >> m)
{
if (!m && !n) break;
if (n > m) {
cout << "Loowater is doomed!" << endl;
break;
}
for(int i = 0; i < n; ++ i) cin >> d[i];
for(int i = 0; i < m; ++ i) cin >> w[i];
sort(d, d+n);
sort(w, w+m);
cost = 0;
int j = 0;
for(int i = 0; i < m; ++ i)
{
//cout << "cost: " << cost << endl;
if(w[i] >= d[j])
{
++ j;
cost += w[i];
if (j >= n)
break;
}
}
if(j >= n)
cout << cost << endl;
else
cout << "Loowater is doomed!" << endl;
}
fclose(stdin);
return 0;
}
第二题:UVa11729
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int MAX = 1005;
struct task
{
int b;
int j;
};
task t[MAX];
// int cmp function, ">" means descending order
int cmp(const task &t1, const task &t2) { return t1.j > t2.j;}
// return max
int max(const int &a, const int &b) { return (a > b) ? a : b; }
int main()
{
//freopen("in.txt", "r", stdin);
int n;
int kase = 1; // kase ? case !
int ans = 0, s = 0;
while(cin >> n && n)
{
for(int i = 0; i < n; ++ i) cin >> t[i].b >> t[i].j;
// sort function with ascending order by default
sort(t, t+n, cmp);
ans = 0; s = 0; // important!
for(int i = 0; i < n; ++ i)
{
s += t[i].b;
ans = max(s + t[i].j, ans);
}
cout << "Case " << kase ++ << ": " << ans << endl;
}
return 0;
}
第三题:UVa11300
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAX = 1000005;
int main()
{
//freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false);
int n;
long long c[MAX];
long long sum, a, avg, ans;
while(cin >> n && n)
{
cin >> a; sum = a; c[0] = 0;
for(int i = 1; i < n; ++ i)
{
c[i] = c[i-1] + a;
cin >> a;
sum += a;
}
avg = sum / n;
for(int i = 1; i < n; ++ i)
c[i] = c[i] - avg * i;
sort(c, c+n);
long long x1 = c[n/2]; // 取中位数
ans = 0;
for(int i = 0; i < n; ++ i)
{
ans += abs(x1 - c[i]);
}
cout << ans << endl;
}
//fclose(stdin);
return 0;
}