Submission #473136

# Submission time Handle Problem Language Result Execution time Memory
473136 2021-09-15T08:42:31 Z MamdouhN ZigZag (COCI17_zigzag) C++17
48 / 80
2000 ms 13648 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;
    int x = -1;
    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()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    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: In function 'std::string bs(char)':
zigzag.cpp:20:9: warning: unused variable 'x' [-Wunused-variable]
   20 |     int x = -1;
      |         ^
zigzag.cpp: At global scope:
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 142 ms 364 KB Output is correct
4 Correct 271 ms 376 KB Output is correct
5 Correct 278 ms 360 KB Output is correct
6 Correct 312 ms 368 KB Output is correct
7 Execution timed out 2084 ms 13524 KB Time limit exceeded
8 Execution timed out 2080 ms 13532 KB Time limit exceeded
9 Execution timed out 2092 ms 13648 KB Time limit exceeded
10 Execution timed out 2078 ms 13548 KB Time limit exceeded