【原创】Codeforces 39A C*++ Calculations
Index
- Codeforces 39A C*++ Calculations
- 題意翻譯
- 題目描述
- 輸入格式
- 輸出格式
- 輸入輸出樣例
- 輸入 #1
- 輸出 #1
- 輸入 #2
- 輸出 #2
- 說明/提示
- 題意
- 分析
- 代碼
Codeforces 39A C*++ Calculations
題意翻譯
表達式求和,和正常的運算不同的是可以選擇每個加數的運算順序,求最大的值是多少。
題目描述
C*++ language is quite similar to C++. The similarity manifests itself in the fact that the programs written in C*++ sometimes behave unpredictably and lead to absolutely unexpected effects. For example, let’s imagine an arithmetic expression in C*++ that looks like this ( expression expression is the main term):
e x p r e s s i o n : = s u m m a n d ∣ e x p r e s s i o n + s u m m a n d ∣ e x p r e s s i o n ? s u m m a n d expression~:=~ summand~|~ expression+summand ~|~ expression-summand expression?:=?summand?∣?expression+summand?∣?expression?summand
s u m m a n d : = i n c r e m e n t ∣ c o e f f i c i e n t ? i n c r e m e n t summand~:=~increment ~|~ coefficient~ *~ increment summand?:=?increment?∣?coefficient???increment
i n c r e m e n t : = a + + ∣ + + a increment~:=~a++~|++a increment?:=?a++?∣++a
c o e f f i c i e n t : = 0 ∣ 1 ∣ 2 ∣ . . . ∣ 1000 coefficient~:=~0|1|2|...|1000 coefficient?:=?0∣1∣2∣...∣1000
For example,"5*a++-3*++a+a++" is a valid expression in C*++.
Thus, we have a sum consisting of several summands divided by signs “+” or “-”. Every summand is an expression “a++” or “++a” multiplied by some integer coefficient. If the coefficient is omitted, it is suggested being equal to 1 1 1 .
The calculation of such sum in C*++ goes the following way. First all the summands are calculated one after another, then they are summed by the usual arithmetic rules. If the summand contains “a++”, then during the calculation first the value of the “a” variable is multiplied by the coefficient, then value of “a” is increased by 1 1 1 . If the summand contains “++a”, then the actions on it are performed in the reverse order: first “a” is increased by 1 1 1 , then — multiplied by the coefficient.
The summands may be calculated in any order, that’s why sometimes the result of the calculation is completely unpredictable! Your task is to find its largest possible value.
輸入格式
The first input line contains an integer a a a ( ? 1000 < = a < = 1000 -1000<=a<=1000 ?1000<=a<=1000 ) — the initial value of the variable “a”. The next line contains an expression in C*++ language of the described type. The number of the summands in the expression does not exceed 1000 1000 1000. It is guaranteed that the line describing the expression contains no spaces and tabulation.
輸出格式
Output a single number — the maximal possible value of the expression.
輸入輸出樣例
輸入 #1
1 5*a++-3*++a+a++輸出 #1
11
輸入 #2
3 a+++++a輸出 #2
8
說明/提示
Consider the second example. Initially a = 3 a=3 a=3 . Suppose that at first the first summand is calculated, and then the second one is. The first summand gets equal to 3 3 3 , and the value of a a is increased by 1 1 1 . At the calculation of the second summand a a a is increased once more (gets equal to 5 5 5 ). The value of the second summand is 5 5 5 , and together they give 8 8 8 . If we calculate the second summand first and the first summand later, then the both summands equals to 4 4 4 , and the result is 8 8 8, too.
題意
給定一個形如: p 1 ? ( + + a ) + p 2 ? ( a + + ) ? p 3 ? ( + + a ) ? + p k ? ( + + a ) p_1*(++a)+p_2*(a++)-p_3*(++a)\cdots +p_k*(++a) p1??(++a)+p2??(a++)?p3??(++a)?+pk??(++a)的運算式,a是變量, p i p_i pi?是若干個正整數,加和減號是給定的,++a和a++是給定的,你可以自行決定計算這些a++和++a的順序,給定a的初值,使運算式的結果最大。
分析
我起了,一眼秒了,又懷疑自己了, W D N M D \xcancel{W^D{N_M}^D} WDNM?D ?真就白給啊。
因為a只會不斷變大,所以貪心地想,就讓小的a乘上小的系數。也就是,把所有的系數拿出來排序(如果是減號就看作+負系數),然后再乘起來加起來就好了。
①如果a是負的,是不是在考慮負系數的時候……?
你在想什么?
系數是從小到大的,a也是從小到大的,也就是說絕對值越大的負系數和絕對值越大的-a會乘在一起,答案就更大了啊。
如果你實在是擔心a負數的情況,你就這么意會一下,a+=1000,不就正了嗎,再進行上述操作,最后在把剛開始加上的再減去,就不存在a是負數的問題了。
我就是這么把自己糊弄過去的。
②正確性?會不會存在k*(++a)>m*(a++) & k<m的情況?
就拿k*(++a)&m*(a++)來舉例吧。
假設先算前者,ans=k*(a+1)+m*(a+1)=a*(k+m)+k+m;
假設先算后者,ans=k*(a+2)+m*(a)=a*(k+m)+2*k
假設前者更優,那么就應有:[a*(k+m)+k+m]-[a*(k+m)+2*k]>0即m-k>0;所以得出結論,系數更小的就要先算。
k*(++a)&m*(++a),k*(a++)&m*(a++),k*(a++)&m*(++a)的情況同理。
③讀入比較煩,細節很多。
④對于同一個系數,是先計算++a還是a++?
你在想什么?把系數提出來之后k*[(a++)+(++a)+…]的結果會隨著你順序的改變而改變嗎?
白給白給。
碼代碼的時候遇到的錯誤:
①讀入優化沒有讀負數;
②考慮是++a還是a++的時候想著只看前兩位是不是都是+號就好了,結果會被這樣卡掉:a+++a++。還好我沒花多久就想出這組數據了:a+++a+++++a
代碼
#include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std;const int MAXN=102030;void Read(int &p)//無駄無駄無駄無駄無駄無駄無駄無駄無駄無駄無駄無駄無駄無駄無駄無駄無駄無駄無駄無駄無駄 {p=0;char c=getchar();while(c<'0' || c>'9') c=getchar();while(c>='0' && c<='9') p=p*10+c-'0',c=getchar(); }char s[MAXN]; int n,ori,len,ans; struct crl {int num,pre;bool operator < (const crl &c) const{return num<c.num;} }arr[MAXN];int main() {for(int i=1;i<=100000;i++) arr[i].num=-1;scanf("%d",&ori),scanf("%s",s+1),len=strlen(s+1),s[0]='+';for(int i=1,las=0;i<=len;i++)if(s[i]=='a'){n++;for(int j=las+1;j<i;j++)if(s[j]>='0' && s[j]<='9') arr[n].num=(arr[n].num==-1?0:arr[n].num*10)+s[j]-'0';else break;if(arr[n].num==-1) arr[n].num=1; arr[n].num*=(s[las]=='+'?1:-1),las=i+3-2*(arr[n].pre=s[i-1]==s[i-2] && s[i-2]=='+' && i-2>las);}sort(arr+1,arr+1+n);for(int i=1;i<=n;i++) if(arr[i].pre) ans+=++ori*arr[i].num; else ans+=ori++*arr[i].num;printf("%d\n",ans); }總結
以上是生活随笔為你收集整理的【原创】Codeforces 39A C*++ Calculations的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Unity3D获取当前键盘按键及Unit
- 下一篇: 【转】Java-满天繁星案例