Category Archives: POJ

POJ 1011-Sticks

POJ 1011-Sticks Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 121990 Accepted: 28247 Description George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were originally. Please help him and design a program which computes the smallest possible original length of those sticks. All lengths expressed in units are integers greater than zero. Input The input contains blocks of 2 lines. The first line contains the number of sticks parts after cutting, there are at most 64 sticks. The second line contains the lengths of those parts separated by the space. The last line of the file contains zero. Output The output should contains the smallest possible length of original sticks, one per line. Sample Input 9 5 2 1 5 2 1 5 2 1 4 1 2 3 4 Sample Output 6 5 Source Central Europe 1995


这个题是个深度搜索的好题目,题目的大意为:将一摞长度相同的棍子随机砍成不同长度的小棍子,问原始棍子的长度是多少。 设要求的原始棍子长度为original_len,砍乱之后,所有小棍子的长度总和为sum,因为原来都是整数根相同长度的棍子,所以sum一定是original_len的整数倍;而且因为棍子是随机砍乱的,所以original_len一定不小于砍乱的小棍子中的最大值。由此可以得出original_len的一个取值范围。然后再利用深度搜索求解。 在做算法题时,搜索题算比较难的了,由于我之前这方面的题练的也比较少,所以dfs的思路不太清晰,后来参考了这篇博文,里面关于剪枝的说明也挺详细的,我理解了他的代码,然后自己重新写了一遍,并在某些细节有所改动。代码如下: [cpp] #include <iostream> #include<algorithm> using namespace std; int total_sticks,n,original_len;//分别表示没截断之前有几根棍子,总的截断的数量,没截断之前每根棍子的长度 int sticks[65];//截断之后每部分的长度 int mark[65];//指示每一段是否搜索过了 void init_mark() { for(int i=0;i<65;i++) mark[i]=0; } bool cmp(int a,int b) { return a>b; } //深度搜索,参数分别表示已经拼接好了几根棍子,当前正在拼接的棍子已经拼了多长了,当前测试的下标 bool dfs(int done_sticks,int done_parts,int pos) { if(done_sticks==total_sticks)//如果所有棍子都拼好了,成功 return true; for(int i=pos+1;i<n;i++)//从pos+1开始测试 { if(mark[i]==1)//如果这根小棍子已经拼过了,则开始下一个 continue; if(done_parts+sticks[i]==original_len)//刚好拼成了一个原始的棍子 { mark[i]=1;//标记 if(dfs(done_sticks+1,0,-1))//继续深度搜索,注意pos=-1,因为之前搜索的时候有些前面的小棍子可能没用上 return true; mark[i]=0;//如果搜索失败,回溯 return false; } else if(done_parts+sticks[i]<original_len) { mark[i]=1; if(dfs(done_sticks,done_parts+sticks[i],i))//继续深度搜索,这时pos=i,因为继续往下搜的过程中,只能越取越小,因为这个时候还没有拼成一个完整的棍子,且之前已经搜过大的了 return true; mark[i]=0;//如果搜索失败,回溯 if(done_parts==0)//如果这根小棍子是某次拼接时的第一个棍子,则失败 return false; while(sticks[i]==sticks[i+1])//否则找下一个小棍子时,跳过数值相同的小棍子,因为肯定失败 i++; } } return false; } int main() { while(cin>>n&&n) { int sum=0; for(int i=0;i<n;i++) { cin>>sticks[i]; sum+=sticks[i];//求和 } sort(sticks,sticks+n,cmp);//按降序排序 for(original_len=sticks[0];original_len<=sum;original_len++) { if(sum%original_len==0)//总和肯定能被原始棍子的长度整除 { total_sticks=sum/original_len; init_mark();//每次都要初始化 if(dfs(0,0,-1))//dfs初始值 { cout<<original_len<<endl; break; } } } } return 0; } [/cpp] 代码都已经做了详细的注释,应该不难看懂,唯一和参考博客不同的地方是进行dfs时初始值的不同,我的是0,0,-1,不知道为什么原博客是1,0,-1,按理说最开始还没有组成一个完整的棍子啊,为什么done_sticks=1呢? 我的代码提交AC,用时16MS,内存220K。]]>

