Problem 1: Ski Lessons [Brian Jacokes, 2002] Farmer John wants to take Bessie skiing in Colorado. Sadly, Bessie is not really a very good skier. Bessie has learned that the ski resort is offering S (0 <= S <= 100) ski classes throughout the day. Lesson i starts at time M_i (1 <= M_i <= 10,000) and lasts for time L_i (1 <= L_i <= 10,000). After lesson i, Bessie's ski ability becomes A_i (1 <= A_i <= 100). Note: this ability is an absolute, not an incremental change. Bessie has purchased a map which shows all N (1 <= N <= 10,000) ski slopes along with the time D_i (1 <= D_i <= 10,000) required to ski down slope i and the skill level C_i (1 <= C_i <= 100) required to get down the slope safely. Bessie's skill level must be greater than or equal to the skill level of the slope in order for her to ski down it. Bessie can devote her time to skiing, taking lessons, or sipping hot cocoa but must leave the ski resort by time T (1 <= T <= 10,000), and that means she must complete the descent of her last slope without exceeding that time limit. Find the maximum number of runs Bessie can complete within the time limit. She starts the day at skill level 1. Extra feedback will be provided on the first 50 submissions. PROBLEM NAME: ski INPUT FORMAT: * Line 1: Three space-separated integers: T, S, and N * Lines 2..S+1: Line i+1 describes ski lesson i with three space-separated integers: M_i, L_i, and A_i * Lines S+2..S+N+1: Line S+i+1 describes ski slope i with two space-separated integers: C_i and D_i. SAMPLE INPUT (file ski.in): 10 1 2 3 2 5 4 1 1 3 OUTPUT FORMAT: A single integer on a line by itself, the maximum number of runs that Bessie may ski within the time limit. SAMPLE OUTPUT (file ski.out): 6 OUTPUT DETAILS: Ski the second slope once, take the lesson, and ski the first slope 5 times before time is up: a total of 6 slopes.
Work Scheduling [Richard Peng, 2008] 时间限制: 1 Sec 内存限制: 128 MB 题目描述 Farmer John has so very many jobs to do! In order to run the farm efficiently, he must make money on the jobs he does, each one of which takes just one time unit. His work day starts at time 0 and has 1,000,000,000 time units (!). He currently can choose from any of N (1 <= N <= 100,000) jobs conveniently numbered 1..N for work to do. It is possible but extremely unlikely that he has time for all N jobs since he can only work on one job during any time unit and the deadlines tend to fall so that he can not perform all the tasks. Job i has deadline D_i (1 <= D_i <= 1,000,000,000). If he finishes job i by then, he makes a profit of P_i (1 <= P_i <= 1,000,000,000). What is the maximum total profit that FJ can earn from a given list of jobs and deadlines? The answer might not fit into a 32-bit integer. 输入 * Line 1: A single integer: N * Lines 2..N+1: Line i+1 contains two space-separated integers: D_i and P_i 输出 * Line 1: A single number on a line by itself that is the maximum possible profit FJ can earn. 样例输入 2 10 5 7 样例输出 提示 OUTPUT DETAILS: Complete job 3 (1,7) at time 1 and complete job 1 (2,10) at time 2 to maximize the earnings (7 + 10 -> 17).
仔细分析不难发现对于前i时间,我们只需保留i个价值最大的任务,那么可以用堆维护。
Problem 3: Tower of Hay [Brian Dean, 2009] Cows so hate the dark. In order to change a lightbulb at the top of the barn, Bessie must build a tower out of bales of hay to climb so that she can reach it. The N (1 <= N <= 100,000) bales of hay (conveniently numbered 1..N) enter the barn sequentially on a conveyor belt. Bale i has integer width w_i (1 <= w_i <= 10,000); all bales have depth and height of one unit. Bessie must use all N hay bales to build the tower and must lay them down in the order they arrive. She can lay as many bales as she wishes (tightly packed in a line, of course) to create the foundation of the tower. She can then lay some of the next bales on top of the previous level to create the next level (no level can be wider than the level beneath it). She continues this process until all the bales are used. She must stack the bales in the order they arrive, from the bottom to the top of the tower. To be clear: she must not try to sneak a bale back on the foundation after a bale is placed on the second layer. Bessie's goal is to create the tallest tower possible -- and she already knows how wide each every bale of hay will be as it comes into the barn on the belt. How tall a tower can she construct? PROBLEM NAME: tower INPUT FORMAT: * Line 1: A single integer: N * Lines 2..N+1: Line i+1 contains a single integer: w_i SAMPLE INPUT (file tower.in): 3 1 2 3 OUTPUT FORMAT: * Line 1: A single integer, the height of the tallest possible tower that can be built SAMPLE OUTPUT (file tower.out): 2 OUTPUT DETAILS: Use the first bales with width 1 and 2 for the bottom row (total width: 3), then the next bale (width 3) for the top row: +----------+ | 3 | +---+------+ | 1 | 2 | +---+------+ **********************************************************************
真是好题,看了题解之后什么都会了。。具体看这里:http://www.cnblogs.com/lazycal/archive/2012/11/07/2759427.html
Problem 4: Bovine Embroidery [Brian Dean, 2009] Bessie has taken up the detailed art of bovine embroidery. Cows embroider a cloth mounted in a circular hoop of integer radius d (1 <= d <= 50,000). They sew N (2 <= N <= 50,000) threads, each in a straight line from one point on the edge of the hoop to another point on the edge of the hoop (no two embroidered points share a location on the hoop's edge). Being mathematically inclined, Bessie knows a formula of the form ax + by + c = 0 for each straight line piece of thread. Conveniently, a, b, and c are integers (-1,000,000 <= a <= 1,000,000; -1,000,000 <= b <= 1,000,000; -1,000,000 <= c <= 1,000,000). Even more conveniently, no two threads coincide exactly. Perhaps less conveniently, Bessie knows that her set of formula coefficients also includes a number of formulae for threads that do not appear to pass inside the hoop's circle. She regrets this greatly. The origin (0,0) is in the precise middle of the hoop, so all points on the hoop's edge are distance d from the origin. At least one of the coefficients a and b is non-zero for each thread's formula. Bovine embroidery is more highly regarded when the number of thread intersections is maximized. Help Bessie: count the number of pairs of threads that intersect on the cloth (i.e., within distance d of the origin). Note that if three threads happen to coincide at the same point, that would be three pairs of intersections. Four threads at the same point -> six pairs of intersections, etc. PROBLEM NAME: cowemb INPUT FORMAT: * Line 1: Two space-separated integers: N and d * Lines 2..N+1: Line i+1 describes thread i with three integers: a, b, and c SAMPLE INPUT (file cowemb.in): 2 1 1 0 0 0 1 0 INPUT DETAILS: The two lines are x=0 and y=0. OUTPUT FORMAT: * Line 1: One integer, on a line by itself, that is the count of pairs of threads that intersect. SAMPLE OUTPUT (file cowemb.out): 1 OUTPUT DETAILS: The two lines intersect at (0,0), which is clearly with 1 of the origin.
Codes:
1 #include<queue> 2 #include<cmath> 3 #include<cstdio> 4 #include<cstdlib> 5 #include<cstring> 6 #include<iostream> 7 #include<algorithm> 8 using namespace std; 9 #define For(i,n) for(int i=1;i<=n;i++) 10 #define Rep(i,l,r) for(int i=l;i<=r;i++) 11 12 struct jobs{ 13 int times,profit; 14 }G[100100]; 15 16 struct cmp{ 17 bool operator () (jobs A,jobs B){ 18 return A.profit>B.profit; 19 } 20 }; 21 22 priority_queue<jobs,vector<jobs>,cmp> T; 23 24 long long ans; 25 int n,x,y,limits; 26 27 bool CMP(jobs A,jobs B){ 28 return A.times<B.times; 29 } 30 31 int main(){ 32 scanf("%d",&n); 33 For(i,n){ 34 scanf("%d%d",&x,&y); 35 G[i].times = x; G[i].profit = y; 36 } 37 sort(G+1,G+n+1,CMP); 38 For(i,n){ 39 T.push(G[i]); 40 limits = G[i].times; 41 while(T.size()>limits) T.pop(); 42 } 43 while(T.size()){ 44 ans+=T.top().profit; 45 T.pop(); 46 } 47 cout<<ans<<endl; 48 return 0; 49 } 50 -----------------------------以上是B题------------------------------- 51 #include<cstdio> 52 #include<cstdlib> 53 #include<cstring> 54 #include<iostream> 55 #include<algorithm> 56 using namespace std; 57 const int N = 100010; 58 #define For(i,n) for(int i=1;i<=n;++i) 59 #define Rep(i,l,r) for(int i=l;i<=r;++i) 60 #define Down(i,r,l) for(int i=r;i>=l;--i) 61 int n,A[N],sum[N],Ans[N],f[N]; 62 int q[N],l = 1 , r = 1; 63 64 void init(){ 65 scanf("%d",&n); 66 For(i,n){ 67 scanf("%d",&A[i]); 68 sum[i] = sum[i-1] + A[i]; 69 } 70 } 71 72 void DP(){ 73 q[r] = n+1; 74 Down(i,n,1){ 75 while(l<r && f[q[l+1]]<=sum[q[l+1]-1]-sum[i-1]) l++;//找出决策j 76 f[i] = sum[q[l]] - sum[i-1]; 77 Ans[i] = Ans[q[l]] + 1; 78 q[++r] = i; 79 while(l<r && (sum[q[r-1]-1] - f[q[r-1]] < sum[q[r]-1] - f[q[r]])) q[--r] = q[r+1]; 80 } 81 printf("%d\n",Ans[1]); 82 } 83 84 int main(){ 85 init(); 86 DP(); 87 return 0; 88 } 89 ---------------------------------以上是C题---------------------------