Tickets Gym - 101911F

Last nn days Monocarp used public transport to get to the university. He received a ticket with number titi during the ii-th day.

Tickets’ numbers are six digit non-negative integers with possible leading zeroes. For example, 123456123456, 000000000000, 099999099999, 999999999999 are correct ticket numbers, but 12345671234567, 1234512345, 99 are not. Every day tickets are numbered from zero. The first passenger gets ticket number 000000000000, the second one — ticket number 000001000001, the third one — number 000002000002 and so on every day. Assume that each day the number of passengers doesn’t exceed 106106.

Unluckiness of the ticket is equal to absolute difference between the sum of the first three digits and the sum of the last three. For example, unluckiness of the ticket number 345123345123 is equal to |(3+4+5)−(1+2+3)|=6|(3+4+5)−(1+2+3)|=6, or unluckiness of the ticket number 238526238526 is equal to |(2+3+8)−(5+2+6)|=0|(2+3+8)−(5+2+6)|=0.

One passenger is luckier than other if unluckiness of first passenger’s ticket is strictly less than unluckiness of the second one’s ticket.

For each of nn days for given Monocarp’s ticket’s number titi calculate the number of passengers who received their tickets before him during this day and are luckier than Monocarp.

Examine examples for the further understanding of the statement.

Input
The first line contains one integer nn (1≤n≤2⋅105)(1≤n≤2⋅105) — the number of days during which Monocarp used public transport.

Each of the next nn lines contains one six digit integer titi (0≤ti<1060≤ti<106, titi can have leading zeroes) — the ticket Monocarp received during the corresponding day.

Output
Print nn lines: one integer per line — the number of passengers who received their tickets before Monocarp during the corresponding day and are luckier than Monocarp.

Example
Input
5
001000
000000
999000
453234
654331
Output
1
0
998999
121496
470362
Note
During the first day the only one passenger who got ticket before Monocarp was luckier. This passenger got ticket number 000000000000.

During the second day Monocarp was the first one, so there was nobody before him.

During the third day all passengers except one who got tickets before Monocarp were more luckier than him. The one whose unluckiness was equal to Monocarp’s unluckiness got ticket number 000999000999.

题意:给你一个6位数x,x的前三位之和与后三位之和的绝对值之差记为X。求6位数y,y的前三位之和与后三位之和的绝对值之差记为Y。当x>y并且X>Y时满足条件的Y的个数。

思路:提前预处理,预处理出每个数的结果,然后O(1)复杂度的查询。

#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int f[30];
int a[1000000];
int main()
{
	memset(f,0,sizeof(f));
	for(int i=0;i<1000000;i++)
	{
		a[i]=0;
		int x=0,y=0,tmp;
		x+=i/100000;
		x+=(i/10000)%10;
		x+=(i/1000)%10;
		y+=(i/100)%10;
		y+=(i/10)%10;
		y+=i%10;
		tmp=abs(x-y);
		f[tmp]++;
		for(int j=0;j<tmp;j++)
			a[i]+=f[j];	
	}	
	int t,n;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		printf("%d\n",a[n]);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/why932346109/article/details/85109386