Submission #473127

# Submission time Handle Problem Language Result Execution time Memory
473127 2021-09-15T08:30:14 Z MamdouhN ZigZag (COCI17_zigzag) C++17
48 / 80
2000 ms 13728 KB
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"

vector<string>words;
map<string,int>done;

bool comp(string a,string b)
{
    if(a[0]!=b[0])return a<b;
    if(done[a]==done[b])return a<b;
    return done[a]<done[b];
}

string bs(char c)
{
    int st=0,en=words.size();
    int mid;
    while(st<en)
    {
        mid=(st+en)/2;
        if(words[mid][0]>c)en = mid-1;
        if(words[mid][0]<c)st = mid+1;
        if(words[mid][0]==c)en =mid;
    }
    mid=(st+en)/2;
    done[words[mid]]++;
    return words[mid];
}

main()
{
    int n,q;
    cin>>n>>q;
    for(int i=0;i<n;i++)
    {
        string x;
        cin>>x;
        words.push_back(x);
    }
    while(q--)
    {
        sort(words.begin(),words.end(),comp);
        char c;
        cin>>c;
        cout<<bs(c)<<endl;;
    }

}

Compilation message

zigzag.cpp:32:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   32 | main()
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 2 ms 204 KB Output is correct
3 Correct 147 ms 332 KB Output is correct
4 Correct 222 ms 344 KB Output is correct
5 Correct 280 ms 348 KB Output is correct
6 Correct 309 ms 340 KB Output is correct
7 Execution timed out 2075 ms 13576 KB Time limit exceeded
8 Execution timed out 2082 ms 13584 KB Time limit exceeded
9 Execution timed out 2083 ms 13728 KB Time limit exceeded
10 Execution timed out 2069 ms 13684 KB Time limit exceeded