UVALIVE 2949 Elevator Stopping Plan(二分 + 貪心)

看了好久別人的報告纔看明白,,太弱了。。。

#include <map>
#include <set>
#include <list>
#include <cmath>
#include<cctype>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b)
{
	return a % b == 0 ? b : gcd(b, a % b);
}
#define MAXN 35
int N,cas,limit,Max,num;
bool Hash[MAXN];
int stop[MAXN];
//走路上一層或下一層都耗費20s,樓梯上下一層要用4s,每次停下來持續10s,
void read()
{
    int tmp;
    Max=1;
    memset(Hash,false,sizeof(Hash));
    for (int i=0;i<N;i++)
    {
        scanf("%d",&tmp);
        Hash[tmp]=true;
        if (tmp>Max)Max=tmp;
    }
    limit=(Max-1)*14;
}
bool judge(int time)
{
    int i,j;num=0;
    i=time/20+2;
    while (i<=Max)
    {
        while (i<=Max && !Hash[i]) i++;
        if ((i-1)*4+10*num>time) return false;
        j=(time-10*num+20*i+4)/24;
        i=(time-10*num+16*j+4)/20+1;
        stop[num++]=j;
    }
    return true;
}
int binary()
{
    int mid,left=0,right=limit;
    while (left<right)
    {
        mid=(left+right)/2;
        if (judge(mid)) right=mid;
        else left=mid+1;
    }
    //printf("%d %d %d",left,mid,right);
    return right;
}
int main()
{
    while (scanf("%d",&N)!=EOF)
    {
        if (N==0) break;
        read();
        printf("%d\n",binary());
        judge(binary());
        printf("%d",num);
        for (int i=0;i<num;i++)
            printf(" %d",stop[i]);
        putchar('\n');
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章