ZOJ 4025 King of Karaoke

题目:It’s Karaoke time! DreamGrid is performing the song Powder Snow in the game King of Karaoke. The song performed by DreamGrid can be considered as an integer sequence D1, D2,……,Dn, and the standard version of the song can be considered as another integer sequence S1,S2,……,Sn. The score is the number of integers i satisfying 1 ≤ i ≤ n and Si= Di. 
 

As a good tuner, DreamGrid can choose an integer (can be positive, 0, or negative) as his tune and add to every element in . Can you help him maximize his score by choosing a proper tune?Input:There are multiple test cases. The first line of the input contains an integer T (about 100), indicating the number of test cases. For each test case: 
 

The first line contains one integer n (1 ≤ n ≤ 105), indicating the length of the sequences D and S.

The second line contains n integers D1,D2,……,Dn (-105 ≤ Di ≤ 105), indicating the song performed by DreamGrid.

The third line contains n integers S1,S2,……,Sn (-105 ≤ Si ≤ 105), indicating the standard version of the song.

It’s guaranteed that at most 5 test cases have n > 100.Output:For each test case output one line containing one integer, indicating the maximum possible score.Sample Input:241 2 3 42 3 4 65-5 -4 -3 -2 -15 4 3 2 1Sample Output:3 
 
 
 
 
 
 
 
 
 
 
 
 

1

It is to look at the difference between the two groups of numbers and want to reduce it, and output the maximum number of times the same number appears. It may be negative. If there is a negative value, it can be turned into a positive value.

see code

# include <stdio.h>
# include <string.h>

int main(void)
{
	int t, n, a[100001], b[100001], c[300010];
	int i, j, min, m, sum, q, w, k;
	scanf("%d", &t);
	while (t --)
	{
		min = -1;
		memset(c, 0, sizeof(c)); //记得初始化
		scanf("%d", &n);
		for (i = 1; i <= n; i ++)
			scanf("%d", &a[i]);
		for (i = 1; i <= n; i ++)
		{
			scanf("%d", &b[i]);
			if (b[i]-a[i] < 0)
				c[-(b[i]-a[i]-100000)] ++;// 小于0的转化为1000001  比如-1为1000001 -2为10000002
			else 
			c[b[i]-a[i]] ++ ;
		}
		for (i = 0; i < 200100; i ++)
		{
			if (c[i] > min)
				min = c[i];//遍历一遍即可
		}
		
		printf("%d\n", min);
	}
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325760249&siteId=291194637
ZOJ
ZOJ