Instant Complexity
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 8 Accepted Submission(s) : 7
Generally, one determines the run-time of an algorithm in relation to the `size' n of the input, which could be the number of objects to be sorted, the number of points in a given polygon, and so on. Since determining a formula dependent on n for the run-time of an algorithm is no easy task, it would be great if this could be automated. Unfortunately, this is not possible in general, but in this problem we will consider programs of a very simple nature, for which it is possible. Our programs are built according to the following rules (given in BNF), where < number > can be any non-negative integer:
< Program > ::= "BEGIN" < Statementlist > "END"
< Statementlist > ::= < Statement > | < Statement > < Statementlist >
< Statement > ::= < LOOP-Statement > | < OP-Statement >
< LOOP-Statement > ::= < LOOP-Header > < Statementlist > "END"
< LOOP-Header > ::= "LOOP" < number > | "LOOP n"
< OP-Statement > ::= "OP" < number >
The run-time of such a program can be computed as follows: the execution of an OP-statement costs as many time-units as its parameter specifies. The statement list enclosed by a LOOP-statement is executed as many times as the parameter of the statement indicates, i.e., the given constant number of times, if a number is given, and n times, if n is given. The run-time of a statement list is the sum of the times of its constituent parts. The total run-time therefore generally depends on n.
Output a blank line after each test case.
LOOP n
OP 4
LOOP 3
LOOP n
OP 1
END
OP 2
END
OP 1
END
OP 17
END
OP 1997 LOOP n LOOP n OP 1 END END
END
Runtime = 3*n^2+11*n+17
Program #2
Runtime = n^2+1997
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <string>
#include <algorithm>
using namespace std; char cmd[][];
int mul[]; struct factor
{
int coef,time;
}fac[]; bool cmp(const factor f1,const factor f2)
{
return f1.time>f2.time||f1.time==f2.time&&f1.coef>f2.time;
} int main()
{
// freopen("in.txt","r",stdin);
int T,cas=;
scanf("%d",&T);
while(T--)
{
cas++;
int hn=,tn=,tot=,i,j,k,rear1=,rear2=;
memset(fac,,sizeof(fac));
memset(mul,,sizeof(mul));
while(hn!=tn) //当左右括号数相等时输入结束
{
scanf("%s",cmd[tot]);
if(strcmp(cmd[tot],"LOOP")==)
hn++;
else if(strcmp(cmd[tot],"END")==)
tn++;
tot++;
}
// for(i=0;i<tot;i++)
// cout<<cmd[i]<<endl;
for(i=;i<tot;i++)
{
if(cmd[i][]=='O')
{
if(cmd[i+][]=='n')
{
fac[rear1].coef=;
fac[rear1].time=;
rear1++;
}
else
{
int tmp=;
for(j=;j<strlen(cmd[i+]);j++)
tmp=tmp*+(cmd[i+][j]-'');
fac[rear1++].coef=tmp;
// cout<<tmp<<endl;
}
}
else if(cmd[i][]=='L')
{
fac[rear1++].coef=-; //作为分界线,表明括号的开始
if(cmd[i+][]=='n')
mul[rear2++]=-; //以-1代表乘数是n
else
{
int tmp=;
for(j=;j<strlen(cmd[i+]);j++)
tmp=tmp*+(cmd[i+][j]-'');
mul[rear2++]=tmp;
// cout<<tmp<<endl;
}
}
else if(cmd[i][]=='E')
{
int t=rear1-;
rear2--;
while(fac[t].coef!=-&&t!=-)
{
t--;
}
for(j=t+;j<rear1-;j++)
{
if(fac[j].coef==)
continue;
for(k=j+;k<rear1;k++)
{
if(fac[k].coef==)
continue;
if(fac[j].time==fac[k].time) //合并同类项
{
fac[j].coef+=fac[k].coef;
fac[k].coef=;
fac[k].time=;
}
}
}
if(rear2==-) //表明运行到最后一个end了,跳出
break;
fac[t].coef=; //消除分界线
int m=mul[rear2];
if(m==-)
{
for(j=t+;j<rear1;j++)
{
if(fac[j].coef)
fac[j].time++;
}
}
else
{
for(j=t+;j<rear1;j++)
{
if(fac[j].coef)
fac[j].coef*=m;
}
}
}
}
sort(fac,fac+rear1,cmp);
// for(i=0;i<rear1;i++)
// cout<<fac[i].coef<<' '<<fac[i].time<<endl;
printf("Program #%d\n",cas);
printf("Runtime = ");
if(fac[].coef==)
{
printf("0\n\n");
continue;
}
for(i=;i<rear1;i++)
{
if(fac[i].time==&&fac[i].coef==)
break;
if(fac[i].coef==&&fac[i].time==)
printf("%d",fac[i].coef);
if(fac[i].coef>)
{
printf("%d",fac[i].coef);
if(fac[i].time>)
printf("*");
}
if(fac[i].coef>&&fac[i].time>)
{
printf("n");
if(fac[i].time>)
printf("^%d",fac[i].time);
}
if(fac[i+].coef!=)
printf("+");
}
printf("\n\n");
}
return ;
}