Codeforces Round #308 (Div. 2) A B C D E

来源:互联网 发布:java string搜索 编辑:程序博客网 时间:2024/05/16 04:38

A Vanya and Table

        水。

#include <bits/stdc++.h>using namespace std;int mp[110][110];int main(){int n;while(cin>>n){int ans=0;while(n--){int x1,y1,x2,y2;cin>>x1>>y1>>x2>>y2;ans+=(x2-x1+1)*(y2-y1+1);}cout<<ans<<endl;}return 0;}

B Vanya and Books

        统计1~n的所有数中,共有多少数字。随便统计一下就好了。

#include <bits/stdc++.h>using namespace std;#define ll long longint main(){ll n;cin>>n;ll ans=0;if(n-1e9>=0){ans+=(n-1e9+1);}if(n-1e8>=0){ans+=(n-1e8+1);}if(n-1e7>=0){ans+=(n-1e7+1);}if(n-1e6>=0){ans+=(n-1e6+1);}if(n-1e5>=0){ans+=(n-1e5+1);}if(n-1e4>=0){ans+=(n-1e4+1);}if(n-1e3>=0){ans+=(n-1e3+1);}if(n-1e2>=0){ans+=(n-1e2+1);}if(n-1e1>=0){ans+=(n-1e1+1);}if(n-1>=0){ans+=(n-1e0+1);}cout<<ans<<endl;return 0;}

C Vanya and Scales

        给w和m两个数,你有一个天平,砝码质量为w^0,w^1,w^2...w^100。问能否称出m。

        贪心,先用大砝码,后用小的,每次使需要称的重量变小就可以了,看看能不能得到0。

#include <bits/stdc++.h>using namespace std;#define ll long longint main(){ll w,m;cin>>w>>m;ll tmp=w;while(tmp<m)tmp*=w;bool ok=0;while(1){if(tmp>m){if(tmp-m<m){m=tmp-m;}}else{m-=tmp;}if(m==0){ok=1;break;}tmp/=w;if(tmp==0)break;}if(ok){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}return 0;}

D Vanya and Triangles

        计算一个点集能构成多少三角形,其实就是问有多少组三点共线,竟然可以暴力过。

#include <bits/stdc++.h>using namespace std;#define ll long longint x[2010];int y[2010];int main(){int n;cin>>n;for(int i=1;i<=n;i++){scanf("%d%d",&x[i],&y[i]);}ll ans=0;for(int k=1;k<=n;k++){for(int i=k+1;i<=n;i++){x[i]-=x[k];y[i]-=y[k];}for(int i=k+1;i<=n;i++){for(int j=i+1;j<=n;j++){if(x[i]*y[j]!=x[j]*y[i]){ans++;}}}} cout<<ans<<endl;return 0;}

Vanya and Brackets

        一个只含'+'和'*'的表达式,添加一个括号,使得结果尽可能大。

        稍微分析一下就可以知道,最优解的括号两边,要么是乘号,要么是开头末尾。。枚举所有添加括号方法用栈处理下表达式就可以了。

#include <bits/stdc++.h>using namespace std;string str;string str2;#define ll long longstack<ll> num;stack<char> op;ll calc(){for(int i=0;i<str.size();i++){if(str[i]=='+'){op.push(str[i]);}else if(str[i]=='*'){op.push(str[i]);}else if(str[i]=='('){op.push(str[i]);}else if(str[i]==')'){while(op.top()!='('){char o=op.top();op.pop();ll a=num.top();num.pop();ll b=num.top();num.pop();if(o=='+'){num.push(a+b);}else{num.push(a*b);}}op.pop();if(op.size()&&op.top()=='*'){op.pop();ll a=num.top();num.pop();ll b=num.top();num.pop();num.push(a*b);}}else{num.push(str[i]-'0');if(op.size()&&op.top()=='*'){op.pop();ll a=num.top();num.pop();ll b=num.top();num.pop();num.push(a*b);}}}while(op.size()){char o=op.top();op.pop();ll a=num.top();num.pop();ll b=num.top();num.pop();if(o=='+'){num.push(a+b);}else{num.push(a*b);}}ll re=num.top();num.pop();return re;}int pos[5010];int main(){cin>>str;str2=str;int len=str.size();ll ans=calc();int cnt=0;for(int i=0;i<len;i++){if(str[i]=='*'){pos[++cnt]=i;}}for(int i=1;i<=cnt;i++){str=str2;str.insert(0,1,'(');str.insert(pos[i]+1,1,')');ans=max(ans,calc());}for(int i=1;i<=cnt;i++){str=str2;str.insert(pos[i]+1,1,'(');str.insert(len+1,1,')');ans=max(ans,calc());}for(int i=1;i<cnt;i++){for(int j=i+1;j<=cnt;j++){str=str2;str.insert(pos[i]+1,1,'(');str.insert(pos[j]+1,1,')');ans=max(ans,calc());}}cout<<ans<<endl;return 0;}

0 0
原创粉丝点击