只所以把两个题目放在一块,是觉得这两个题目有异曲同工之妙吧。算是排序和贪心吧。
题目链接:http://cstest.scu.edu.cn/soj/problem.action?id=4263
http://cstest.scu.edu.cn/soj/problem.action?id=2750
4263:
#include <stdio.h>
#include <algorithm>
using namespace std;
const int MAX = 1000+10;
struct node
{
int a;
int b;
}cloth[MAX];
bool operator<(const node&p1, const node&p2)
{
return p1.b-p1.a>p2.b-p2.a;
}
int main()
{
int v, m;
while (scanf("%d%d", &v, &m) == 2)
{
int i;
for (i=1; i<=m; i++)
{
scanf("%d%d", &cloth[i].a, &cloth[i].b);
}
sort(cloth+1, cloth+m+1);
for (i=1; i<=m; i++)
{
if (v<=0||v<cloth[i].b)
{
puts("NO");
break;
}
else
{
v -= cloth[i].a;
}
}
if (i==m+1)
{
puts("YES");
}
}
}
2750:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAX = 10005;
struct node
{
string str;
}no[MAX];
int n;
bool operator<(const node& a, const node& b)
{
return a.str + b.str < b.str + a.str;
}
int main()
{
while (cin >> n)
{
for (int i=0; i<n; i++)
{
cin >> no[i].str;
}
sort(no, no+n);
for (int i=0; i<n; i++)
{
cout << no[i].str;
}
cout << endl;
}
}