1433. Digital problem solution problem (Standard IO)

Description

  Alice has a grid of N * N, the 1-N ^ 2 in the order from left to right from top to bottom filled into the table, allows two operations on the form:
  (1) rotating the line - this line the number of move one position, and the number of the last column to the first column will;
  (2) rotating column - the number in this row is moved downward one position, the number of the last row to the first row will.
  Number Alice wants to move X (R, C) of the method may be employed:
  • if X is not in the C column, operating by the rotation of the X row move the C column;
  • if X is not in the line R, by rotating operation of the column the X moved to the R line.
  The following is an example of 6 to move (3, 4):
  
  Alice now like to the above method, the order number K to move to respective target positions, the number is calculated for each programming requires several operations.
 

Input

  The first line contains two integers N (12 <= N <= 10000) and K (1 <= K <= 1000).
  Next K rows, each row containing three integers X (1 <= X <= N ^ 2), R and C (1 <= R, C <= N), is described to be moved, and the destination number.
  Alice must be moved sequentially input order.

Output

  K output lines, each output an integer representing the number of operations.
 

Sample Input

Input 1: 
. 4 1 
. 6 3. 4 

Input 2: 
. 4 2 
. 6. 4 3 
. 6 2 2 

Input 3: 
. 5 3 
1 2 2 
2 2 2 
12 is. 5. 5

Sample Output

Output 1: 
3 

Output 2: 
3 
. 5 

Output 3: 
2 
. 5 
3

  The question we look at the data range, N <= 10000, we know, is not to take a two-dimensional array to record each point, and the time complexity can not depend on N, so we can only K start, for each inquiry, the answer is actually very good count, but more difficult is calculated after each answer, the impact on the operation of the initial number of other locations, we can set two structural array x [i] and y [ i], respectively, move, now two elements. Indicates the i-th asked how many were moved in the x and y-axis direction, x [i] .now showing the operation this time, what line of movement x (because the movement of the line, so use to indicate which row y , because only the same y, x is not the same), move to the right illustrate a number, y [i] .now showing the operation of the mobile column which is (again, on a number, x same, y is not the same, the denoted x), move represents a mobile number, because the provisions on the subject Xianxiang right, down again, so we first calculate the time line operation, the computed column.

  We each new operation will be saved during previous operation which row move which column number, one by one, out of this number in order to traverse the current real position, making calculations.

  For a very simple calculation, two cases, 1 to be larger than the current coordinates reach the coordinates (x coordinate, y coordinate for both), then directly obtained by subtracting the difference operation is to be performed many times, are known xy meet the requirements, you can output the answer. 2- about to reach than it is now small coordinate coordinate our operations can only go down backwards, not looking back, only after reaching the border back to the first division, converted to Question 1.

Code:

#include<iostream>
#include<cstdio>
using namespace std;
struct number{
    long long move;
    long long now;
}x[1010],y[1010];
long long n,k,x2,y2;
long long num[1010],x1[1010],y1[1010],ans;
int main(){
    ios::sync_with_stdio(false);
    cin>>n>>k;
    for(int i=1;i<=k;i++) cin>>num[i]>>y1[i]>>x1[i];
    for(int i=1;i<=k;i++){
        ans=0;
        x2=num[i]%n;
        if(num[i]%n==0) x2=n;
        y2=num[i]/n;
        if(num[i]%n!=0) y2++;
        for(int j=1;j<i;j++){            
            if(x[j].now==y2){ 
                x2=(x[j].move+x2)%n;
                if(x2==0) x2=n;
            }
            if(y[j].now==x2){
                y2=(y[j].move+y2)%n;
                if(y2==0) y2=n;
            }
        }
        if(x1[i]<x2){
            ans+=n-x2+x1[i];
            x[i].move=n-x2+x1[i];
            x[i].now=y2;
        }
        else if(x1[i]>x2){
            ans+=x1[i]-x2;
            x[i].move=x1[i]-x2;
            x[i].now=y2;
        }        
        if(y1[i]<y2){
            ans+=n-y2+y1[i];
            y[i].move=n-y2+y1[i];
            y[i].now=(x2+x[i].move)%n;
            if(y[i].now==0) y[i].now=n;
        }
        else if(y1[i]>y2){
            ans+=y1[i]-y2;
            y[i].move=y1[i]-y2;
            y[i].now=(x2+x[i].move)%n;
            if(y[i].now==0) y[i].now=n;
        }
        cout<<ans<<endl;
    }
}

 

  

thanks for reading.

 

Guess you like

Origin www.cnblogs.com/tianbowen/p/11305317.html