HDU - 1735 Word Count

HDU - 1735 Word Count

Topic . There are multiple sets of
Input
  test data. The first line of each set of test data has three integers: N (the number of lines in the composition 1 ≤ N ≤ 10000), L (the number of grids in each line of the composition paper 10 ≤ L ≤ 100), M (the number of paragraphs in the original text 1 ≤ M ≤ 20), separated by spaces.
  Next is a N × L bit matrix (Aij) (two adjacent numbers are separated by spaces), representing the composition after being destroyed. Among them, when Aij is 0, it means that there is no text in row i and column j (or it cannot be seen clearly), and when it is 1, it means that there is text. You can assume that each row has at least one 1 and that all data is valid.
  
Output Outputs
  one line for each set of tests, an integer indicating at least how many texts are corrupted.
Sample Input

10 10 3
0 0 0 1 1 1 0 1 1 0
1 1 0 0 0 1 1 1 0 0
0 0 1 1 0 0 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 0 1 0 1 1 1 0 0 0
1 1 0 0 1 1 1 1 1 1
1 1 1 1 1 1 1 0 0 0
0 0 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
0 0 0 0 1 1 1 1 1 0

Sample Output

19

Idea (greedy) :

  1. First count all the numbers sum whose value is 0, which is the maximum value of the damaged text;
  2. Starting from the second line, judge whether the first two numbers in each line are 0, if so, it may be a paragraph of the article, set the array temp, the value of the array temp is 2 + the number of 0s in the previous line, the value is not Count the number of damaged characters;
  3. Arrange the values ​​of the array temp from large to small (from the question, we hope that the number of damaged text is the least), loop m-1 times, and use sum-the value of the array temp (there are m paragraphs, not counting the first paragraph);
  4. Finally sum-2 (two zeros at the beginning of the paragraph).

Note:
When counting the number of 0s in a row, count from the back to the front until the value is 1 and stop.

code show as below:

#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
int map[10001][101];
int temp[10001];
int cmp(int a,int b)
{
    
    
	return a>b;
}
int main()
{
    
    
	int n,l,m,i,j,sum,k;
	while(cin>>n>>l>>m)
	{
    
    //行,列,段落数
		memset(temp,0,sizeof(temp));//每组测试时初始化为0 
		sum=0;//0的总数,每组初始化为0 
		k=0;
		for(i=0;i<n;++i)
		{
    
    
			for(j=0;j<l;++j)
			{
    
    
				cin>>map[i][j];
				if(map[i][j]==0)
					sum++;
			}
		} 
		for(i=1;i<n;++i)
		{
    
    
			if(map[i][0]==0&&map[i][1]==0)
			{
    
    
				temp[k]=2;
				for(j=l-1;j>=0;--j)
				{
    
    
					if(map[i-1][j]==0)
						temp[k]++;
					else
					{
    
    
						k++;
						break;
					}		
				}
			}
		}
		sort(temp,temp+k,cmp);
		for(i=n-1,j=l-1;;--j)
		{
    
    
			if(map[i][j]==0)
				sum--;
			else
				break;
		}
		for(i=0;i<m-1;++i)
		{
    
    
			sum=sum-temp[i];
		}
		sum=sum-2;
		cout<<sum<<endl;
	}
	return 0;
 } 

Guess you like

Origin blog.csdn.net/fortune_mz/article/details/113604074