PAT 1070
Basic Greed
mooncake ( time limit) ms
Memory Limit 32000 KB
Code length limit 16000 B
Standard author CHEN, Yue
Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types of fillings and crusts can be found in traditional mooncakes according to the region ' s culture. Now given the inventory amounts and the prices of all kinds of the mooncakes, together with the maximum total demand of th E Market, you are supposed to tell the maximum profit the can is made.
Note:partial Inventory storage can be taken. The sample shows the following situation:given three kinds of mooncakes with inventory amounts being 180 Ousand tons, and the prices being 7.5, 7.2, and 4.5 billion. If the market demand can be at most thousand tons, the best we can are to sell the thousand tons of the second kind Mooncake, and thousand tons of the third kind. Hence The total profit is 7.2 + 4.5/2 = 9.45 (Billion Yuans).
Input Specification:
Each input file contains one test case. For the contains 2 positive integers N (<=1000), the number of different kinds of mooncakes, and D (<=500 thousand tons), the maximum total demand of the market. Then the second line gives the positive inventory amounts (in thousand tons), and the third line gives the positive (in billion yuans) of N kinds of mooncakes. All of the numbers in a line are separated by a.
Output Specification:
For each test case, print the maximum profit (in billion Yuans) at one line, accurate up to 2 decimal places. Sample Input:
3
180
7.5 7.2 4.5
Sample Output:
9.45
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include "stdlib.h"
#include <string.h>
#include <functional>
#include <memory.h>
using namespace std;
#define N 1010
#define MIN (a,b) a>b?b:a
struct P
{
double cap;
Double Price;
Double Perprice;
BOOL operator < (const P & A) const
{return
perprice>a.perprice;
}
} Moon[n];
int main ()
{
int n,cap,i;
while (Cin>>n>>cap)
{for
(i=0;i<n;i++)
cin>>moon[i].cap;
for (i=0;i<n;i++)
cin>>moon[i].price;
for (i=0;i<n;i++)
moon[i].perprice = Moon[i].price/moon[i].cap;
Sort (Moon, moon+n);
Double sum = 0;
int i = 0;
while (cap>0&&i<=n)
{
int v = min (cap,moon[i].cap);
sum + + V*moon[i].perprice;
Cap-= v;
i++;
}
printf ("%.2f\n", sum);
}
return 0;
}
PAT 1071
String processing +map
Speech Patterns ( time limit) ms
Memory Limit 32000 KB
Code length limit 16000 B
Standard author HOU, qiming
People often have a preference among synonyms of the same word. For example, some could prefer "the police" while others may prefer "the cops". Analyzing such patterns can help to narrow down a speaker ' s identity, which was useful when validating, for example, whethe R it ' s still the same person behind a online avatar.
Now given a paragraph of text sampled from someone's speech, can you find the person ' s most commonly used word?
Input Specification:
Each input file contains one test case. For each case, the there is one line of the text no more than the 1048576 characters in length and terminated by a carriage return ' \ n '. The input contains at least one alphanumerical character, i.e., one character from the set [0-9 A-z].
Output Specification:
For each test case, print in one line the most commonly occurring word in the input text, followed by A and the numb Er of times it has occurred in the input. If There are more than one such words, print the lexicographically smallest one. The word should is printed in all lower case. Here a "word" was defined as a continuous sequence of alphanumerical characters separated by non-alphanumerical or the line beginning/end.
Note This words are case insensitive. Sample Input:
Can1: Can A can can a can? It can! "
Sample Output:
Can 5
#include "iostream" #include "string" #include "map" #include "algorithm" using namespace std; #define N 1000 bool Isalp (char a) {if (a>= ' a ' &&a<= ' z ') | | a>= ' A ' &&a<= ' Z ' | |
a>= ' 0 ' &&a<= ' 9 ') return true;
return false;
int main () {string Str,word; Map<string,int> Count;//notice It ' s better not substitute ' string ' to ' char* ', for char* stores the address while St
Ring Stores object Map<string,int>::iterator it,tmp;
while (Getline (CIN,STR)) {transform (Str.begin (), Str.end (), Str.begin (),:: ToLower);
cout<<str<<endl;
int i=0,j;
while (I<str.length ()) {j = i;
while (!isalp (Str[j]) &&j<str.length ()) J++;//skip non-alphanumerical character i = j; while (Isalp (Str[j]) &&i<str.length ()) j + +; I is the "Start" and "J" is the "end" if (i!=j) {string word=str.substr (i,j-i);//notice the usage of substr:s UBSTR (Start,length) if (Count.find (word) ==count.end ()) Count[word] = 1;
else count[word]++;
I=j;
AN int minn =-1;
for (it = Count.begin (); It!=count.end (); it++) if (it->second>minn) {minn = it->second;
TMP = it;
} cout<<tmp->first<< "" <<tmp->second<<endl;
return 0; }
PAT 1072
Shortest path +sort (using some STL functions)
Gas Station ( time limit) ms
Memory Limit 32000 KB
Code length limit 16000 B
Standard author CHEN, Yue
A gas station has to being built at such a location this minimum distance between the station and any of the residential Housing is as far away as possible. However it must guarantee the houses are in its service range.
Now given the map of the "city" and several candidate locations for the "gas station," are supposed to give the best recom Mendation. If There are more than one solution, output the "one with" smallest average to all distance. If Such a solution is still isn't unique, output the one with the smallest index number.
Input Specification:
Each input file contains one test case. For each case, the the 4 positive integers:n (<=) of the ' contains ', the total number of houses; M (<=), the total number of the candidate locations for the gas stations; K (<=), the number of roads connecting the houses and the gas stations; And DS, the maximum service range of the gas station. It is hence assumed which all of the houses are numbered from 1 to N, and all the candidate locations-are numbered from G1 to GM.
Then K lines Follow, each describes a road in the format
P1 P2 Dist
Where P1 and P2 are the two ends of a road which can be either house numbers or gas station numbers, and Dist are the Integ Er length of the road.
Output Specification:
For the all test case, print in the index number of the best location. In the "Next line", print the minimum and the average distances between the solution and all houses. The numbers in a line must is separated by a space and is accurate up to 1 decimal place. If The solution does not exist, simply output "No solution". Sample Input 1:
4 3 5 1 2 2 1 4 2 1
G1 4 1 G2 3 2 3 2 2
G2 1 3 4 2 3
G3 2 4
G1 3 G2
G1 1
G3 G2 2
Sample Output 1:
G1
2.0 3.3
Sample Input 2:
2 1 2
1 G1 9
2 G1 20
Sample Output 2:
No Solution
#include "iostream" #include "string" #include "algorithm" #include "memory.h" #include "numeric" #include "stdlib.h"
using namespace Std;
#define N 1001 #define M #define INF 10000000 double map[n+m][n+m];
Double dist[n+m];
BOOL Visited[n+m];
struct gas {int idx;
Double Mindis;
Double Averagedis;
BOOL operator < (CONST gas & A) const {if (A.mindis!=mindis) return mindis>a.mindis;
if (A.averagedis!=averagedis) return averagedis<a.averagedis;
Return idx<a.idx;
}}g[m];
int Str2Int (char s[],int n) {int i=0;
if (s[0]== ' G ') i=1;
int sum=i==0?0:n;
Sum+=atoi (S+i);
return sum;
} void Dijkstra (int source,int n) {memset (visited,false,sizeof (visited));
Visited[source] = true;
int i,j;
for (j=1;j<=n;j++) dist[j] = Map[j][source];
Dist[source] = 0;
for (j=0;j<n-1;j++) {int minn = Inf,point;
for (i=1;i<=n;i++) if (!visited[i] && dist[i]<minn) {minn = dist[i];
Point = i;
} Visited[point] = true; for (i=1;i<=n;i++) if (!visited[i]&&dist[i]>dist[point]+map[point][i]) dist[i] = Dist[point]+map[point][i];
int main () {int n,m,k,r;
cin>>n>>m>>k>>r;
Char str1[5],str2[5];
Double dis;
int i,j;
for (i=1;i<=n+m;i++) for (j=1;j<=n+m;j++) map[i][j] = map[j][i] = INF; while (k--) {scanf ("%s%s%lf&