目录
1.区间贪心
最精髓的地方在cmp函数里面
#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <map>
using namespace std;
struct line{
int x;
int y;
}martix[100];
bool cmp(line a,line b){
if(a.x!=b.x){
return a.x>b.x;
}else{
return a.y<b.y;
}
}
int main(){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d %d",&martix[i].x,&martix[i].y);
}
sort(martix,martix+n,cmp);
int ans=1,pos=martix[0].x;
for(int i=1;i<n;i++){
if(martix[i].y<=pos){
ans+=1;
pos = martix[i].x;
}
}
printf("%d\n",ans);
return 0;
}
2.区间贪心(右端点)
#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <map>
using namespace std;
struct line{
int x;
int y;
}martix[100];
bool cmp(line a,line b){
if(a.y!=b.y){
return a.y<b.y;
}else{
return a.x>b.x;
}
}
int main(){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d %d",&martix[i].x,&martix[i].y);
}
sort(martix,martix+n,cmp);
int ans=1,pos=martix[0].y;
for(int i=1;i<n;i++){
if(martix[i].x>=pos){
ans+=1;
pos = martix[i].y;
}
}
printf("%d\n",ans);
return 0;
}
3.不相交区间的点
#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <map>
using namespace std;
struct line{
int x;
int y;
}martix[100];
bool cmp(line a,line b){
if(a.y!=b.y){
return a.y<b.y;
}else{
return a.x>b.x;
}
}
int main(){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d %d",&martix[i].x,&martix[i].y);
}
sort(martix,martix+n,cmp);
int ans=1,pos=martix[0].y;
for(int i=1;i<n;i++){
if(martix[i].x>pos){
ans+=1;
pos = martix[i].y;
}
}
printf("%d\n",ans);
return 0;
}
4.TO FILL OR NOT TO FILL
这是我一开始的想法,算是伪代码吧,虽然思想是对的,但是实现起来有很多错误
#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <map>
using namespace std;
struct car{
double price;
int distance;
}cars[100];
bool cmp(car a,car b){
return a.distance<b.distance;
}
int maxrun;
int getfar(int distance){
int i=0;
int flag=-1;
while(1){
if(cars[i].distance-distance<=maxrun){
i++;
flag=1;
}else{
if(flag==1){
return i-1;
}else{
return flag;
}
}
}
}
int get_minprice_position(int now,int far){
int min_p= now+1;
for(int i=now+1;i<=far;i++){
if(cars[i].price<cars[now].price){
min_p = i;
break;
}
if(cars[min_p].price>cars[i].price){
min_p = i;
}
}
return min_p;
}
int main(){
int cap,dis,davg,n;
scanf("%d %d %d %d",&cap,&dis,&davg,&n);
maxrun = cap*davg;
int pos;
for(pos=0;pos<n;pos++){
scanf("%lf %d",&cars[pos].price,&cars[pos].distance);
}
cars[pos].distance=dis;
cars[pos].price=0;
sort(cars,cars+n,cmp);
if(cars[0].distance!=0){//出发点没油
printf("The maximun travel distance = 0.00");
return 0;
}
int now=0;
int far;
int distance=0;
int min_price;
int price=0;
int tank=0;
int need=0;
while(1){
far = getfar(now);
if(far==-1){
printf("The maximun travel distance = %d",distance+maxrun);//加满了油也到不了任何站
return 0;
}else{
min_price = get_minprice_position(now,far);
if(min_price<cars[now].price){
need =
}
distance+=cars[min_price].distance-cars[now].distance;
now = min_price;
if(cars[pos].distance-cars[now].distance<=maxrun){
printf("The maximun travel distance = %d",distance+cars[pos].distance-cars[now].distance);
return 0;
}
}
}
return 0;
}
这是晴神的代码,整体思路更加清晰,而且实现起来也很清楚
这里面有个很绝的点就是把终点设置成油费为0,这样可以在比较油费的时候一起判断是否到达终点了。
整体代码清晰明了,真的厉害
#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <map>
using namespace std;
const int maxn = 510;
const int INF = 100000000;
struct station{
double price,dis;
}st[maxn];
bool cmp(station a,station b){
return a.dis<b.dis;
}
int main(){
int n;
double cmax,d,davg;
scanf("%lf %lf %lf %lf",&cmax,&d,&davg,&n);
for(int i=0;i<n;i++){
scanf("%lf%lf",&st[i].price,&st[i].dis);
}
st[n].price=0;
st[n].dis= d;
sort(st,st+n,cmp);
if(st[0].dis!=0){
printf("the maximum travel distance = 0.00\n");
}else{
int now=0;
double ans=0,nowtank=0,max=cmax*davg;
while(now<n){
int k=-1;
double pricemin = INF;
for(int i=now+1;i<=n&&st[i].dis-st[now].dis<=max;i++){
if(st[i].price<pricemin){
pricemin = st[i].price;
k=i;
if(st[i].price<st[now].price){
break;
}
}
}
if(k==-1) break;
double need = (st[k].dis-st[now].dis)/davg;
if(pricemin<st[now].price){
if(nowtank<need){
ans+=(need-nowtank)*st[now].price;
nowtank=0;
}else{
nowtank-=need;
}
}else{
ans += (cmax-nowtank)*st[now].price;
nowtank=cmax-need;
}
now = k;
}
if(now == n){
printf("%.2f\n",ans);
}else{
printf("the maximum travel distance = %.2f\n",st[now].dis+max);
}
}
return 0;
}