标签:情况 acm cat define 问题 div 一个 rem sel
BaoBao is taking a walk in the interval [0,n] on the number axis, but he is not free to move, as at every point (i−0.5) for all i∈[1,n], where i is an integer, stands a traffic light of type t?i?? (t?i??∈{0,1}).
BaoBao decides to begin his walk from point p and end his walk at point q (both pand q are integers, and p<q). During each unit of time, the following events will happen in order:
A traffic light of type 0 is initially red, and a traffic light of type 1 is initially green.
Denote t(p,q) as the total units of time BaoBao needs to move from point p to point q. For some reason, BaoBao wants you to help him calculate
where both p and q are integers. Can you help him?
There are multiple test cases. The first line of the input contains an integer T, indicating the number of test cases. For each test case:
The first and only line contains a string s (1≤∣s∣≤10?^5??, ∣s∣=n, s?i??∈{‘0’,‘1’} for all 1≤i≤∣s∣), indicating the types of the traffic lights. If s?i??=‘0’, the traffic light at point (i−0.5) is of type 0 and is initially red; If s?i??=‘1’, the traffic light at point (i−0.5) is of type 1 and is initially green.
It‘s guaranteed that the sum of ∣s∣ of all test cases will not exceed 10?6??.
For each test case output one line containing one integer, indicating the answer.
3
101
011
11010
12
15
43
For the first sample test case, it‘s easy to calculate that t(0,1)=1, t(0,2)=2, t(0,3)=3, t(1,2)=2, t(1,3)=3 and t(2,3)=1, so the answer is 1+2+3+2+3+1=12.
For the second sample test case, it‘s easy to calculate that t(0,1)=2, t(0,2)=3, t(0,3)=5, t(1,2)=1, t(1,3)=3 and t(2,3)=1, so the answer is 2+3+5+1+3+1=15.
题目思路:
真数据:
真测试输入 4 10100101010101010101010101011110101010101 111111111111111110100101010101010101010101011110101010101 1111111111111111111 00000000000000000000000000 真测试输出 13951 41543 2470 6552
感兴趣的可以试试上面的数据,应该没问题!
代码;
#include<stdio.h> #include<iostream> #include<cmath> #include<string> #include<string.h> #include<time.h> #include<algorithm> using namespace std; const double eps=1e-7; #define PI acos(-1.0) #define ll long long #define mian main #define mem(a,b) memset(a,b,sizeof(a)) char a[100005]; int b[100005][2]; int main() { int T; while(scanf("%d",&T)!=EOF) { while(T--) { scanf("%s",a); int len=strlen(a); for(int i=0; i<len; i++) { b[i][0]=a[i]-‘0‘; ///原数据层 b[i][1]=!b[i][0]; ///红绿灯交替一遍后!若红绿灯再交替一遍其实就是上一层! } int Time=0; int ans=0,k=0; int f1=0,f2=-1;//表示当前层数0或者1 int num1=0,num2=0; for(int k=0; k<len; k++) ///暴力枚举一遍len个区间即可!! { if(f1==f2) { f2=-1; num1+=num2; num2=0; } num1-=k; ///这里是个终点,每往后走一个,会少k个前面的区间覆盖到当前区间的,必须减去 // printf(" (f1=%d f2=%d) ",f1,f2); if(f1==0) ///分配每轮的萌新层,与f1层相符就加进f1里 { num1+=len-k; } else ///否则就新开一个f2来进行记录! { if(f2==-1) f2=0; num2+=len-k; } if(b[k][f1]==1) { ans+=1*(num1 ),f1=!f1; } else ans+=2*(num1 ); if(f2!=-1) { if(b[k][f2]==1) { ans+=1*(num2 ),f2=!f2; } else ans+=2*(num2 ); } // printf("&%d ",ans); // printf("endd =%d ans_sum=%d\n",j,ans); // printf("]%d\n",ans); // printf("--> (f1=%d f2=%d)\n",f1,f2); // printf("num1=%d num2=%d Time=%d &ans=%d\n",num1,num2,++Time,ans); } printf("%d\n",ans); } } return 0; }
The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online - H Traveling on the Axis-【思维模拟题目】
标签:情况 acm cat define 问题 div 一个 rem sel
原文地址:https://www.cnblogs.com/zhazhaacmer/p/9676664.html