For this problem I need to to do multiple queries so I used a for loop for it
#include<bits/stdc++.h>
using namespace std;
int main(){
vector<int>ans; //vector to store the ans
string s,e; //real dna and virus dna
cin>>s;
int q,start,match=0; // no. of queries, starting value of the query, var to store matching occurence
unsigned int step=0; //var to keep count of virus dna iteration
cin>>q;
for(int i=0;i<q;i++){//loop to iterate q times
cin>>start;
int l,r,x,c;
if(start==2){ // for 2nd type query
cin>>l>>r>>e;
for(int i=l-1;i<=r-1;i++){
if(s[i]==e[step]){
match+=1;
}
step+=1;
if(step== e.length()){
step=0; //starting again from start of virus
}
}
}
ans.push_back(match);
match=0; //reintializing value for next query
if(start==1){ //for 1st type query
cin>>x>>c;
s[x-1]=c; //replacing char at x-1 with c
}
}
for(int j=0;j<ans.size();j++){ //loop for ans output
cout<<ans[j]<<endl;
}
return 0;
}
but it terminates before it should for ex: for this input,
ATGCATGC
4
2 1 8 ATGC
2 2 6 TTT
1 4 T
2 2 6 TA
It would stop at 5th line and print 8 ,2 ,0, 0 whereas it should be 8, 2, 4. If I do individual queries without loop things work fine but any kind of loop doesn't work. Pls help. Also any suggestion for solving this kind of problems more efficiently will be very helpful to me.
The bigger problem in your code is that the variable c is defined as int
int l,r,x,c;
when receive chars and not integers (T, in your example) so should be defined as char
int l,r,x;
char c;
If c is int, when you send 4 T to
cin>>x>>c;
x receive 4, c doesn't receive T (T isn't a valid int) so start the next iteration of the external cycle, is called
cin>>start;
when T remain in the buffer; start is integer so there is the same problem and the program end.
And, as pointed by soon, ans.push_back() should be inside the first if. Now a value (zero) is added also with rows starting with 1.
Related
Problem
Finally, progress reached the Madoka family and she decided to play with her little sister in the sensational game Space Arrays.
The rules of the game are as follows:
Initially, a sequence a1,a2,…,aN is given.
The players alternate turns.
In each turn, the current player must choose an index i and increment ai, i.e. change ai to ai+1.
Afterwards, if there is no permutation p1,p2,…,pN of the integers 1
through N such that ai≤pi holds for each valid i, the current player
loses.
Otherwise, the game continues with the next turn.
Madoka is asking you to help her ― tell her if the first player (the player that plays in the first turn) or second player wins this game if both play optimally.
Input
The first line of the input contains a single integer T denoting the
number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer N.
The second line contains N space-separated integers a1,a2,…,aN.
Output
For each test case, print a single line containing the string "First" if the first player wins or "Second" if the second player wins (without quotes).
Constraints
1≤T≤2⋅10^4
1≤N≤2⋅10^5
The sum of N over all test cases doesn't exceed 2⋅10^5
1≤ai≤N for each valid i
Subtasks
Subtask #1 (100 points): Original constraints
Example Input
4
4
1 2 3 3
4
1 1 3 3
5
1 2 2 1 5
3
2 2 3
Example Output
First
Second
Second
Second
Explanation
Example case 1:
If the first player increases the fourth element, the resulting sequence is (1,2,3,4).The second player loses after increasing any of the elements.
If the first player increases the second element, the resulting
sequence is (1,3,3,3) ,and he loses because there is no valid
permutation. For example if p=(2,1,4,3), the second element of a is
greater than the second element of p.
Time Limit : 1 Secs
Source limit : 50000 byte
Here is my code
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
int main() {
// your code goes here
lli t;
cin >> t;
while(t--){
lli n;
cin >> n;
int arr;
int arrSum = 0;
for(lli i=0; i<n ;i++){
cin >> arr;
arrSum += arr;
}
lli turn = 0;
lli sum = (n*(n+1))/2;
turn = sum-arrSum;
if(turn < 0){
turn = 0;
}
if(turn%2 == 1){
cout << "First" << endl;
}
else{
cout << "Second" << endl;
}
}
return 0;
}
In your algorithm you don't check if first player initially loses without making a turn but the sum is lower than n*(n+1)/2, e.g. for input
1
5
1 1 2 5 5
The sum is 13.
5 * (5 + 1) / 2 - (1 + 1 + 2 + 5 + 5) == 1
But first player can't do anything and has lost before the first turn. Ssecond player wins.
Actual output from your code: First.
Expected output: Second
Another example is
1
5
1 1 4 4 4
with
5 * (5 + 1) / 2 - (1 + 1 + 4 + 4 + 4) == 1
In both cases your code returns that first player wins but actually second player wins without the game even starting.
The question asks for total time that will be required to type a string on a keyboard, which is represented as two dimensional matrix of characters, with one finger.
input:
2 31
YLrJpXOygVUl6MqBIRFWuAKsH7Gw4Z8
kE0tTQdP1CcxSjamizon9e5NfvDbh32
YE0
The first line contains n and m as input denoting dimensions of the keyboard matrix.
Next n lines contain m characters each denoting the character in the keyboard.
Next line will contain a string S
output:
3
Explanation:
The finger is initially at the first symbol of the keyboard so the time taken to press that key is 0. After that the new key is located at 1,1 so total time taken will be |1-0|+|1-0| i.e. 2 . Now the third key is located at position 1,2 so total time to move to that key will be |2-1|+|1-1| = 1 . So our answer is 3.
The string that's asked to print is in the last input line, YE0, consisting of letters from the above 2-D matrix.
The time calculation logic is:
If you are at cell (x1,y1) of the keyboard and now you want to press the key at (x2,y2) then the time taken will be |x1-x2| + |y1-y2|. You need to calculate the total time taken to type the complete string.
In case it is impossible to type the string you have to print -1.
#include<bits/stdc++.h>
using namespace std;
int main() {
int n,m;
cin>>n>>m;
unordered_map<char, pair<int, int>> map1;
for(int i=0;i<n;i++){
string s;
cin>>s;
for(int j=0;j<m;j++) {
map1.insert({s[j], make_pair(i,j)});
}
}
string key;
cin>>key;
long long total=0;
pair<int,int> sp;
for(int i=0;i<key.length();i++) {
if(map1.find(key[i])==map1.end()) {
total=-1;
break;
} else {
auto it = map1.find(key[i]);
if(i==0) sp=it->second;
pair<int,int> p = it->second;
total+=(abs(p.first-sp.first) + abs(p.second-sp.second));
sp=p;
}
}
cout<<total;
}
This solution is partially accepted and I am not able to figure out the edge cases for which its failing. Can somebody help me?
Here is one failing test case for free.
2 31
YLrJpXOygVUl6MqBIRFWuAKsH7Gw4Z8
kE0tTQdP1CcxSjamizon9e5NfvDbh32
YE 0
Should be -1 because the blank is not in the key matrix.
It fails because you only read in the "word" until first whitespace.
Here is another one:
2 31
YLrJpXOygVUl6MqBIRFW AKsH7Gw4Z8
kE0tTQdP1CcxSjamizon9e5NfvDbh32
Y E0
Shoudl not be -1, but is. Same problem, but with the matrix.
So what you need to do is change your input reading to include white space.
It's mentioned in the question that, "The finger is initially at the first symbol of the keyboard" so when you are parsing the key
In your code if(i==0) sp=it->second; should start from {0,0} to consider the movement from the first symbol of the keyboard to the first symbol of the key.
I was trying to solve the problem here- https://www.codechef.com/APRIL19B/problems/FENCE and initialized the array to 0 but when I try to access the value at arr[0][4] with n=4 and m=4, it prints a garbage value.
I tried to initialize using vector thinking I'm making some mistake in the initialization of array, it works for the sample testcase but still gives segmentation fault.
Here is my code -
#include<bits/stdc++.h>
#include <iostream>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t--){
long long n,m,k,res=0;
cin>>n>>m>>k;
//vector<vector<long long>> arr(n+2,vector<long long>(m+2,0));
long long arr[n+2][m+2]={0};
long long vec[k][k];
for(unsigned int it=0;it<k;it++){
int t1,t2;
cin>>t1>>t2;
arr[t1][t2]=1;
vec[it][0]=t1;
vec[it][1]=t2;
}
cout<<"values:"<<arr[1][4]<<endl;
for(unsigned int itr =0;itr<k;itr++){
int j = vec[itr][0];
int i = vec[itr][1];
//cout<<i<<" "<<j<<endl;
res+=4-(arr[i-1][j]+arr[i+1][j]+arr[i][j-1]+arr[i][j+1]);
}
cout<<res<<endl;
}
return 0;
}
Edit:
The sample input is:
Example Input
2
4 4 9
1 4
2 1
2 2
2 3
3 1
3 3
4 1
4 2
4 3
4 4 1
1 1
Example Output
20
4
The constraints:
1≤T≤10
1≤N,M≤10^9
1≤K≤10^5
1≤r≤N
1≤c≤M
the cells containing plants are pairwise distinct
I expect the output to be- 20 for the first testcase but get garbage value.
When declaring an array in C++, its size must be a constant expression - that is, the size must be known at compile time. Your compiler ought to be complaining about these lines because m, n, and k are uninitialized (more correctly, initialized to indeterminate values) at compile time:
long long arr[n+2][m+2]={0};
long long vec[k][k];
I think that array is getting out of bounds in arr[i-1][j] or arr[i+1][j] or arr[i][j+1] or arr[i][j-1] that why the error is coming.
The input consists of a set of tasks given in increasing order of start time, and each task has a certain duration associated.
The first line is number of tasks, for example
3
2 5
4 23
7 4
This means that there are 3 tasks. The first one starts at time 2, and ends at 7 (2+5). Second starts at 4, ends at 27. Third starts at 7, ends at 11.
We assume each task starts as soon as it is ready, and does not need to wait for a processor or anything else to free up.
This means we can keep track of number of active tasks:
Time #tasks
0 - 2 0
2 - 4 1
4 - 11 2
11 - 27 1
I need to find 2 numbers:
Max number of active tasks at any given time (2 in this case) and
Average number of active tasks over the entire duration computed here as :
[ 0*(2-0) + 1*(4-2) + 2*(11-4) + 1*(27-11) ] / 27
For this,
I have first found the time when all tasks have come to an end using the below code:
#include "stdio.h"
#include "stdlib.h"
typedef struct
{
long int start;
int dur;
} task;
int main()
{
long int num_tasks, endtime;
long int maxtime = 0;
scanf("%ld",&num_tasks);
task *t = new task[num_tasks];
for (int i=0;i<num_tasks;i++)
{
scanf("%ld %d",&t[i].start,&t[i].dur);
endtime = t[i].start + t[i].dur;
if (endtime > maxtime)
maxtime = endtime;
}
printf("%ld\n",maxtime);
}
Can this be done using Priority Queues implemented as heaps ?
Your question is rather broad, so I am going to just give you a teaser answer that will, hopefully, get you started, attempting to answer your first part of the question, with a not necessarily optimized solution.
In your toy input, you have:
2 5
4 23
7 4
thus you can compute and store in the array of structs that you have, the end time of a task, rather than its duration, for later usage. That gives as an array like this:
2 7
4 27
7 11
Your array is already sorted (because the input is given in that order) by start time, and that's useful. Use std::sort to sort the array, if needed.
Observe how you could check for the end time of the first task versus the start time of the other tasks. With the right comparison, you can determine the number of active tasks along with the first task. Checking whether the end time of the first task is greater than the start time of the second task, if true, denotes that these two tasks are active together at some point.
Then you would do the same for the comparison of the first with the third task. After that you would know how many tasks were active in relation with the first task.
Afterwards, you are going to follow the same procedure for the second task, and so on.
Putting all that together in code, we get:
#include "stdio.h"
#include "stdlib.h"
#include <algorithm>
typedef struct {
int start;
int dur;
int end;
} task;
int compare (const task& a, const task& b) {
return ( a.start < b.start );
}
int main() {
int num_tasks;
scanf("%d",&num_tasks);
task *t = new task[num_tasks];
for (int i=0;i<num_tasks;i++) {
scanf("%d %d",&t[i].start,&t[i].dur);
t[i].end = t[i].start + t[i].dur;
}
std::sort(t, t + num_tasks, compare);
for (int i=0;i<num_tasks;i++) {
printf("%d %d\n", t[i].start, t[i].end);
}
int max_noOf_tasks = 0;
for(int i = 0; i < num_tasks - 1; i++) {
int noOf_tasks = 1;
for(int j = i + 1; j < num_tasks; j++) {
if(t[i].end > t[j].start)
noOf_tasks++;
}
if(max_noOf_tasks < noOf_tasks)
max_noOf_tasks = noOf_tasks;
}
printf("Max. number of active tasks: %d\n", max_noOf_tasks);
delete [] t;
}
Output:
2 7
4 27
7 11
Max. number of active tasks: 2
Now, good luck with the second part of your question.
PS: Since this is C++, you could have used an std::vector to store your structs, rather than a plain array. That way you would avoid dynamic memory allocation too, since the vector takes care that for you automatically.
char name[4][20];
int count=0;
cout<<"Enter 4 name at most , one name per line:\n";
while(cin.getline(name[count++],20))
;
--count;
(rest of code is for printing if u need to see that too it's at the end)
when i Enter names more than 4 it still prints those names but how can that happen?
because the first dimension is 4 so how can it get more than four
printing part of code:
for(int i=0; i<count; i++)
{
cout<<i<<"="<<name[i]<<endl;
}
system("pause");
}
If I get it correctly, your are asking "why if the array is 4 I can fit 5?".
Unlike Pascal, where arrays boundaries are checked at runtime, C++ doens't do such thing.
Think about it. An array is just some pointer that is added the offset later.
Let's say that you've an array of 5 integers and you do this
int a[5];
a[3] = 3;
a[6] = 4;
Nothing is really wrong, because in the first assignment, the statement equals to a+12 and the second one a+24. You're just incrementing the pointer and unless you don't bother the OS, you could go on and potentially overwrite some other data.
So C++ will very unlikely say something if you cross the arrays boundaries.
This implies that you must always somehow know how big is the array, in your case by simply adding to the loop:
while(count < 4 && cin.getline(name[count++], 20));
You need to tell the while() loop when to stop.
Try this:
char name[4][20];
int count=0;
cout<<"Enter 4 name at most , one name per line:\n";
while(count < 4 && cin.getline(name[count++],20))
;