2016寒假冬训练习#1

来源:互联网 发布:淘宝女装主图视频 编辑:程序博客网 时间:2024/05/16 17:15

https://vjudge.net/contest/151248#overview


A

Mohammad has recently visited Switzerland. As he loves his friends very much, he decided to buy some chocolate for them, but as this fine chocolate is very expensive(You know Mohammad is a little BIT stingy!), he could only afford buying one chocolate, albeit a very big one (part of it can be seen in figure 1) for all of them as a souvenir. Now, he wants to give each of his friends exactly one part of this chocolate and as he believes all human beings are equal (!), he wants to split it into equal parts.

The chocolate is an  rectangle constructed from  unit-sized squares. You can assume that Mohammad has also  friends waiting to receive their piece of chocolate.

To split the chocolate, Mohammad can cut it in vertical or horizontal direction (through the lines that separate the squares). Then, he should do the same with each part separately until he reaches  unit size pieces of chocolate. Unfortunately, because he is a little lazy, he wants to use the minimum number of cuts required to accomplish this task.

Your goal is to tell him the minimum number of cuts needed to split all of the chocolate squares apart.

 

Figure 1. Mohammad’s chocolate

Input

The input consists of several test cases. In each line of input, there are two integers , the number of rows in the chocolate and , the number of columns in the chocolate. The input should be processed until end of file is encountered.

Output

For each line of input, your program should produce one line of output containing an integer indicating the minimum number of cuts needed to split the entire chocolate into unit size pieces.

Sample Input

2 21 11 5

Sample Output

304

稍微想以下就好了。。。

#include<iostream>using namespace std;int min(int a,int b){return a>b?b:a;}int main(){int a,b;while(cin>>a>>b)cout<<min(a-1+a*(b-1),b-1+b*(a-1))<<endl;}

B题

The Dragon of Loowater

Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem.

The shores of Rellau Creek in central Loowater had always been a prime breeding ground for geese. Due to the lack of predators, the geese population was out of control. The people of Loowater mostly kept clear of the geese. Occasionally, a goose would attack one of the people, and perhaps bite off a finger or two, but in general, the people tolerated the geese as a minor nuisance.

One day, a freak mutation occurred, and one of the geese spawned a multi-headed fire-breathing dragon. When the dragon grew up, he threatened to burn the Kingdom of Loowater to a crisp. Loowater had a major problem. The king was alarmed, and called on his knights to slay the dragon and save the kingdom.

The knights explained: "To slay the dragon, we must chop off all its heads. Each knight can chop off one of the dragon's heads. The heads of the dragon are of different sizes. In order to chop off a head, a knight must be at least as tall as the diameter of the head. The knights' union demands that for chopping off a head, a knight must be paid a wage equal to one gold coin for each centimetre of the knight's height."

Would there be enough knights to defeat the dragon? The king called on his advisors to help him decide how many and which knights to hire. After having lost a lot of money building Mir Park, the king wanted to minimize the expense of slaying the dragon. As one of the advisors, your job was to help the king. You took it very seriously: if you failed, you and the whole kingdom would be burnt to a crisp!

Input Specification:

The input contains several test cases. The first line of each test case contains two integers between 1 and 20000 inclusive, indicating the numbern of heads that the dragon has, and the numberm of knights in the kingdom. The nextn lines each contain an integer, and give the diameters of the dragon's heads, in centimetres. The followingmlines each contain an integer, and specify the heights of the knights of Loowater, also in centimetres.

The last test case is followed by a line containing:

0 0

Output Specification:

For each test case, output a line containing the minimum number of gold coins that the king needs to pay to slay the dragon. If it is not possible for the knights of Loowater to slay the dragon, output the line:

Loowater is doomed!

Sample Input:

2 3547842 155100 0

Output for Sample Input:

11Loowater is doomed!


排序后贪心的思路

代码 设置2个指标前进。。

#include<iostream>#include<algorithm>using namespace std;int main(){int a,b;while(cin>>a>>b){if(a==0&&b==0) break;int ans=0;int *x=new int[a];int *y=new int[b];for(int i=0;i<a;i++)cin>>x[i];for(int j=0;j<b;j++)cin>>y[j];sort(x,x+a);sort(y,y+b);int pos=0,k;for(k=0;k<a;k++,pos++){if(pos==b) break;if(y[pos]<x[k]) k--;else ans+=y[pos];}if(k==a) cout<<ans<<endl;else cout<<"Loowater is doomed!"<<endl;}return 0;}


C题

11520 - Fill the Square

Time limit: 1.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=456&page=show_problem&problem=2515

In this problem, you have to draw a square using uppercase English Alphabets.

To be more precise, you will be given a square grid with some empty blocks and others already filled for you with some letters to make your task easier. You have to insert characters in every empty cell so that the whole grid is filled with alphabets. In doing so you have to meet the following rules:

 

  1. Make sure no adjacent cells contain the same letter; two cells are adjacent if they share a common edge.  
  2. There could be many ways to fill the grid. You have to ensure you make the lexicographically smallest one. Here, two grids are checked in row major order when comparing lexicographically.

 

Input
The first line of input will contain an integer that will determine the number of test cases. Each case starts with an integer n( n<=10 ), that represents the dimension of the grid. The next n lines will contain n characters each. Every cell of the grid is either a ‘.’ or a letter from [A, Z]. Here a ‘.’ Represents an empty cell.

 

OutputFor each case, first output Case #: ( # replaced by case number ) and in the next n lines output the input matrix with the empty cells filled heeding the rules above.
 
Sample Input                       Output for Sample Input  

2

3

...

...

...

3

...

A..

... 

Case 1:

ABA

BAB

ABA

Case 2:

BAB

ABA

BAB


思路:穷举法 每个试探

注意点是如果已经给出来的点,结果一定含有那个点。。。调试好。。


#include<iostream>#include<cstring>using namespace std;char mat[12][12];char ans[12][12];bool check(int i,int j,char t){if(mat[i-1][j]!=t&&mat[i+1][j]!=t&&mat[i][j-1]!=t&&mat[i][j+1]!=t&&   ans[i-1][j]!=t&&ans[i+1][j]!=t&&ans[i][j-1]!=t&&ans[i][j+1]!=t)return true;return false;}int main(){int T;cin>>T;int cnt=1;while(T--){int n,i,j;cin>>n;memset(ans,' ',sizeof(ans));memset(mat,' ',sizeof(mat));for(i=0;i<n;i++)for(j=0;j<n;j++)cin>>mat[i][j];for(i=0;i<n;i++){for(j=0;j<n;j++){if(mat[i][j]!='.') {ans[i][j]=mat[i][j];continue;}for(char t='A';t<='Z';t++){if(check(i,j,t)){ans[i][j]=t;break;}}}}cout<<"Case "<<cnt<<":"<<endl;cnt++;for(i=0;i<n;i++){for(j=0;j<n;j++){cout<<ans[i][j];}cout<<endl;}}return 0;}


0 0