C*++ Calculations
http://codeforces.com/problemset/problem/39/A
贪心 按照常数大小从小到大排序, 结果一个一个计算上来, 不影响结果正确性。
View Code
#include <iostream> #include <cstdio> #include <vector> #include <cmath> #include <set> #include <map> #include <queue> #include <fstream> #include <string> #include <cstring> #include <algorithm> int f_min(int x,int y) {if(x<y)return x; else return y;} int f_max(int x,int y) {if(x<y)return y; else return x;} void swap(int&x,int&y) {x=x^y;y=x^y;x=x^y;} int f_abs(int x) {return (x)>(0)?(x):(-x);} int lowbit(int x) {return (x)&(-x);} bool isdig(char ch) {return ch>='0' && ch<='9';} //double f_min(double x,double y) {if(x<y)return x; else return y;} //double f_max(double x,double y) {if(x<y)return y; else return x;} using namespace std; const int MM = 222222; #define mod 10000000000 const double Pi = acos(-1.0); const double lep = 1e-10; const double inf = 1000000000.00; #define debug puts("wrong") #define maxint 0x3f3f3f3f #define lson rt<<1 #define rson rt<<1|1 typedef __int64 int64; //typedef long long int64; //const __int64 maxint = 1000000000000000; int dx[8]={1,2,1,2,-1,-2,-1,-2}; int dy[8]={2,1,-2,-1,2,1,-2,-1}; int N,M, Q; int NE; struct Pos { int64 val; bool ff; bool operator<(Pos x) const{ return val<x.val; } }p[MM]; char name[MM]; void get_data() { int64 i,j,k,a,tmp,len; scanf("%s",name); len=strlen(name); NE=0; for(i=0;i<len;i++) { // printf("%d\n", i); if(i==0) { if(name[i]=='a') { p[NE].val=1; p[NE++].str="a"; i+=2; continue; } if(name[i]=='+') { p[NE].val=1; p[NE++].str="+"; i+=2; continue; } if(isdig(name[i])) { tmp=0; for(j=i;j<len;j++) if(name[j]=='*') break; else tmp=tmp*10+name[j]-'0';; p[NE].val=tmp; p[NE++].str=name[j+1]; i=j+3; continue; } } else { if(name[i]=='+') { if(name[i+1]=='a') { p[NE].val=1; p[NE++].str="a"; i+=3; continue; } if(isdig(name[i+1])) { tmp=0; for(j=i+1;j<len;j++) if(name[j]=='*') break; else tmp=tmp*10+name[j]-'0'; p[NE].val=tmp; p[NE++].str=name[j+1]; i=j+3; continue; } if(name[i+1]=='+') { p[NE].val=1; p[NE++].str="+"; i+=3; continue; } } if(name[i]=='-') { if(name[i+1]=='a') { p[NE].val=-1; p[NE++].str=name[i+1]; i+=3; continue; } if(isdig(name[i+1])) { tmp=0; for(j=i+1;j<len;j++) if(name[j]=='*') break; else tmp=tmp*10+name[j]-'0'; p[NE].val=-tmp; p[NE++].str=name[j+1]; i=j+3; continue; } if(name[i+1]=='+') { p[NE].val=-1; p[NE++].str=name[i+1]; i+=3; continue; } } } } } void solve() { int64 i,j,k,tmp=0,ans=0; sort(p,p+NE); // for(i=0;i<NE;i++) cout<<p[i].str<<endl; for(i=0;i<NE;i++) { if(p[i].str=="+") ans+=(p[i].val*(N+1)); else ans+=(p[i].val*N); N++; } printf("%I64d\n", ans); } int main() { // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); while(scanf("%I64d",&N)!=EOF) get_data(),solve(); return 0; } /* 10 a++-++a-5*++a-4*a++ */