当前位置:编程学习 > C/C++ >>

关于二分法中取中间值时向下和向上取整的问题(由大白LA3971想到的)

[cpp]
/*************************************************************************
    > File Name: 12124.cpp
    > Author: BobLee
    > Mail: wustboli@gmail.com 
    > Created Time: Mon 25 Mar 2013 08:36:44 PM CST
 ************************************************************************/ 
 
#include<iostream>  
#include<cstdio>  
#include<cstring>  
#include<cmath>  
#include<algorithm>  
#include<vector>  
#include<map>  
#include<string>  
using namespace std; 
 
const int maxn = 1010; 
 
struct co 

    int price; 
    int qua; 
}; 
 
map<string,int> id; 
vector<co> com[maxn]; 
int N,B; 
int cnt; 
 
int ID(string s) 

    if(!id.count(s)) 
        id[s] = cnt++; 
    return id[s]; 

 
bool fun(int q) 

    int sum = 0; 
    for(int i=0;i<cnt;i++) 
    { 
        int cheap = B+1; 
        int m = com[i].size(); 
        for(int j=0;j<m;j++) 
        { 
            if(com[i][j].qua>=q) 
                cheap = min(cheap,com[i][j].price); 
        } 
        if(cheap > B) 
            return false; 
        sum+=cheap; 
        if(sum>B) 
            return false; 
    } 
    return true; 

 
int main() 

#ifndef ONLINE_JUDGE  
    freopen("in.txt","r",stdin); 
#endif  
    int t; 
    scanf("%d",&t); 
    while(t--) 
    { 
        scanf("%d%d",&N,&B); 
        char type[30],name[30]; 
        int price,quaa; 
        int maxq = 0; 
        cnt=0; 
        for(int i=0;i<N;i++) 
            com[i].clear(); 
        for(int i=0;i<N;i++) 
        { 
            scanf("%s%s%d%d",type,name,&price,&quaa); 
            maxq = max(maxq,quaa); 
            com[ID(type)].push_back((co){price,quaa}); 
        } 
        int L=0; 
        int R=maxq; 
        while(L<R) 
        { 
            int M=(L+R+1)/2; 
            //cout<<L<<"  "<<R<<" "<<M<<endl;  
            if(fun(M)) 
            { 
                L=M; 
            } 
            else 
                R=M-1; 
            //cout<<L<<"  "<<R<<" "<<M<<endl;  
            //getchar();  
        //  if(M==0)  
        //      break;  
        } 
        printf("%d\n",L); 
    } 
    return 0; 
 

/*************************************************************************
    > File Name: 12124.cpp
    > Author: BobLee
    > Mail: wustboli@gmail.com
    > Created Time: Mon 25 Mar 2013 08:36:44 PM CST
 ************************************************************************/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<string>
using namespace std;

const int maxn = 1010;

struct co
{
 int price;
 int qua;
};

map<string,int> id;
vector<co> com[maxn];
int N,B;
int cnt;

int ID(string s)
{
 if(!id.count(s))
  id[s] = cnt++;
 return id[s];
}

bool fun(int q)
{
 int sum = 0;
 for(int i=0;i<cnt;i++)
 {
  int cheap = B+1;
  int m = com[i].size();
  for(int j=0;j<m;j++)
  {
   if(com[i][j].qua>=q)
    cheap = min(cheap,com[i][j].price);
  }
  if(cheap > B)
   return false;
  sum+=cheap;
  if(sum>B)
   return false;
 }
 return true;
}

int main()
{
#ifndef ONLINE_JUDGE
 freopen("in.txt","r",stdin);
#endif
 int t;
 scanf("%d",&t);
 while(t--)
 {
  scanf("%d%d",&N,&B);
  char type[30],name[30];
  int price,quaa;
  int maxq = 0;
  cnt=0;
  for(int i=0;i<N;i++)
   com[i].clear();
  for(int i=0;i<N;i++)补充:软件开发 , C++ ,

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,