Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.math.BigInteger;
- import java.util.*;
- public class J {
- long gcd(long x,long y){
- if(x==0)return y;
- return gcd(y%x,x);
- }
- void solve() throws IOException {
- int t=nextInt();
- while(t-->0){
- int n=nextInt();
- long c1=0;
- long c2=0;
- int[] count=new int[n];
- int[] type=new int[n];
- for(int i=0;i<n;i++){
- count[i]=nextInt();
- char c=nextToken().charAt(0);
- if(c=='B')type[i]=1;
- else type[i]=0;
- if(type[i]==0)c1+=count[i];
- else c2+=count[i];
- }
- if(c1==0||c2==0){
- out.println(c1+c2);
- continue;
- }
- long g=gcd(c1,c2);
- c1/=g;
- c2/=g;
- long x1=0;
- long x2=0;
- if(type[0]==0)x1+=count[0];
- else x2+=count[0];
- int ans=0;
- for(int i=1;i<n;i++){
- if(type[i]==0){
- long q=x2*c1;
- if(q%c2==0){
- long y=q/c2;
- if(y<=x1){
- x1+=count[i];
- continue;
- }
- if(y>x1+count[i]){
- x1+=count[i];
- continue;
- }
- //long z=y-x1;
- x1+=count[i];
- //x2+=z;
- ans++;
- }
- else x1+=count[i];
- }
- else{
- long q=x1*c2;
- if(q%c1==0){
- long y=q/c1;
- if(y<=x2){
- x2+=count[i];
- continue;
- }
- if(y>x2+count[i]){
- x2+=count[i];
- continue;
- }
- //long z=y-x2;
- //x2=y;
- //x1+=z;
- x2+=count[i];
- ans++;
- }
- else
- x2+=count[i];
- }
- }
- out.println(ans);
- }
- }
- public static void main(String[] args) throws IOException {
- new J().run();
- }
- void run() throws IOException {
- reader = new BufferedReader(new InputStreamReader(System.in));
- tokenizer = null;
- out = new PrintWriter(new OutputStreamWriter(System.out));
- solve();
- reader.close();
- out.flush();
- }
- BufferedReader reader;
- StringTokenizer tokenizer;
- PrintWriter out;
- int nextInt() throws IOException {
- return Integer.parseInt(nextToken());
- }
- long nextLong() throws IOException {
- return Long.parseLong(nextToken());
- }
- double nextDouble() throws IOException {
- return Double.parseDouble(nextToken());
- }
- String nextToken() throws IOException {
- while (tokenizer == null || !tokenizer.hasMoreTokens()) {
- tokenizer = new StringTokenizer(reader.readLine());
- }
- return tokenizer.nextToken();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement