1381. deleted (Standard IO)

Subject description:

Alice on chemical class and distracted, he drew a first 3 rows and N columns of the table, then the first row of numbers 1 to N fill the table to ensure that each number appears only once , while he also filled two rows numbers 1 to N, but not limit the number of occurrence of each number. Alice now want to delete several columns so that each row exactly the same sequence drained after programming to calculate the minimum required number of columns deleted.

Input: The first line contains an integer N ( . 1 <= N <= 100000 ), the number of columns of the table. The next three lines each line contains N integers, each a number between 1 to N, the number of the first row and different from each other.

Output: Output the minimum number of columns you want to delete.


[Resolved] ideas

  First, we note red part 3 rows and N columns, which has only three lines, so we can directly process the input manually: lin1 / 2/3 [i] represents the number of i-th column of the row 1/2/3, sum2 / 3 [] represents the number of times each of the second and third lines (1-n) is emerging, then we can cycle to determine: If the following two lines can have some number of occurrences is 0, then the number corresponding to the first row must be deleted, it is impossible to exactly the same, when you delete this column may also cause the number of additional number of the following two lines appear becomes zero, we can write out a do -while cycle, when all the columns are deleted after completion or two lines of the same elements and the first line, the end of the cycle and outputs the answer. In a while, we can write a for loop, the number two lines below the first line of the column if the column has been deleted or has (not need to delete) from 1-n (i enumerator to the first column), then jump too, otherwise remove this column: the answer plus one, the number of columns of the two lines occurs behind the number -1, and then make the column number of the first row as 0 (as a determination condition for the next can directly determine whether the column has been dropped) and the value of the loop bool variable is set to true, the operating instructions in this cycle, the cycle continues (note, be sure to head to the value of the while loop bool variable reduction of 0, otherwise it will fall into an infinite loop). Finally exit the loop, the output answer.


Code:

#include<iostream>
#include<cstdio>
using namespace std;
int lin1[100001]={0},lin2[100001]={0},lin3[100001]={0};
int num2[100001]={0},num3[100001]={0};
int n,dele=0,ans=0;
int main(){
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=1;i<=3;i++){
        for(int j=1;j<=n;j++){
            if(i==1) cin>>lin1[j];
            else if(i==2) cin>>lin2[j],num2[lin2[j]]++;
            else if(i==3) cin>>lin3[j],num3[lin3[j]]++;
        }
    }
    do{
        dele=0;
        for(int i=1;i<=n;i++){
            if(lin1[i]&&((!num2[lin1[i]])||(!num3[lin1[i]]))){
                ans++;
                lin1[i]=0;
                num2[lin2[i]]--;
                num3[lin3[i]]--;
                dele=1;
            }
        }
    }while(dele);
    cout<<ans;
}

 


 

Summary: The title is a flood problem, no knowledge, that is, the ability to achieve point test code, it looks like, after all, I am too weak.

Guess you like

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