[Group] NOIP2012 popular treasure hunt

This actual road difficult entry title're really a trouble ™ to death of me, because the computer can not touch, tune in the brain three days have turned not turn! !

P1076 treasure hunt

Thinking: Violence analog (which is the basis, all said to be simply violent TLE) + modulo optimization (such dips into the time complexity O (NM)).

AC Code:

#include<bits/stdc++.h>
using namespace std;
long long n,m,t,a[10005][105],able[10005],start,ans,p;
bool sign[10005][105];
int main()
{
    scanf("%lld%lld",&n,&m);
    for(long long i=1; i<=n; i++)
        for(long long j=0; j<m; j++)
        {
            scanf("%lld%lld",&t,&a[i][j]);
            sign[i][j]=t;
            if(sign[i][j])able[i]++;
        }
    scanf("%lld",&start);
    long long j=start;
    for(long long i=1; i<=n; i++)
    {
        ans+=a[i][start];
        p=a[i][start]%able[i];
        if(p==0)p=able[i];
        while(1)
        {
            if(sign[i][j])p--;
            if(!p)break;
            j++;
            if(j==m)j=0;
        }
        start=j;
    }
    ans=ans%20123;
    printf("%lld",ans);
    return 0;
}
Really not easy ™

Core Optimization:

p=a[i][start]%able[i];

A start indicates the start point of the floor of a room number, Able [] denotes a layer number, with the phrase optimize room stairs mod operator can be greatly reduced time complexity, for example:

There are three floors of a room with a staircase, but the room's wooden plaque start x = 10000, simulated violence need to take 10,000, but 10,000% 3 = 1, 1 walk on it.

In fact, the optimization is not the point

The key is to adjust the cycle for simulation!

the while ( . 1 ) 
        { 
            IF (Sign [I] [J]) the P--; // Sign [] [] is bool type, recording whether the room has a staircase 
            IF (P!) BREAK ; 
            J ++ ;
             IF (J = m =) J = 0 ; // analog annular corridor 
        }

Do not forget to open long long

AC code is the first submission took 20 minutes because the did not see the answer to 20123%

Guess you like

Origin www.cnblogs.com/yige2019/p/11273097.html