Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

注意在判断的过程中越界

Posted by when at 2017-04-11 12:12:45 on Problem 3104
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

const int MAXN=100000;
const int INF=1<<25;
int a[MAXN];
int N,M,K;

/*
C(mid):烘干所有的衣服所用的时间为mid。
false: 代表不能在mid时间烘干所有的衣服
true:代表在mid时间能烘干所有的衣服
*/

/*
设烘干所有的衣服需要用的时间是x,那么自然晾干的时间为mid-x
那么k×x+mid-x>=a[i]
那么x>=(a[i]-mid)/(k-1)
*/

//限制条件是用烘干机的时间t<=mid
bool calc(int mid){
    //注意这儿越界
    long long t=0;     //计算烘干所有的衣服所用烘干机的时间
    for(int i=0;i<N;i++){
        if(a[i]-mid>0){
            t+=(a[i]-mid+K-2)/(K-1);
        }
    }
    return t<=mid;
}
int main(){
    cin>>N;
    int Max=-INF;
    for(int i=0;i<N;i++){
        scanf("%d",&a[i]);
        Max=max(Max,a[i]);
    }
    cin>>K;
    if(K==1){
        cout<<Max<<endl;
        return 0;
    }
    int L=0,R=Max;

    while(R-L>1){
        int mid=(L+R)>>1;
        if(calc(mid)){
            R=mid;
        }else{
            L=mid;
        }
    }
    cout<<R<<endl;
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator