39F - Pacifist frogs - CodeForces Solution


implementation *1300

Please click on ads to support us..

Python Code:

def read_numbers():
    return [int(x) for x in input().split()]

def main():
    n, m, k = read_numbers()
    d = read_numbers()
    h = read_numbers()
    res = []
    min_count = 10 ** 9

    for frog in enumerate(d, start=1):
        curr_count = 0
        for hill in h:
            if hill % frog[1] == 0:
                curr_count += 1
        if curr_count < min_count:
            min_count = curr_count
            res = [frog[0]]
        elif curr_count == min_count:
            res.append(frog[0])
        
    print(len(res))
    print(' '.join([str(x) for x in res]))

if __name__ == '__main__':
    main()

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define sort(x) sort(all(x))
#define uniq(x) unique(all(x))
template<typename _Tp>
istream& operator >> (istream& i, vector<_Tp>& v){
  for(_Tp& x : v)
    i >> x;
  return i;
}
template<typename _Tp>
ostream& operator << (ostream& o, vector<_Tp>& v){
  for(_Tp& x : v)
    o << x << ' ';
  return o;
}
signed main(){
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int tt=1;
  #ifdef MULTITEST
  cin >> tt;
  #endif
  while(tt--){
    int n, m, k;
    cin >> n >> m >> k;
    vector<int> d(m), pos(k);
    cin >> d >> pos;
    
    int best=1001;
    vector<int> ans;
    for(int i=0; i<m; ++i){
      int di=d[i];

      int cnt=0;
      for(int j=0; j<k; ++j)
        if(pos[j]%di==0)
          ++cnt;

      if(cnt < best){
        best=cnt;
        ans.clear();
      }
      if(best == cnt)
        ans.emplace_back(i+1);
    }
    cout << (int)ans.size() << '\n' << ans;
    if(tt)  cout << endl;
  }
  return 0;
}


Comments

Submit
0 Comments
More Questions

402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins
752. Open the Lock
1535A - Fair Playoff
1538F - Interesting Function
1920. Build Array from Permutation
494. Target Sum
797. All Paths From Source to Target
1547B - Alphabetical Strings
1550A - Find The Array
118B - Present from Lena
27A - Next Test
785. Is Graph Bipartite
90. Subsets II
1560A - Dislike of Threes
36. Valid Sudoku
557. Reverse Words in a String III
566. Reshape the Matrix
167. Two Sum II - Input array is sorted
387. First Unique Character in a String
383. Ransom Note
242. Valid Anagram
141. Linked List Cycle
21. Merge Two Sorted Lists
203. Remove Linked List Elements