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

本文包含四个编程题目:绘制矩形区域、统计数字总数、实现天平称重算法、计算点集中形成的三角形数量,以及一个关于优化表达式计算的问题。

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;
}

Vanya and Books

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long

int 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;
}

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 long

int 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;
}

Vanya and Triangles

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long

int 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 long

stack<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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值