POJ 1006-Biorhythms

POJ 1006-Biorhythms Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 114815 Accepted: 35991 Description Some people believe that there are three cycles in a person’s life that start the day he or she is born. These three cycles are the physical, emotional, and intellectual cycles, and they have periods of lengths 23, 28, and 33 days, respectively. There is one peak in each period of a cycle. At the peak of a cycle, a person performs at his or her best in the corresponding field (physical, emotional or mental). For example, if it is the mental curve, thought processes will be sharper and concentration will be easier. Since the three cycles have different periods, the peaks of the three cycles generally occur at different times. We would like to determine when a triple peak occurs (the peaks of all three cycles occur in the same day) for any person. For each cycle, you will be given the number of days from the beginning of the current year at which one of its peaks (not necessarily the first) occurs. You will also be given a date expressed as the number of days from the beginning of the current year. You task is to determine the number of days from the given date to the next triple peak. The given date is not counted. For example, if the given date is 10 and the next triple peak occurs on day 12, the answer is 2, not 3. If a triple peak occurs on the given date, you should give the number of days to the next occurrence of a triple peak. Input You will be given a number of cases. The input for each case consists of one line of four integers p, e, i, and d. The values p, e, and i are the number of days from the beginning of the current year at which the physical, emotional, and intellectual cycles peak, respectively. The value d is the given date and may be smaller than any of p, e, or i. All values are non-negative and at most 365, and you may assume that a triple peak will occur within 21252 days of the given date. The end of input is indicated by a line in which p = e = i = d = -1. Output For each test case, print the case number followed by a message indicating the number of days to the next triple peak, in the form: Case 1: the next triple peak occurs in 1234 days. Use the plural form “days” even if the answer is 1. Sample Input 0 0 0 0 0 0 0 100 5 20 34 325 4 5 6 7 283 102 23 320 203 301 203 40 -1 -1 -1 -1 Sample Output Case 1: the next triple peak occurs in 21252 days. Case 2: the next triple peak occurs in 21152 days. Case 3: the next triple peak occurs in 19575 days. Case 4: the next triple peak occurs in 16994 days. Case 5: the next triple peak occurs in 8910 days. Case 6: the next triple peak occurs in 10789 days. Source East Central North America 1999


这一题考的是中学数学,稍微分析一下:假设最终结果为rs,则距离第一天rs+d=M,也就是说在第M天,三种周期同时达到高潮,又因为三种周期某次高潮的时间为第p、e、i天,周期分别为23、28、33,所以可得M-p、M-e、M-i分别为23、28、33的倍数。又因为题干中有如下两句话:
If a triple peak occurs on the given date, you should give the number of days to the next occurrence of a triple peak. and you may assume that a triple peak will occur within 21252 days of the given date.
所以M的取值范围为[d+1,d+21252],最后就是用穷举法来一个一个试了。具体代码如下: [cpp] #include<iostream> #include<string> using namespace std; int main() { int p,e,i,d; int case_num=1; while(cin>>p>>e>>i>>d&&p!=-1&&e!=-1&&i!=-1&&d!=-1) { for(int rs=d+1;rs<=21252+d;rs++) { if(((rs-p)%23==0)&&((rs-e)%28==0)&&((rs-i)%33==0)) { cout<<"Case "<<case_num<<": the next triple peak occurs in "<<rs-d<<" days."<<endl; break; } } case_num++; } return 0; } [/cpp] 提交结果AC,用时797MS,内存216K,这里还有几种优化的方法,可以参考。]]>

POJ 1005-I Think I Need a Houseboat

