Diberikan dua larik A[] dan B[] (B[i] < A[i]) masing-masing berukuran N dan sebuah bilangan bulat K, tugasnya adalah melakukan operasi dari (K – A[i] + B[i]
Catatan. Suatu operasi tidak akan dilakukan sedemikian rupa sehingga K menjadi negatif i. e. operasi tidak dapat dilakukan jika K – A[i] negatif dan sembarang indeks dapat dipilih sebanyak mungkin
Contoh
Memasukkan. N = 3, K = 3, A[] = {2, 3, 4}, B[] = {1, 1, 1}
Keluaran. 2
Penjelasan. Untuk belokan maksimal,
Pilih A[0] dan B[0], jadi K = 3 – 2 + 1 = 2
Pilih A[0] dan B[0], jadi K = 2 – 2 + 1 = 1
Tidak ada operasi lain yang memungkinkan. Jadi putaran maksimal = 2Memasukkan. N = 2, K = 10, A[] = {5, 4} B[] ={1, 2}
Keluaran. 4
Mendekati. Masalahnya dapat diselesaikan berdasarkan ide berikut
Untuk melakukan operasi dalam jumlah maksimum, selalu pilih indeks (katakanlah i) sehingga A[i] – B[i] minimum
Katakanlah nilai tersebut adalah A[i] dan B[i], sehingga operasi maksimum yang dilakukan untuk indeks tersebut adalah (K – B[i]) / (A[i] – B[i]) [Di Sini K – B[i
Ikuti langkah-langkah yang disebutkan di bawah ini untuk menerapkan ide tersebut
- Buat array (katakanlah v) untuk menyimpan nilai dalam bentuk (A[i] – B[i], A[i])
- Urutkan array dalam urutan meningkat berdasarkan (A[i] – B[i])
- Loop dari i = 0 ke N-1
- Jika K – nilai kedua dari v[i] tidak negatif maka gunakan rumus di atas untuk mencari kontribusinya (misalkan x)
- Tingkatkan hitungan dengan x
- Kembalikan hitungan terakhir sebagai jawabannya
Di bawah ini adalah implementasi dari pendekatan di atas
C++
// C++ code to implement the approach
#include <bits/stdc++.h>_
using namespace std;
// Function to calculate the maximum operations
int ________7__6_______ // C++ code to implement the approach0int // C++ code to implement the approach2int // C++ code to implement the approach4int // C++ code to implement the approach6
// C++ code to implement the approach_7
// C++ code to implement the approach8// C++ code to implement the approach9
// C++ code to implement the approach8#include <bits/stdc++.h>1int#include <bits/stdc++.h>3int#include <bits/stdc++.h>5
// C++ code to implement the approach8#include <bits/stdc++.h>7 #include <bits/stdc++.h>8int using0
using1using2
// C++ code to implement the approach8using4
// C++ code to implement the approach8using6
// C++ code to implement the approach8int using9
// C++ code to implement the approach8#include <bits/stdc++.h>7 #include <bits/stdc++.h>8int namespace4
using1namespace6
using1namespace8 namespace9
std;0std;1std;2
using1std;4
using1std;6
using1std;8
using1int // Function to calculate the maximum operations1
std;0// Function to calculate the maximum operations3
using1// Function to calculate the maximum operations5
using1// Function to calculate the maximum operations7
using1// Function to calculate the maximum operations9
// C++ code to implement the approach8int1
// C++ code to implement the approach8int3 int4
int_1
int_6
int int8
// C++ code to implement the approach_7
// C++ code to implement the approach8int MakeitMin(2
// C++ code to implement the approach8int MakeitMin(5
// C++ code to implement the approach8int MakeitMin(8
// C++ code to implement the approach8int0
// C++ code to implement the approach8int3 int3
int_1
Jawa
int_5
int6 int7
int6 int9
// C++ code to implement the approach00 // C++ code to implement the approach01
// C++ code to implement the approach8// Function to calculate the maximum operations
// C++ code to implement the approach8// C++ code to implement the approach05 // C++ code to implement the approach06 int MakeitMin(int // C++ code to implement the approach0int // C++ code to implement the approach2intusing83
Bagaimana cara mengurangi elemen dari array di PHP?
Bagaimana Anda mengurangi dua nilai dalam array?
Bagaimana cara mengurangi dua nilai dalam PHP?
.
.