贪心算法总结
几道题目
贪心算法一般前提操作:结构体,排序
FatMouse’ Trade
胖老鼠的交易(HDU1009)
根据每个房间能获得的JavaBeans和需要做为交换的猫食的比例,将数组进行排序。此时,从前到后每个房间获得的JavaBeans一定是最合适的。
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1005;
struct node {
int f;
int j;
double v;
}a[maxn];
bool cmp(const node &a1,const node &a2) {
return a1.v>a2.v;
}
int main() {
int m,n;
while(cin >> m >> n) {
if(m==-1 && n==-1) break;
double ans=0;
for(int i=1; i<=n; i++) {
cin >> a[i].j >> a[i].f;
a[i].v=1.0*a[i].j/a[i].f;
}
sort(a+1,a+n+1,cmp);
for(int i=1; i<=n; i++) {
if(a[i].f<=m) {
ans+=a[i].j;
m-=a[i].f;
}
else {
double s=1.0*a[i].j/a[i].f;
ans+=s*m;
m=0;
}
if(m==0) break;
}
printf("%.3f\n", ans);
}
}
Moving Tables
Moving Tables(HDU1050)
结构体数组a用来存储所有要搬动的操作。按照搬动的桌子的起始位置排序。开一个数组s用来记录总共要移动几轮,s[i]表示本轮搬动最后的位置。遍历数组a,如果当前搬动操作与s数组中已有的每轮操作都有冲突,s就要增加新的一轮。
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 205;
struct node {
int s;
int e;
}a[maxn];
int s[maxn];
bool cmp(const node &a1, const node &a2) {
return a1.s<a2.s;
}
int main() {
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
for(int i=1; i<=n; i++) {
cin >> a[i].s >> a[i].e;
}
for(int i=1; i<=n; i++) {
if(a[i].s>a[i].e) {
swap(a[i].s,a[i].e);
}
}
sort(a+1,a+n+1,cmp);
int cnt=1;
for(int i=1; i<=n; i++) {
if(i==1) {
s[i]=a[i].e;
continue;
}
int flag=0;
for(int j=1; j<=cnt; j++) {
if(s[j]%2==0 && a[i].s>s[j]) {
s[j]=a[i].e;
flag=1;
break;
}
else if(s[j]%2!=0 && a[i].s>s[j]+1) {
s[j]=a[i].e;
flag=1;
break;
}
}
if(flag==0) {
cnt++;
s[cnt]=a[i].e;
}
}
cout << cnt*10 << endl;
}
}
今年暑假不AC
今年暑假不AC(HDU2037)
按照节目结束时间早的排序。结束时间越早,看的节目越可能多。
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 105;
struct node {
int s;
int e;
}a[maxn];
bool cmp(const node &a1,const node &a2) {
return a1.e<a2.e;
}
int main() {
int n;
while(cin >> n) {
if(n==0) break;
int ans=1;
for(int i=1; i<=n; i++) {
int s,e;
cin >> a[i].s >> a[i].e;
}
sort(a+1,a+n+1,cmp);
int flag=1;
for(int i=2; i<=n; i++) {
if(a[i].s>=a[flag].e) {
flag=i;
ans++;
}
}
cout << ans <<endl;
}
}
Doing Homework again
Doing Homework again(HDU1789)
按照截至日期早的排序。遍历数组,如果当前天数小于等于截至日期,照常把当前的作业做完,否则就去已经做完作业的前几天去找比做不完作业的这天扣的分最少的那个作业,把那个作业扔掉,换成做这个。贪心!优先做扣分多的作业,反正要有一个作业做不完。
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1005;
struct node {
int dead;
int rdu;
bool v;
}a[maxn];
bool cmp(const node &a1, const node &a2) {
return a1.dead<a2.dead;
}
int main() {
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
for(int i=1; i<=n; i++) {
cin >> a[i].dead;
}
for(int i=1; i<=n; i++) {
cin >> a[i].rdu;
a[i].v=false;
}
sort(a+1,a+n+1,cmp);
for(int i=1,day=1; i<=n; i++) {
if(day<=a[i].dead) {
a[i].v=true;
day++;
}
else {
int min=-1;
//先将第一个设为最小
for(int j=1; j<i; j++) {
if(a[j].v==true) {
min=j;
break;
}
}
if(min==-1) continue;
//找最小值
for(int j=1; j<i; j++) {
if(a[j].rdu<a[min].rdu && a[j].v==true) {
min=j;
}
}
//当前比最小大,做当前,不做最小
if(a[min].rdu<a[i].rdu) {
a[min].v=false;
a[i].v=true;
}
}
}
int ans=0;
for(int i=1; i<=n; i++) {
if(a[i].v==false) {
ans+=a[i].rdu;
}
}
cout << ans << endl;
}
}
Tian Ji – The Horse Racing
Tian Ji – The Horse Racing(HDU1052)
将田忌的马跟齐王的马从大到小排序。分多钟情况。
1.田忌最快的马比齐王最快的马快,就直接比,赢。
2.田忌最快的马比齐王最快的马慢,反正都要输,就拿田忌最慢的马跟齐王最快的马比。
3.田忌最快的马和齐王最快的马一样快
(1)田忌最慢的马比齐王最慢的马快,直接比,赢。
(2)田忌最慢的马比齐王最慢的马慢,反正都要输,就拿田忌最慢的马跟齐王最快的马比。
(3)田忌齐王最慢的马也一样快,那就和情况(2)一样,齐王最快的和田忌最慢的比,最差也是后面能打平。
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1005;
int a[maxn];
int b[maxn];
bool cmp(const int &a, const int &b) {
return a>b;
}
int main() {
int n;
while (cin >> n && n) {
for (int i=1; i<=n; i++)
cin >> a[i];
for (int i=1; i<=n; i++)
cin >> b[i];
sort(a+1, a+n+1, cmp);
sort(b+1, b+n+1, cmp);
int sa=1,ea=n;
int sb=1,eb=n;
int ans=0;
while(sa<=ea && sb<=eb) {
if(a[sa]>b[sb]) {
ans++;
sa++;
sb++;
}
else if(a[sa]<b[sb]) {
ans--;
ea--;
sb++;
}
else {
if(a[ea]>b[eb]) {
ans++;
ea--;
eb--;
}
else if(a[ea]<b[eb]) {
ans--;
ea--;
sb++;
}
else {
if(a[ea]<b[sb]) {
ans--;
}
ea--;
sb++;
}
}
}
cout << ans*200 <<endl;
}
}
Degree Sequence of Graph G
Degree Sequence of Graph G(HDU2454)
判断一个序列是否可图
Havel-Hakimi定理:把序列排成不增序,即d1 >= d2 >= …… >= dn,则d可简单图化当且仅当d’ = { d2 - 1,d3 - 1,……d(d1 + 1) - 1, d(d1 + 2),d(d1 + 3),……dn }可简单图化。简单的说,把d排序后,找出度最大的点(设度为d1),把它与度次大的d1个点之间连边,然后这个点就可以不管了,一直继续这个过程,直到建出完整的图,或出现负度等明显不合理的情况。
下标 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
---|---|---|---|---|---|---|---|
起始 | 5 | 4 | 4 | 3 | 2 | 1 | 1 |
第一轮 | 0 | 3 | 3 | 2 | 1 | 0 | 1 |
第二轮 | 0 | 0 | 2 | 1 | 0 | 0 | 1 |
第三轮 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1005;
int a[maxn];
bool cmp(const int &a1, const int &a2) {
return a1>a2;
}
int main() {
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
for(int i=1; i<=n ;i++) {
cin >> a[i];
}
if(n==1) {
if(a[1]==0) cout <<"yes"<<endl;
else cout <<"no"<<endl;
continue;
}
sort(a+1, a+n+1, cmp);
int flag=0;
while(a[1]>0) {
int cnt=0;
for(int i=2; i<=n; i++) {
if(a[i]-1>=0) {
a[i]=a[i]-1;
cnt++;
}
else {
flag=1;
break;
}
if(cnt==a[1]) {
break;
}
}
a[1]=0;
if(flag==1) break;
sort(a+1,a+n+1, cmp);
}
if(flag==0) cout <<"yes"<< endl;
else cout <<"no"<< endl;
}
}