POJ 1005-I Think I Need a Houseboat I Think I Need a Houseboat Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 87833 Accepted: 38137 Description Fred Mapper is considering purchasing some land in Louisiana to build his house on. In the process of investigating the land, he learned that the state of Louisiana is actually shrinking by 50 square miles each year, due to erosion caused by the Mississippi River. Since Fred is hoping to live in this house the rest of his life, he needs to know if his land is going to be lost to erosion. After doing more research, Fred has learned that the land that is being lost forms a semicircle. This semicircle is part of a circle centered at (0,0), with the line that bisects the circle being the X axis. Locations below the X axis are in the water. The semicircle has an area of 0 at the beginning of year 1. (Semicircle illustrated in the Figure.) Input The first line of input will be a positive integer indicating how many data sets will be included (N). Each of the next N lines will contain the X and Y Cartesian coordinates of the land Fred is considering. These will be floating point numbers measured in miles. The Y coordinate will be non-negative. (0,0) will not be given. Output For each data set, a single line of output should appear. This line should take the form of: “Property N: This property will begin eroding in year Z.” Where N is the data set (counting from 1), and Z is the first year (start from 1) this property will be within the semicircle AT THE END OF YEAR Z. Z must be an integer. After the last data set, this should print out “END OF OUTPUT.” Sample Input 2 1.0 1.0 25.0 0.0 Sample Output Property 1: This property will begin eroding in year 1. Property 2: This property will begin eroding in year 20. END OF OUTPUT. Hint 1.No property will appear exactly on the semicircle boundary: it will either be inside or outside. 2.This problem will be judged automatically. Your answer must match exactly, including the capitalization, punctuation, and white-space. This includes the periods at the ends of the lines. 3.All locations are given in miles. Source Mid-Atlantic 2001


这题也不难,常规数学题,侵蚀面积以每年50平方米的速度进行,可以由此算出每年之后总的侵蚀面积的半径,然后通过比较给出的x、y到原点的距离判断是否在侵蚀面积之内。具体代码如下: [cpp] #include<iostream> #include<string> #include<cmath> using namespace std; double R[100];//Fred is hoping to live in this house the rest of his life.余生最多设为100年 //初始化 void init() { for(int i=0;i<100;i++) { R[i]=sqrt(2*50*(i+1)/3.14); } } //二分查找 int bin_search(double r) { int start=0,end=99,mid; while(start<=end) { mid=(start+end)/2; if(R[mid]>r) end=mid-1; else start=mid+1; } //这个时候start==end if(R[start]<r) return start+1; else return start; } int main() { init(); int n; double x,y; cin>>n; for(int i=1;i<=n;i++) { cin>>x>>y; double r=sqrt(x*x+y*y); cout<<"Property "<<i<<": This property will begin eroding in year "<<bin_search(r)+1<<"."<<endl; } cout<<"END OF OUTPUT."; return 0; } [/cpp] 本代码提交AC,用时0MS,内存220K。]]>

POJ 1002-487-3279

POJ 1002-487-3279 487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 245807 Accepted: 43582 Description Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phrase. For example, you can call the University of Waterloo by dialing the memorable TUT-GLOP. Sometimes only part of the number is used to spell a word. When you get back to your hotel tonight you can order a pizza from Gino’s by dialing 310-GINO. Another way to make a telephone number memorable is to group the digits in a memorable way. You could order your pizza from Pizza Hut by calling their “three tens’’ number 3-10-10-10. The standard form of a telephone number is seven decimal digits with a hyphen between the third and fourth digits (e.g. 888-1200). The keypad of a phone supplies the mapping of letters to numbers, as follows: A, B, and C map to 2 D, E, and F map to 3 G, H, and I map to 4 J, K, and L map to 5 M, N, and O map to 6 P, R, and S map to 7 T, U, and V map to 8 W, X, and Y map to 9 There is no mapping for Q or Z. Hyphens are not dialed, and can be added and removed as necessary. The standard form of TUT-GLOP is 888-4567, the standard form of 310-GINO is 310-4466, and the standard form of 3-10-10-10 is 310-1010. Two telephone numbers are equivalent if they have the same standard form. (They dial the same number.) Your company is compiling a directory of telephone numbers from local businesses. As part of the quality control process you want to check that no two (or more) businesses in the directory have the same telephone number. Input The input will consist of one case. The first line of the input specifies the number of telephone numbers in the directory (up to 100,000) as a positive integer alone on the line. The remaining lines list the telephone numbers in the directory, with each number alone on a line. Each telephone number consists of a string composed of decimal digits, uppercase letters (excluding Q and Z) and hyphens. Exactly seven of the characters in the string will be digits or letters. Output Generate a line of output for each telephone number that appears more than once in any form. The line should give the telephone number in standard form, followed by a space, followed by the number of times the telephone number appears in the directory. Arrange the output lines by telephone number in ascending lexicographical order. If there are no duplicates in the input print the line: No duplicates. Sample Input 12 4873279 ITS-EASY 888-4567 3-10-10-10 888-GLOP TUT-GLOP 967-11-11 310-GINO F101010 888-1200 -4-8-7-3-2-7-9- 487-3279 Sample Output 310-1010 2 487-3279 4 888-4567 3 Source East Central North America 1999


