Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- //O(n^2)
- bool checkOneOne(const vector<int> &a){
- bool check = true;
- for(size_t i=0;i<a.size()-1;i++){
- for(size_t j=i+1;j<a.size();j++){
- if(a[i]==a[j]) {
- check=false;
- break;
- }
- }
- }
- return check;
- }
- //O(n)
- bool checkOneOne2(const vector<int> &a){
- vector<int> b;
- b.resize(a.size());
- bool check = true;
- for(size_t i=0;i<a.size()-1;i++){
- if(b[a[i]]==0){
- b[a[i]]++;
- }
- else {
- check=false;
- }
- }
- return check;
- }
- int main() {
- vector<int> v;
- int n;
- cin >> n;
- v.resize(n);
- for (int i = 0;i < n;i++) {
- cin >> v[i]; // guarantee 0 ... n-1
- }
- //print "YES" only when v[i] is 1-1 and onto on set {0..n-1}
- //print "NO" otherwise
- cout<<(checkOneOne2(v)?"YES" : "NO")<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement