From df972774f06907bc35a6b9dd7f1b363b6416922d Mon Sep 17 00:00:00 2001 From: Sazid Hasan <108776549+iamsazidh@users.noreply.github.com> Date: Fri, 18 Oct 2024 16:46:54 +0600 Subject: [PATCH 1/4] Add files via upload --- en.md | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 en.md diff --git a/en.md b/en.md new file mode 100644 index 00000000..88b1bd01 --- /dev/null +++ b/en.md @@ -0,0 +1,75 @@ + +# LOJ-1261: K-SAT Problem + +#### Problem breakdown +In this problem, there are n numbers of people. Now, each person has k wishes. In one wish, they either want us to take a number or to ignore a number. If we grant one of his wishes, he will be happy. + +The numbers we are taking are already given to you. You have to just verify if it is possible to make everyone happy with those numbers. + +#### How it works? +**input**: First we have to take all the inputs for each testcase (t). +In each test case, there are given 3 numbers (n, m, k) in the first line. n denotes the number of people, after that m (I don't think we need m), and k denotes how many wishes a person has. After that, there are given n lines, and each has exactly k non-zero integers. I am using a 2D array to store it. Here, if an integer is positive, that means the person wants us to take that number, and if it's negative, he wants us to avoid that. After n lines, there is an integer p. After that, we have p integers, which we are taking. + +**Let's do it:** After talking about all the inputs, we will use a loop (index-based), which will iterate over each line. After that, we are having another loop (index j-based) for checking if we are granting JTH. As we are granting p numbers, we will use another loop for iterating over each number we are granting. + +Now, + +- We will use some bool variable for identifying the state. At first we will have a bool for the main result initialized as true. +- In the i-based loop we will have a bool with a default value of true to check if we are granting ith person's atleast one wish. (If we are not granting any of them, then simply we will make it false. And also we will break the loop because the entire result is false as we have to make everyone happy.) +- In the j-based loop we are going to declear two bools. one for checking if we took that the absolute value of that jth number and another for checking if we don't. +- After that, if it seems we took that absolute value, then we will check if that was bigger than 0. If yes, that means we have granted one of his wishes. On the other hand, if we didn't grant, we will check if it was less than 0. If yes, that means we granted that wish. +- If we have granted it, then we can say that this person is happy. And we can go to the next person. +- If it seems that this person is not happy, then we should make the main bool false and break all the loops. +- Print the result based on the main bool. + +### C++ +----- +```cpp +#include +#include //Because of abs() function +using namespace std; + +int main(){ + int t; + cin >> t; + for(int cs = 1; cs <= t; cs++){ + int n, m, k, p; cin >> n >> m >> k; + int wishes[n][k]; + bool isPossible = true; + for(int i = 0; i < n; i++){ + for(int j = 0; j < k; j++){ + cin >> wishes[i][j]; + } + } + cin >> p; + int granted[p]; + for(int a = 0; a < p; a++) cin >> granted[a]; + //Inut Completed for current testcase------------- + + for(int i = 0; i < n; i++){ + bool isSatisfield = false; + for(int j = 0; j < k; j++){ + bool taken = false; + bool notTaken = true; + for(int a = 0; a < p; a++){ + if(granted[a] == abs(wishes[i][j])){ + taken = true; + notTaken = false; + } + } + if(wishes[i][j] < 0 && notTaken) isSatisfield = true; + else if(wishes[i][j] > 0 && taken) isSatisfield = true; + + } + if(isSatisfield == false){ + isPossible = false; + break; + } + } + + if(isPossible) cout << "Case " << cs << ": Yes" << endl; + else cout << "Case " << cs << ": No" << endl; + } + return 0; +} +``` \ No newline at end of file From 15821b5dd0437e52a512a70f561e28b9b31dd0a2 Mon Sep 17 00:00:00 2001 From: Sazid Hasan <108776549+iamsazidh@users.noreply.github.com> Date: Fri, 18 Oct 2024 17:04:35 +0600 Subject: [PATCH 2/4] Create en.md Added Tutorial for LOJ-1261 : K-SAT Problem --- 1261/en.md | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 1261/en.md diff --git a/1261/en.md b/1261/en.md new file mode 100644 index 00000000..2b9bfad4 --- /dev/null +++ b/1261/en.md @@ -0,0 +1,75 @@ + +# LOJ-1261: K-SAT Problem + +#### Problem breakdown +In this problem, there are n numbers of people. Now, each person has k wishes. In one wish, they either want us to take a number or to ignore a number. If we grant one of his wishes, he will be happy. + +The numbers we are taking are already given to you. You have to just verify if it is possible to make everyone happy with those numbers. + +#### How it works? +**input**: First we have to take all the inputs for each testcase (t). +In each test case, there are given 3 numbers (n, m, k) in the first line. n denotes the number of people, after that m (I don't think we need m), and k denotes how many wishes a person has. After that, there are given n lines, and each has exactly k non-zero integers. I am using a 2D array to store it. Here, if an integer is positive, that means the person wants us to take that number, and if it's negative, he wants us to avoid that. After n lines, there is an integer p. After that, we have p integers, which we are taking. + +**Let's do it:** After talking about all the inputs, we will use a loop (index-based), which will iterate over each line. After that, we are having another loop (index j-based) for checking if we are granting JTH. As we are granting p numbers, we will use another loop for iterating over each number we are granting. + +Now, + +- We will use some bool variable for identifying the state. At first we will have a bool for the main result initialized as true. +- In the i-based loop we will have a bool with a default value of true to check if we are granting ith person's atleast one wish. (If we are not granting any of them, then simply we will make it false. And also we will break the loop because the entire result is false as we have to make everyone happy.) +- In the j-based loop we are going to declear two bools. one for checking if we took that the absolute value of that jth number and another for checking if we don't. +- After that, if it seems we took that absolute value, then we will check if that was bigger than 0. If yes, that means we have granted one of his wishes. On the other hand, if we didn't grant, we will check if it was less than 0. If yes, that means we granted that wish. +- If we have granted it, then we can say that this person is happy. And we can go to the next person. +- If it seems that this person is not happy, then we should make the main bool false and break all the loops. +- Print the result based on the main bool. + +### C++ +----- +```cpp +#include +#include //Because of abs() function +using namespace std; + +int main(){ + int t; + cin >> t; + for(int cs = 1; cs <= t; cs++){ + int n, m, k, p; cin >> n >> m >> k; + int wishes[n][k]; + bool isPossible = true; + for(int i = 0; i < n; i++){ + for(int j = 0; j < k; j++){ + cin >> wishes[i][j]; + } + } + cin >> p; + int granted[p]; + for(int a = 0; a < p; a++) cin >> granted[a]; + //Inut Completed for current testcase------------- + + for(int i = 0; i < n; i++){ + bool isSatisfield = false; + for(int j = 0; j < k; j++){ + bool taken = false; + bool notTaken = true; + for(int a = 0; a < p; a++){ + if(granted[a] == abs(wishes[i][j])){ + taken = true; + notTaken = false; + } + } + if(wishes[i][j] < 0 && notTaken) isSatisfield = true; + else if(wishes[i][j] > 0 && taken) isSatisfield = true; + + } + if(isSatisfield == false){ + isPossible = false; + break; + } + } + + if(isPossible) cout << "Case " << cs << ": Yes" << endl; + else cout << "Case " << cs << ": No" << endl; + } + return 0; +} +``` From 1e9e75555cc1cb21ab5a75f9ccdb847a71cf3033 Mon Sep 17 00:00:00 2001 From: Sazid Hasan <108776549+iamsazidh@users.noreply.github.com> Date: Fri, 18 Oct 2024 17:05:37 +0600 Subject: [PATCH 3/4] Delete en.md Deleting mistakenly created file --- en.md | 75 ----------------------------------------------------------- 1 file changed, 75 deletions(-) delete mode 100644 en.md diff --git a/en.md b/en.md deleted file mode 100644 index 88b1bd01..00000000 --- a/en.md +++ /dev/null @@ -1,75 +0,0 @@ - -# LOJ-1261: K-SAT Problem - -#### Problem breakdown -In this problem, there are n numbers of people. Now, each person has k wishes. In one wish, they either want us to take a number or to ignore a number. If we grant one of his wishes, he will be happy. - -The numbers we are taking are already given to you. You have to just verify if it is possible to make everyone happy with those numbers. - -#### How it works? -**input**: First we have to take all the inputs for each testcase (t). -In each test case, there are given 3 numbers (n, m, k) in the first line. n denotes the number of people, after that m (I don't think we need m), and k denotes how many wishes a person has. After that, there are given n lines, and each has exactly k non-zero integers. I am using a 2D array to store it. Here, if an integer is positive, that means the person wants us to take that number, and if it's negative, he wants us to avoid that. After n lines, there is an integer p. After that, we have p integers, which we are taking. - -**Let's do it:** After talking about all the inputs, we will use a loop (index-based), which will iterate over each line. After that, we are having another loop (index j-based) for checking if we are granting JTH. As we are granting p numbers, we will use another loop for iterating over each number we are granting. - -Now, - -- We will use some bool variable for identifying the state. At first we will have a bool for the main result initialized as true. -- In the i-based loop we will have a bool with a default value of true to check if we are granting ith person's atleast one wish. (If we are not granting any of them, then simply we will make it false. And also we will break the loop because the entire result is false as we have to make everyone happy.) -- In the j-based loop we are going to declear two bools. one for checking if we took that the absolute value of that jth number and another for checking if we don't. -- After that, if it seems we took that absolute value, then we will check if that was bigger than 0. If yes, that means we have granted one of his wishes. On the other hand, if we didn't grant, we will check if it was less than 0. If yes, that means we granted that wish. -- If we have granted it, then we can say that this person is happy. And we can go to the next person. -- If it seems that this person is not happy, then we should make the main bool false and break all the loops. -- Print the result based on the main bool. - -### C++ ------ -```cpp -#include -#include //Because of abs() function -using namespace std; - -int main(){ - int t; - cin >> t; - for(int cs = 1; cs <= t; cs++){ - int n, m, k, p; cin >> n >> m >> k; - int wishes[n][k]; - bool isPossible = true; - for(int i = 0; i < n; i++){ - for(int j = 0; j < k; j++){ - cin >> wishes[i][j]; - } - } - cin >> p; - int granted[p]; - for(int a = 0; a < p; a++) cin >> granted[a]; - //Inut Completed for current testcase------------- - - for(int i = 0; i < n; i++){ - bool isSatisfield = false; - for(int j = 0; j < k; j++){ - bool taken = false; - bool notTaken = true; - for(int a = 0; a < p; a++){ - if(granted[a] == abs(wishes[i][j])){ - taken = true; - notTaken = false; - } - } - if(wishes[i][j] < 0 && notTaken) isSatisfield = true; - else if(wishes[i][j] > 0 && taken) isSatisfield = true; - - } - if(isSatisfield == false){ - isPossible = false; - break; - } - } - - if(isPossible) cout << "Case " << cs << ": Yes" << endl; - else cout << "Case " << cs << ": No" << endl; - } - return 0; -} -``` \ No newline at end of file From 54827b33b4d83b060b01b6377f20af0f9540afb9 Mon Sep 17 00:00:00 2001 From: Sazid Hasan <108776549+iamsazidh@users.noreply.github.com> Date: Fri, 18 Oct 2024 17:08:35 +0600 Subject: [PATCH 4/4] Added tutorials for LOJ-1261 --- 1261/en.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/1261/en.md b/1261/en.md index 2b9bfad4..662b248f 100644 --- a/1261/en.md +++ b/1261/en.md @@ -73,3 +73,7 @@ int main(){ return 0; } ``` +Happy Coding... + + +Contributor : [Sazid Hasan]([https://github.com](https://www.linkedin.com/in/iamsazidh/))