这题的思路其实很简单,使用C++的map搞定。 首先将字符串转换成标准的格式,然后将题目中给的对应关系转换成一个简单的hash数组,将字符串中的字符转换成数字(char),最后加入到map中,加入的方法使用map[s]++,如果s在map中,则s出现的次数加1;反之,则将s加入到map中,然后次数加1,即0++=1。这个方法很巧妙,是我从《C++ Primer》上看到的。 最后就是遍历map,如果s出现次数大于1,则将其输出;如果都不大于1,则输出No duplicates. 代码如下: [cpp] #include<iostream> #include<map> #include<string> using namespace std; char hash_h[26]={‘2′,’2′,’2′,’3′,’3′,’3′,’4′,’4′,’4′,’5′,’5′,’5′,’6′,’6′,’6′,’7′,’0′,’7′,’7′,’8′,’8′,’8′,’9′,’9′,’9′,’0′};//题目给出的map对应关系,其中q和z对应0 //将输入转换成标准格式 string get_standard_form(string s) { string rs=""; int s_size=s.size(); for(int i=0;i<s_size;i++) { if(s[i]!=’-‘)//将s中的连接符都去掉 rs+=s[i]; } s_size=rs.size(); for(int i=0;i<s_size;i++) { if(rs[i]>=’A’&&rs[i]<=’Z’) rs[i]=hash_h[rs[i]-‘A’];//替换 } return rs.substr(0,3)+"-"+rs.substr(3);//添加连接符 } int main() { int n; string s; cin>>n; map<string,int> msi; while(n–) { cin>>s; msi[get_standard_form(s)]++;//使用map存储 } map<string,int>::iterator it=msi.begin();//充分利用map的特性 int count=0; while(it!=msi.end())//map已经自动按字典序排好了 { if(it->second>1) { cout<<it->first<<" "<<it->second<<endl; count++; } it++; } if(count==0)//记得判断是否有重复的! cout<<"No duplicates. "<<endl; return 0; } [/cpp] 本代码提交AC,用时1594MS,内存5160K。]]>

POJ 1004-Financial Management

POJ 1004-Financial Management Financial Management Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 138422 Accepted: 57882 Description Larry graduated this year and finally has a job. He’s making a lot of money, but somehow never seems to have enough. Larry has decided that he needs to grab hold of his financial portfolio and solve his financing problems. The first step is to figure out what’s been going on with his money. Larry has his bank account statements and wants to see how much money he has. Help Larry by writing a program to take his closing balance from each of the past twelve months and calculate his average account balance. Input The input will be twelve lines. Each line will contain the closing balance of his bank account for a particular month. Each number will be positive and displayed to the penny. No dollar sign will be included. Output The output will be a single number, the average (mean) of the closing balances for the twelve months. It will be rounded to the nearest penny, preceded immediately by a dollar sign, and followed by the end-of-line. There will be no other spaces or characters in the output. Sample Input 100.00 489.12 12454.12 1234.10 823.05 109.20 5.27 1542.25 839.18 83.99 1295.01 1.75 Sample Output $1581.42 Source Mid-Atlantic 2001


这一题是目前遇到的最简单的题目,求输入数组的平均值,注意如果使用scanf输入double的话,格式为scanf(“%lf”,&d),求得平均值之后,输出保留两位小数为printf(“%.2f”,d)。 代码如下: [cpp] #include<stdio.h> int main() { double rs=0,d; for(int i=0;i<12;i++) { scanf("%lf",&d); rs+=d; } printf("$%.2f",rs/12); return 0; } [/cpp] 本代码提交AC,用时0MS,内存140K。]]>