ACM 第三次测验 关于贪心算法

来源:互联网 发布:湖北大学网络课程 编辑:程序博客网 时间:2024/06/01 09:57

火星A+B

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12398    Accepted Submission(s): 4200


Problem Description
读入两个不超过25位的火星正整数A和B,计算A+B。需要注意的是:在火星上,整数不是单一进制的,第n位的进制就是第n个素数。例如:地球上的10进制数2,在火星上记为“1,0”,因为火星个位数是2进制的;地球上的10进制数38,在火星上记为“1,1,1,0”,因为火星个位数是2进制的,十位数是3进制的,百位数是5进制的,千位数是7进制的……
 

Input
测试输入包含若干测试用例,每个测试用例占一行,包含两个火星正整数A和B,火星整数的相邻两位数用逗号分隔,A和B之间有一个空格间隔。当A或B为0时输入结束,相应的结果不要输出。
 

Output
对每个测试用例输出1行,即火星表示法的A+B的值。
 

Sample Input
1,0 2,14,2,0 1,2,01 10,6,4,2,10 0
 

Sample Output
1,0,11,1,1,01,0,0,0,0,0
 

Source
浙大计算机研究生复试上机考试-2006年

#include<stdio.h>  #include<string.h>  #include<math.h>  int prime[26]={0};  int is_prime( int n )  {      int i;      for( i = 2; i <=sqrt(n); i ++ )          if( n%i == 0 ) return 0;          return n;  }  void f()  {      int i=0, t, k = 2;      for(;i<26; )      {          t = is_prime(k++);          if( t )          prime[++i] = t;      }  }  int main()  {      f();      char a[100], b[100];      int c[26], i, j, s1[26], s2[26];      while( ~scanf( "%s %s", a, b ) )      {          int la = strlen(a), lb = strlen(b);          memset( c, 0, sizeof(c) );          memset( s1, 0, sizeof(s1));          memset( s2, 0, sizeof(s2) );          int aa = 0, bb = 0;          i = la-1;          int k = 0, flag = 1;          while( i >= 0 )          {              if( a[i]!=',' )              {                  s1[k] += (a[i]-'0')*flag;                  flag*=10;                  aa+=s1[k];              //printf( "%d..\n", s1[k] );              }              else              {                  ++k;                  flag = 1;              }              --i;          }          i = lb-1;          k = 0;          flag = 1;          while( i >= 0 )          {              if( b[i]!=',' )              {                  s2[k] += (b[i]-'0')*flag;                  flag *=10;                  bb+=s2[k];              //  printf( "%d..\n", s2[k] );              }              else              {                  ++k;                  flag=1;              }              --i;          }          if( aa==0||bb==0 ) break;          for( i = 0; i <= 25; i ++ )          {              c[i]+=s1[i]+s2[i];          //  printf( "%d..", c[i] );              if( c[i]>=prime[i+1] )              {                  ++c[i+1];                  c[i]-=prime[i+1];              }                        }          for( i = 25; i >=0; i -- )          if( c[i] ) break;          for( ; i > 0; i -- )          printf( "%d,", c[i] );          printf( "%d\n", c[0] );      }       return 0;  }  

Greedy?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2125    Accepted Submission(s): 709


Problem Description
iSea is going to be CRAZY! Recently, he was assigned a lot of works to do, so many that you can't imagine. Each task costs Ci time as least, and the worst news is, he must do this work no later than time Di!
OMG, how could it be conceivable! After simple estimation, he discovers a fact that if a work is finished after Di, says Ti, he will get a penalty Ti - Di. Though it may be impossible for him to finish every task before its deadline, he wants the maximum penalty of all the tasks to be as small as possible. He can finish those tasks at any order, and once a task begins, it can't be interrupted. All tasks should begin at integral times, and time begins from 0.
 

Input
The first line contains a single integer T, indicating the number of test cases.
Each test case includes an integer N. Then N lines following, each line contains two integers Ci and Di.

Technical Specification
1. 1 <= T <= 100
2. 1 <= N <= 100 000
3. 1 <= Ci, Di <= 1 000 000 000
 

Output
For each test case, output the case number first, then the smallest maximum penalty.
 

Sample Input
223 42 243 62 74 53 9
 

Sample Output
Case 1: 1Case 2: 3
 

Author
iSea@WHU
 

Source
首届华中区程序设计邀请赛暨第十届武汉大学程序设计大赛
 

/*分析:贪心策略:每次先安排截止时间小的活动。对于两个活动1、2,假设D1<D2,以起始时间为0为例。如果先安排活动1,则扣分最大值为max(C1-D1,C1+C2-D2)。如果先安排活动2,则扣分最大值为max(C2-D2,C1+C2-D1)。显然第二个式子中的C1+C2-D1既大于C1-D1,又大于C1+C2-D2,则选择活动1是明智的。*/#include <cstdio>  #include <algorithm>  using namespace std;  const int maxn = 100000 + 10;  struct Node {      int c, d;      bool operator<(const Node& b) const { return d<b.d; }  } p[maxn];    int main()  {      int T, n, kase = 0;      scanf("%d", &T);      while(T--) {          scanf("%d", &n);          for(int i=0; i<n; i++) scanf("%d%d", &p[i].c, &p[i].d);          sort(p, p+n);          long long sum = 0, ans = 0;          for(int i=0; i<n; i++) {              sum += p[i].c;              if(ans < sum-p[i].d) ans = sum - p[i].d;          }          printf("Case %d: %lld\n", ++kase, ans);      }      return 0;  }  



0 0
原创粉丝点击