Translate

Views

Tuesday, July 12, 2022

Solution Kattis - A Rank Problem

Submission 9130194

IDDATEPROBLEMSTATUSCPULANGTEST CASES
913019417:51:53A Rank Problem
Accepted
0.00 sC++
27/27

Solution 1:  Use STL Vector C++

Files submitted

rankproblem.cpp

#include<bits/stdc++.h>
#define all a.begin(),a.end()
using namespace std;
int n, m;
vector<string> a;
string win, lose;
int main(){
cin >> n >> m;
for(int i=1; i<=n; i++) a.push_back("T"+to_string(i));
while(m--){
cin >> win >> lose;
if (find(all, win) < find(all, lose)) continue;
a.erase(find(all, lose));
a.insert(find(all, win) + 1, lose);
}
for(auto x:a) cout<<x<<" ";
}


Submission 9129968

IDDATEPROBLEMSTATUSCPULANGTEST CASES
912996816:40:02A Rank Problem
Accepted
0.02 sC++
27/27

Solution 2: Use array c++

Files submitted

rankproblem.cpp

#include<bits/stdc++.h>
using namespace std;
const int N= 1000111;
string a[N], win, lose;
int n, m;
bool NotChange(){
int posWin, posLose;
for(int i=1; i<=n; i++){
if (a[i]==win) posWin= i;
if (a[i]==lose) posLose= i;
}
if (posWin < posLose) return true;
return false;
}
int main(){
cin >> n >> m;
for(int i=1; i<=n; i++) a[i]= "T"+to_string(i);
while(m--){
cin >> win >> lose;
if (NotChange()) continue;
//delete element in array
for(int i=1; i<=n; i++)
if (a[i] == lose) for(int j=i; j<n; j++) a[j]= a[j+1];
//insert element in array
for(int i=1; i<=n; i++)
if (a[i] == win){
for(int j=n; j>i; j--) a[j+1]= a[j];
a[i+1]= lose;
}
}
for(int i=1; i<=n; i++) cout<<a[i]<<" ";
}


No comments: