Advertisement
aiThanet

CheckOneToOne

Mar 22nd, 2016
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. //O(n^2)
  8. bool checkOneOne(const vector<int> &a){
  9. bool check = true;
  10. for(size_t i=0;i<a.size()-1;i++){
  11.     for(size_t j=i+1;j<a.size();j++){
  12.           if(a[i]==a[j]) {
  13.                 check=false;
  14.                 break;
  15.           }
  16.     }
  17. }
  18. return check;
  19. }
  20.  
  21. //O(n)
  22. bool checkOneOne2(const vector<int> &a){
  23. vector<int> b;
  24. b.resize(a.size());
  25. bool check = true;
  26. for(size_t i=0;i<a.size()-1;i++){
  27.     if(b[a[i]]==0){
  28.         b[a[i]]++;
  29.     }
  30.     else {
  31.         check=false;
  32.     }
  33. }
  34. return check;
  35. }
  36.  
  37. int main() {
  38.   vector<int> v;
  39.   int n;
  40.   cin >> n;
  41.   v.resize(n);
  42.   for (int i = 0;i < n;i++) {
  43.     cin >> v[i]; // guarantee 0 ... n-1
  44.   }
  45.  
  46.   //print "YES" only when v[i] is 1-1 and onto on set {0..n-1}
  47.   //print "NO" otherwise
  48. cout<<(checkOneOne2(v)?"YES" : "NO")<<endl;
  49.  
  50.  
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement