当前位置:   article > 正文

暑假训练赛20160727  <贪心,思维,二分图--最小点覆盖>_重复选择度数最高的顶点,并去 掉所有邻接边。给出一个例子,说明该贪心算法不是 2-

重复选择度数最高的顶点,并去 掉所有邻接边。给出一个例子,说明该贪心算法不是 2-

Time Limit: 3000MS  64bit IO Format: %lld & %llu

 Status uDebug

Description

Download as PDF

Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, so an island in the sea can be covered by a radius installation, if the distance between them is at most d .

We use Cartesian coordinate system, defining the coasting is the x -axis. The sea side is above x -axis, and the land side below. Given the position of each island in the sea, and given the distance of the coverage of the radar installation, your task is to write a program to find the minimal number of radar installations to cover all the islands. Note that the position of an island is represented by its x - y coordinates.

\epsfbox{p2519.eps}

Input 

The input consists of several test cases. The first line of each case contains two integers n(1$ \le$n$ \le$1000) and d , where n is the number of islands in the sea and d is the distance of coverage of the radar installation. This is followed by n lines each containing two integers representing the coordinate of the position of each island. Then a blank line follows to separate the cases.

The input is terminated by a line containing pair of zeros.

Output 

For each test case output one line consisting of the test case number followed by the minimal number of radar installations needed. `-1' installation means no solution for that case.

Sample Input 

3 2
1 2
-3 1
2 1

1 2
0 2

0 0

Sample Output 

Case 1: 2
Case 2: 1



代码:

  1. #include<cmath>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6. struct node{
  7. double l,r;
  8. }dian[1100];
  9. bool cmp(node xx,node yy)
  10. {
  11. return xx.r<yy.r;
  12. }
  13. int main()
  14. {
  15. int n;int ca=1;
  16. double x,y,d,pp;
  17. while (scanf("%d%lf",&n,&d),n+d)
  18. {
  19. bool fafe=false;
  20. for (int i=0;i<n;i++)
  21. {
  22. scanf("%lf%lf",&x,&y);
  23. if (y>d)
  24. fafe=true;
  25. else
  26. {
  27. pp=sqrt(d*d-y*y);
  28. dian[i].l=x-pp;
  29. dian[i].r=x+pp;
  30. }
  31. }
  32. if (fafe)
  33. {
  34. printf("Case %d: -1\n",ca++);
  35. continue;
  36. }
  37. sort(dian,dian+n,cmp);
  38. int s=1;
  39. double di=dian[0].r;
  40. for (int i=1;i<n;i++)
  41. if (dian[i].l>di)
  42. {
  43. di=dian[i].r;
  44. s++;
  45. }
  46. printf("Case %d: %d\n",ca++,s);
  47. }
  48. return 0;
  49. }


Time Limit: 3000MS  64bit IO Format: %lld & %llu

 Status uDebug

Description

Download as PDF

Suppose there are M people, including you, playing a special card game. At the beginning, each player receives N cards. The pip of a card is a positive integer which is at most N*M . And there are no two cards with the same pip. During a round, each player chooses one card to compare with others. The player whose card with the biggest pip wins the round, and then the next round begins. After N rounds, when all the cards of each player have been chosen, the player who has won the most rounds is the winner of the game.

Given your cards received at the beginning, write a program to tell the maximal number of rounds that you may at least win during the whole game.

Output

For each test case, output a line consisting of the test case number followed by the number of rounds you will at least win during the game.

Sample Input

2 5
1 7 2 10 9

6 11
62 63 54 66 65 61 57 56 50 53 48

0 0

Sample Output

Case 1: 2
Case 2: 4

代码:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. using namespace std;
  5. bool cmp(int xx,int yy)
  6. {return xx>yy;}
  7. int main()
  8. {
  9. int m,n;
  10. int shu[4010];int ca=1;
  11. while (scanf("%d%d",&m,&n),m+n)
  12. {
  13. for (int i=0;i<n;i++)
  14. scanf("%d",&shu[i]);
  15. sort(shu,shu+n,cmp);
  16. int ki=n*m;int s=0;
  17. for (int i=0;i<n;i++)
  18. {
  19. if (shu[i]==ki)
  20. {
  21. ki--;
  22. s++;
  23. }
  24. else
  25. {
  26. ki-=2;
  27. }
  28. }
  29. printf("Case %d: %d\n",ca++,s);
  30. }
  31. return 0;
  32. }


G - Machine Schedule
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Download as PDF

As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history. Scheduling problems differ widely in the nature of the constraints that must be satisfied and the type of schedule desired. Here we consider a 2-machine scheduling problem.

There are two machines A and B . Machine A has n kinds of working modes, which is called mode0 , mode1 , ... , moden-1 , likewise machine B has m kinds of working modes, mode0 , mode1 , ... , modem-1 . At the beginning they are both work at mode0 .

For k jobs given, each of them can be processed in either one of the two machines in particular mode. For example, job 0 can either be processed in machine A at mode3 or in machine B at mode4 , job 1 can either be processed in machine A at mode2 or in machine B atmode4 , and so on. Thus, for job i, the constraint can be represent as a triple (ixy) , which means it can be processed either in machineA at modex , or in machine B at modey .

Obviously, to accomplish all the jobs, we need to change the machine's working mode from time to time, but unfortunately, the machine's working mode can only be changed by restarting it manually. By changing the sequence of the jobs and assigning each job to a suitable machine, please write a program to minimize the times of restarting machines.

Input

The input file for this program consists of several configurations. The first line of one configuration contains three positive integers: n ,m(nm < 100) and k(k < 1000) . The following k lines give the constrains of the k jobs, each line is a triple: ixy .

The input will be terminated by a line containing a single zero.

Output

The output should be one integer per line, which means the minimal times of restarting machine.

Sample Input

5 5 10
0 1 1
1 1 2
2 1 3
3 1 4
4 2 1
5 2 2
6 2 3
7 2 4
8 3 3
9 4 3
0

Sample Output

3



好神奇的二分图-----最小点覆盖=最大匹配数


最大匹配数:在所有的顶点都两两被不重复的线连接起来时的边数

二分图求最小顶点覆盖:即用最少的顶点个数可以让每条边至少与其中一个点关联



代码:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. using namespace std;
  5. int n,m,k;
  6. int cover[120];
  7. int qian[120],dis[120][120];
  8. int find(int xx)
  9. {
  10. for (int i=1;i<m;i++)
  11. {
  12. if (dis[xx][i]&&!cover[i])
  13. {
  14. cover[i]=1;
  15. if (!qian[i]||find(qian[i]))
  16. {
  17. qian[i]=xx;
  18. return true;
  19. }
  20. }
  21. }
  22. return false;
  23. }
  24. int main()
  25. {
  26. while (scanf("%d",&n),n)
  27. {
  28. memset(dis,0,sizeof(dis));
  29. scanf("%d%d",&m,&k);
  30. for (int i=0;i<k;i++)
  31. {
  32. int a,b,c;
  33. scanf("%d%d%d",&a,&b,&c);
  34. if (b&&c)
  35. dis[b][c]=1;
  36. }
  37. int s=0;memset(qian,0,sizeof(qian));
  38. for (int i=1;i<n;i++)
  39. {
  40. memset(cover,0,sizeof(cover));
  41. if (find(i))
  42. s++;
  43. }
  44. printf("%d\n",s);
  45. }
  46. return 0;
  47. }



Time Limit: 3000MS  64bit IO Format: %lld & %llu

 Status uDebug

Description

Download as PDF

Mileage program of ACM (Airline of Charming Merlion) is really nice for the travelers flying frequently. Once you complete a flight with ACM, you can earn ACMPerk miles in your ACM Mileage Bank depended on mileage you actual fly. In addition, you can use the ACMPerk mileage in your Mileage Bank to exchange free flight ticket of ACM in future.

The following table helps you calculate how many ACMPerk miles you can earn when you fly on ACM.


When you fly ACMClass CodeYou'll earn
First ClassFActual mileage + 100% mileage Bonus
Business ClassBActual mileage + 50% mileage Bonus
Economy ClassY 
1-500 miles 500 miles
500+ miles Actual mileage


It's shown that your ACMPerk mileage consists of two parts. One is your actual flight mileage (the minimum ACMPerk mileage for Economy Class for one flight is 500 miles), the other is the mileage bonus (its accuracy is up to 1 mile) when you fly in Business Class and First Class. For example, you can earn 1329 ACMPerk miles, 1994 ACMPerk miles and 2658 ACMPerk miles for Y, B or F class respectively for the fly from Beijing to Tokyo (the actual mileage between Beijing and Tokyo is 1329 miles). When you fly from Shanghai to Wuhan, you can earn ACMPerk 500 miles for economy class and ACMPerk 650 miles for business class (the actual mileage between Shanghai and Wuhan is 433 miles).

Your task is to help ACM build a program for automatic calculation of ACMPerk mileage.

Output

Output the summary of ACMPerk mileages for each test case, one per line.

Sample Input

Beijing Tokyo 1329 F
Shanghai Wuhan 433 Y
0
#

Sample Output

3158

代码“:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. using namespace std;
  5. int main()
  6. {
  7. char ch[2000];
  8. while (scanf("%s",ch),ch[0]!='#')
  9. {
  10. double s=0,a;
  11. char pp[5];
  12. scanf("%s%lf%s",ch,&a,pp);
  13. if (pp[0]=='F')
  14. s+=2*a;
  15. else if (pp[0]=='B')
  16. s+=1.5*a;
  17. else if (a<500)
  18. s+=500;
  19. else
  20. s+=a;
  21. while (scanf("%s",ch),ch[0]!='0')
  22. {
  23. scanf("%s%lf%s",ch,&a,pp);
  24. if (pp[0]=='F')
  25. s+=2*a;
  26. else if (pp[0]=='B')
  27. s+=1.5*a;
  28. else if (a<500)
  29. s+=500;
  30. else
  31. s+=a;
  32. }
  33. printf("%d\n",int(s+0.5));
  34. }
  35. return 0;
  36. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/856717
推荐阅读
相关标签
  

闽ICP备14008679号