Skip to content

Commit ed42806

Browse files
authored
Merge pull request ashutosh97#101 from JanviMahajan14/code
Added PROXY problem
2 parents 0002ce9 + fa0fe8b commit ed42806

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

PROXY/question.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Chef is a brilliant university student that does not attend lectures because he believes that they are boring and coding is life! However, his university follows certain rules and regulations, and a student may only take an exam for a course if he has attended at least 75% of lectures for this course.
2+
3+
Since you are Chef's best friend, you want to help him reach the attendance he needs to take exams. Unfortunately, Chef is still focused on his code and refuses to attend more lectures, so the only option is to have some of his friends mark him as present by proxy. This trick is well-known in the university, but only few have the talent to pull it off.
4+
5+
In a certain course, there is exactly one lesson per day over the course of D days (numbered 1 through D). You are given a string S with length D describing the lessons Chef attended — for each valid i, the i-th character of this string is either 'A' if Chef was absent on day i or 'P' if Chef was actually present on day i.
6+
7+
For each day d when Chef is absent, one of Chef's friends can mark him as present by proxy on this day only if he was present (if he was really present, not just marked as present) on at least one of the previous two days, i.e. days d−1 and d−2, and on at least one of the following two days, i.e. days d+1 and d+2. However, it is impossible to mark him as present by proxy on the first two days and the last two days.
8+
9+
Find the minimum number of times Chef has to be marked as present by proxy so that his attendance becomes at least 75% (0.75). Chef's attendance is number of days when he was marked as present, either by proxy or by actually being present, divided by D.
10+
11+
Input
12+
The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
13+
The first line of each test case contains a single integer D.
14+
The second line contains a single string S with length D.
15+
16+
Output
17+
For each test case, print a single line containing one integer — the minimum number of times Chef needs to be marked as present by proxy, or −1 if it is impossible to make Chef achieve 75% attendance.
18+
19+
Constraints
20+
1≤T≤200
21+
1≤D≤1,000
22+
S contains only characters 'A' and 'P'
23+
Subtasks
24+
Subtask #1 (100 points): original constraints
25+
26+
Example Input
27+
1
28+
9
29+
PAAPPAPPP
30+
Example Output
31+
1
32+
Explanation
33+
Example case 1: With a proxy on the third day, the attendance string is "PAPPPAPPP". Now, Chef's attendance is at least 75%, so the minimum number of times Chef needs to be marked as present by proxy is 1.

PROXY/solution.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
float att(int act,int cnt,int d){
5+
float t = (double)cnt/d;
6+
if(t>=0.75)
7+
cout<<cnt-act<<endl;
8+
return t;
9+
}
10+
11+
int main(){
12+
int t;
13+
cin>>t;
14+
while(t--){
15+
int d,cnt=0,i=0,flag=0;
16+
cin>>d;
17+
char s[d];
18+
cin >> s;
19+
20+
21+
//count of present
22+
for(i=0;i<d;i++){
23+
if(s[i]=='P')
24+
cnt++;
25+
}
26+
27+
int act = cnt;
28+
29+
30+
31+
if(att(act,cnt,d)<0.75){
32+
for(i=2;i<d-2;i++){
33+
if((s[i]=='A') && (s[i-1]=='P' || s[i-2]=='P') && (s[i+1]=='P' || s[i+2]=='P'))
34+
{
35+
cnt++;
36+
if(att(act,cnt,d)>=0.75){
37+
flag=1;
38+
break;
39+
}
40+
41+
}
42+
}
43+
if(flag==0)
44+
cout<<-1<<endl;
45+
}
46+
47+
}
48+
return 0;
49+
}

0 commit comments

Comments
 (0)