2013-7-18

Many graduate students of Peking University are living in Wanliu Campus, which is 4.5 kilometers from the main campus - Yanyuan. Students in Wanliu have to either take a bus or ride a bike to go to school. Due to the bad traffic in Beijing, many students choose to ride a bike. 
We may assume that all the students except "Charley" ride from Wanliu to Yanyuan at a fixed speed. Charley is a student with a different riding habit - he always tries to follow another rider to avoid riding alone. When Charley gets to the gate of Wanliu, he will look for someone who is setting off to Yanyuan. If he finds someone, he will follow that rider, or if not, he will wait for someone to follow. On the way from Wanliu to Yanyuan, at any time if a faster student surpassed Charley, he will leave the rider he is following and speed up to follow the faster one. 
We assume the time that Charley gets to the gate of Wanliu is zero. Given the set off time and speed of the other students, your task is to give the time when Charley arrives at Yanyuan. 
 

Input

There are several test cases. The first line of each case is N (1 <= N <= 10000) representing the number of riders (excluding Charley). N = 0 ends the input. The following N lines are information of N different riders, in such format:
Vi [TAB] Ti 
Vi is a positive integer <= 40, indicating the speed of the i-th rider (kph, kilometers per hour). Ti is the set off time of the i-th rider, which is an integer and counted in seconds. In any case it is assured that there always exists a nonnegative Ti. 
 

Output

Output one line for each case: the arrival time of Charley. Round up (ceiling) the value when dealing with a fraction.
 

#include <iostream>

#include <cmath>

using namespace std;

int main()

{

 int n;  

while(cin>>n &&n != 0)

 {     

  int *a =  new int[n];

    int *b =  new int[n];

    int minTime = 9999999;

    for(int i = 0 ; i < n;i++)

    {      

    cin>>a[i]>>b[i];  

     }

    for(int i = 0;i<n;i++)  

   {    

      if((ceil(16200.0/a[i]) + b[i] < minTime)&& b[i]>=0)     

    {      

       minTime = ceil(16200.0/a[i]) + b[i];   

       }

    }

     cout<<minTime<<endl;

   }    

     return 0;

}

 
 

Description

As you know, Gardon trid hard for his love-letter, and now he's spending too much time on choosing flowers for Angel. When Gardon entered the flower shop, he was frightened and dazed by thousands kinds of flowers.  "How can I choose!" Gardon shouted out. Finally, Gardon-- a no-EQ man decided to buy flowers as many as possible. Can you compute how many flowers Gardon can buy at most?
 

Input

Input have serveral test cases. Each case has two lines. The first line contains two integers: N and M. M means how much money Gardon have. N integers following, means the prices of differnt flowers.
 

Output

For each case, print how many flowers Gardon can buy at most. You may firmly assume the number of each kind of flower is enough.
 
 
#include <iostream>

using namespace std;
void quickSort(int *a,size_tleft,size_tright)
{
    size_tp = (left + right)/2;
    int key = a[p];
    for(size_ti = left,j = right; i < j;)
    {
        while(!(key < a[i] || p < i))
            i++;
        if(i < p)
        {
            a[p] = a[i];
            p = i;
        }
        while(j>0 && !(j < p || a[j] < key))
            j--;
         if(p < j)
         {
             a[p] = a[j];
             p = j;
         }
     }
     a[p] = key;
     if(p - left > 1)
         quickSort(a,left,p-1);
     if(right - p > 1)
         quickSort(a,p + 1, right);
}
int main()
{
         int n,m;
         while(cin>>n>>m)
         {
            int *flower = new int [n];
            for(int i = 0 ; i < n; i++)
            cin>>flower[i];
            quickSort(flower,0,n);
         cout<<m/flower[0]<<endl;
         }
    return 0;
}
 
 
 
The knights explained: "To slay the dragon, we must chop off all its heads. Each knight can chop off one of the dragon's heads. The heads of the dragon are of different sizes. In order to chop off a head, a knight must be at least as tall as the diameter of the head. The knights' union demands that for chopping off a head, a knight must be paid a wage equal to one gold coin for each centimetre of the knight's height." 
Would there be enough knights to defeat the dragon? The king called on his advisors to help him decide how many and which knights to hire. After having lost a lot of money building Mir Park, the king wanted to minimize the expense of slaying the dragon. As one of the advisors, your job was to help the king. You took it very seriously: if you failed, you and the whole kingdom would be burnt to a crisp! 
 

Input

The input contains several test cases. The first line of each test case contains two integers between 1 and 20000 inclusive, indicating the number n of heads that the dragon has, and the number m of knights in the kingdom. The next n lines each contain an integer, and give the diameters of the dragon's heads, in centimetres. The following m lines each contain an integer, and specify the heights of the knights of Loowater, also in centimetres.  The last test case is followed by a line containing:  0 0
 

Output

For each test case, output a line containing the minimum number of gold coins that the king needs to pay to slay the dragon. If it is not possible for the knights of Loowater to slay the dragon, output the line:  Loowater is doomed!
//循环跳出后的输出要注意!!!!
#include <iostream>
using namespace std;
void quickSort(int *a,size_tleft,size_tright)
{
    size_tp = (left + right)/2;
    int key = a[p];
    for(size_ti = left,j = right; i < j;)
    {
        while(!(key < a[i] || p < i))
            i++;
        if(i < p)
        {
            a[p] = a[i];
            p = i;
        }
        while(j>0 && !(j < p || a[j] < key))
            j--;
         if(p < j)
         {
             a[p] = a[j];
             p = j;
         }
     }
     a[p] = key;
     if(p - left > 1)
         quickSort(a,left,p-1);
     if(right - p > 1)
         quickSort(a,p + 1, right);
}
int main()
{
    int m,n;
    while(cin>>n>>m && m && n)
    {
        int *a = new int[n];
        int *b = new int[m];
       for(int i = 0; i< n;i++)
       {
           cin>>a[i];
       }
       for(int i = 0 ; i < m;i++)
       {
           cin>>b[i];
       }
       quickSort(a,0,n);
       quickSort(b,0,m);
       int money = 0;
       if(m<n || a[0] >b[m-1])
       {
           cout<<"Loowater is doomed!"<<endl;
       }
       else
       {
          int z=1;
           for(int i = 0, j = 0; i < m,j < n; )
           {
               if(m-i < n-j)
               {
                   z=0;
                   cout<<"Loowater is doomed!"<<endl;
                   break;
               }

               if(b[i]>= a[j])
               {
                   money += b[i];
                   j++;
                   i++;
               }
               else{i++;}

           }
           if (z)
           cout<<money<<endl;
       }
    }
   return 0;
}
 
 
 
 
 
 
 
 
原文地址:https://www.cnblogs.com/T8023Y/p/3197402.html