Tickets(思维+预处理)

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.

题意:
求小于所给数并且前三个数和与后三个数和差的绝对值也少于所给数的个数

错因:
以前没怎么注意过打表这事,一直超时,谨记

百度百科—打表
预处理出所有答案的时间复杂度比依次读入再求更优,这才是这道题的正解。

超时代码:

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

int num(int i)
{
    int x=0, y=0;

    x+=i/100000;
    x+=(i/10000)%10;
    x+=(i/1000)%10;

    y+=(i/100)%10;
    y+=(i/10)%10;
    y+=i%10;

    if(x>y) return x-y;
    else return y-x;
}

int n, x, a[1000005], i, j, f[300], m;

int main()
{
    scanf("%d", &n);
    while(n--)
    {
        scanf("%d", &m);
        memset(f, 0, sizeof(f));
        for(i=0; i<=m; i++)    //每次都再求一遍,然而n很大,T了
        {
            a[i] = 0;
            x = num(i);
            f[x]++;
            for(j=0; j<x; j++)
            {
                a[i]+=f[j];
            }
        }
        printf("%d\n", a[m]);
    }
    return 0;
}

AC代码:

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

int num(int i)
{
    int x=0, y=0;

    x+=i/100000;
    x+=(i/10000)%10;
    x+=(i/1000)%10;

    y+=(i/100)%10;
    y+=(i/10)%10;
    y+=i%10;

    if(x>y) return x-y;
    else return y-x;
}

int n, x, a[1000005], i, j, f[300];

int main()
{
    memset(f, 0, sizeof(f));
    for(i=0; i<1000000; i++)
    {
        a[i] = 0;
        x = num(i);
        f[x]++;
        for(j=0; j<x; j++)
        {
            a[i]+=f[j];
        }
    }
    scanf("%d", &n);
    while(n--)
    {
        scanf("%d", &x);
        printf("%d\n", a[x]);
    }
    return 0;
}

最近一直出状况,以前没掉过的坑现在都要掉个遍了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章