目录
一、经典贪心问题介绍
二、可图性判定问题
三、sort函数的使用方法、
一、经典贪心问题介绍
总是作出在当前看来是最好的选择!!!
不从整体上加以考虑,所作出的仅仅是在某种意义上的局部最优解。
例一:杭电oj 1052 田忌赛马(链接Problem - 1052 (hdu.edu.cn))
#include <cstdio>
#include <algorithm>
using namespace std;
int tian[1010],qi[1010];
bool cmp(int a,int b){
return a>b;
}
int main(){
int n;
while(scanf("%d",&n)&&n){
for(int i=0;i<n;i++) scanf("%d",&tian[i]);
for(int i=0;i<n;i++) scanf("%d",&qi[i]);
sort(tian,tian+n,cmp);//从大到小进行排序
sort(qi,qi+n,cmp);//从大到小进行排序
int tfast=0,qfast=0;
int tlow=n-1,qlow=n-1;
int cnt=0;//计数,输赢的场次
for (int i = 0; i<n;i++){
if(tian[tfast]>qi[qfast]){
tfast++;
qfast++;
cnt++;
}
else if(tian[tlow]>qi[qlow]){
tlow--;
qlow--;
cnt++;
}
else if(tian[tlow]<qi[qfast]){
tlow--;
qfast++;
cnt--;
}
}printf("%d\n",cnt*200);
}
return 0;
}
经典例子:事件序列问题

例题:杭电oj2037 今年暑假不AC(链接:Problem - 2037 (hdu.edu.cn))
#include<iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
const int N=110;
struct node
{ int start;
int end;
}p[N] ;
bool cmp(node a,node b){
return a.end<b.end;
}
int main()
{
int n;
while (cin >> n && n) {
for(int i=0;i<n;i++) cin>>p[i].start>>p[i].end;
sort(p,p+n,cmp);
int t=p[0].end;
int cnt=1;
for(int i=1;i<n;i++){
if(p[i].start>=t) {
t=p[i].end;
cnt++;
}
}
cout<<cnt<<endl;
}
return 0;
}
例二、Moving Tables (链接:Problem - 1050 (hdu.edu.cn))
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
bool cmp(int a,int b){
return a>b;
}
int main(){
int n;
scanf("%d",&n);
while(n--) {
int cun[210]={0};
int m;
scanf("%d",&m);
for(int i=0;i<m;i++){
int x,y;
scanf("%d %d",&x,&y);
int tx=min(x,y);
int ty=max(x,y);
x=(tx-1)/2;
y=(ty-1)/2;
for(int j=x;j<=y;j++) {
cun[j]++;
}
}
sort(cun,cun+205,cmp);
cout<<cun[0]*10<<endl;
}
return 0;
}
练习:HDOJ1009 FatMouse‘ Trade_m0_55756918的博客-CSDN博客
反证法证明贪心思路的正确性
特别说明:
若要用贪心算法求解某问题的最优解,必须首先证明贪心思想在该问题的应用结果就是最优解!!
二、可图性判定问题
可图性判定!
两个概念:
1.度序列:若把图G所有顶点的度数排成一个序列S,则称S为图的度序列。(有几条边就有几个度)
2.序列是可图的:一个非负整数组成的有限序列,如果是某个无向图的度序列,,则称该序列是可图的。
由该定理得到是否可图。

例题:青蛙的邻居(链接:1659 -- Frogs' Neighborhood (poj.org))
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
struct frog
{
int degree;
int i;
}f[20];
bool cEdge(frog a,frog b){
return a.degree>b.degree;
}
int main(){
int T,flag,d1,n,i,j;
int Edge[15][15];
cin>>T;
while(T--)
{
cin>>n;
for(i=0;i<n;i++)
{
cin>>f[i].degree;
f[i].i=i;
}
flag=1;
memset(Edge,0,sizeof(Edge));
for(j=0;j<n;j++){
sort(f,f+n,cEdge);
d1=f[j].degree;
if(d1>n-j-1){
flag=0;
break;
}
for(i=1;i<=d1;i++){
f[i+j].degree--;
if(f[i+j].degree<0){
flag=0;
break;
}
Edge[f[j].i][f[i+j].i]=Edge[f[i+j].i][f[j].i]=1;
}
if(!flag) break;
}
if(flag)
{
printf("YES\n");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(j) printf(" %d",Edge[i][j]);
else printf("%d",Edge[i][j]);
}
printf("\n");
}
}
else printf("NO\n");
if(T) printf("\n");
}
return 0;
}
三、sort函数的使用方法
头文件:#include<algorithm>
sort(首地址,尾地址+1,【cmp函数】)
这个函数可以传两个或三个参数;
这第一个参数是要排序的区间的首地址
第二个参数是区间为地址的下一地址;
第三个参数不屑,则缺略为递增排序,及从小到大排序。
1)sort对int类型数组排序
int num[100];
bool cmp(int a,int b)
{ return a>b;}
sort(num,num+100,cmp);
以上的代码实现了把数组num从大到小的排序
说明:定义了小于运算的类型都类似,甚至包括字符串类string(字符数组不支持小于运算符,需要strcmp函数)
2)sort对结构体类型数组排序
struct node
{int a;double b;}
arr[100];
先按a值升序排列,如果a值相同,再按b值降序排列。
bool cmp(node x,node y)
{
if(x.a!=y.a) return x.a<y.a;
return x.b>y.b;
}
sort(arr.arr+100,cmp);
3)sort排序的一个综合示范
struct student
{char name[11];int age;double score;}
stu[100];
bool cmp(student x,student y)
{
if(fabs(x.score-y.score)>0.00001)
return x.score>y.score;
if(x.age !=y.age) return x.age<y.age;
return strcmp(x.name,y.name)<0;
}
4)经验之谈
sort函数的灵活运用:可以对数组的某一段进行排序
常见的错误:忘记修改sort的第一个参数!
612




被折叠的 条评论
为什么被折叠?



