Beijing Normal University Fifteenth ACM Finals - Race to reproduce K Keep In Line (String analog implementation)

Links: https://ac.nowcoder.com/acm/contest/3/K
Source: Cattle-off network

Keep In Line
time limit: C / C ++ 1 second, other languages 2 seconds
space limitations: C / C ++ 262144K, other languages 524288K
64bit the IO the Format:% LLD
subject description
and to the point of the meal, SK students against inertial come canteen, but long lines immediately so that he lost his appetite. Suddenly, he noticed that the team in front of a window to jump the queue evident phenomenon, so he quietly recorded the change in the students into the team and the team.
For into the team, SK students only know the ranks of more than one person, do not know the new people are honestly stood in the tail still plugged into the ranks of a location; for the team, SK students can determine the team standing in front of people out of the team.
Team initially empty, giving information out of the n ranks, to ensure that students will not have a team re-entry team, and the final team is also empty, SK students now want to know how many good students do not jump the queue.
Input Description:
The first line is a positive integer T (≤ 5), represents the number of test cases for each test, the first line is an integer n (1≤ n ≤ 100000), information indicating the team out of the number of the next n lines, each line is two strings Opt name, which Opt for the "in" representatives into the team, "out" on behalf of the team, name is into the team or the team's name, all the information in time the order given, the name of English letters and Arabic numerals, the length of not more than 10, to ensure that each person's name is different.
Description Output:
For each test, an output line, comprising an integer representing a number not jump the queue.
Example 1
Input
Copy
1
. 6
in quailty
in hwq1352249
hwq1352249 OUT
in zhuaiballl
OUT quailty
OUT zhuaiballl
output
copy
2

Meaning of the questions: ideas: first with map <string, int> string converted to numbers, then the array vis to indicate whether the number i have a team of people, and then use a variable Num maintain the current team of people look forward to the id, looking forward that is, if he's out of the team, is to meet not jump the queue, and in between, if not out looking for id, then that person is to jump the queue. And jump the queue has been marked as a team, num should be updated according vis array.


See details Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
string a[maxn/5];
map<string,int> m;
int vis[maxn/10+8];
int main()
{
    // freopen("D:\\code\\text\\input.txt","r",stdin);
    //freopen("D:\\code\\text\\output.txt","w",stdout);
//  1
// 6
// in quailty               1
// in hwq1352249            2
// out hwq1352249           1
// in zhuaiballl            3
// out quailty              2
// out zhuaiballl           3
    
    gbtb;
    int t;
    cin>>t;
    while(t--)
    {
        MS0(vis);
        int n;
        m.clear();
        cin>>n;
        string opt,name;
        int id=1;
        int fid=1;
        int ans=0;
        int num=1;
        repd(i,1,n)
        {
            cin>>opt>>name;
            if(opt[0]=='i')
            {
                m[name]=id++;
            }else
            {
                fid=m[name];
                if(num==fid)
                {
                    ans++;
                    num++;
                    vis[fid]=1;
                }else
                {
                    vis[fid]=1;
                }
                while(vis[num])
                {
                    num++;
                }
            }
        }
        cout<<ans<<endl;

    }
    
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}

Guess you like

Origin www.cnblogs.com/qieqiemin/p/11019269.html