非精写版-51nod基础训练(5)

小明爱矩阵

题目说用到的数据结构是结构体,就是把每个矩形的坐标都用结构体存起来,然后两两矩形计算重合的面积,将这些面积存到一个数组中,排序后二分找出大于k的面积即可。

#include<bits/stdc++.h>
using namespace std; 
typedef long long ll;
struct node{
    ll x,y,a,b;
}t[1010];
ll k;
int cnt;
ll p[1010*1010];
ll S(node u,node v){
    ll dx,dy,ux,uy;
    dx=max(u.x,v.x);
    dy=max(u.y,v.y);
    ux=min(u.a,v.a);
    uy=min(u.b,v.b);
    return max(1LL*0,ux-dx)*max(1LL*0,uy-dy);
}
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>t[i].x>>t[i].y>>t[i].a>>t[i].b;
    }
    cnt=0;
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            p[cnt++]=S(t[i],t[j]);
        }
    }
    sort(p,p+cnt);
    int q;
    cin>>q;
    while(q--){
        cin>>k;
        int r=upper_bound(p,p+cnt,k)-p;
        cout<<cnt-r<<endl;
    }
    system("pause");
    return 0;
}
小明查成绩

简单的模拟题

#include<bits/stdc++.h>
using namespace std; 
typedef long long ll;
const int maxn=10010;
struct node{
    string name;
    int x,y,z;
}p[maxn];
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>p[i].name;
        cin>>p[i].x>>p[i].y>>p[i].z;
    }
    int k;
    cin>>k;
    while(k--){
        int a,b,c,d,e,f;
        cin>>a>>b>>c>>d>>e>>f;
        for(int i=0;i<n;i++){
            if(p[i].x>=a&&p[i].x<=b&&p[i].y>=c&&p[i].y<=d&&p[i].z>=e&&p[i].z<=f){
                cout<<p[i].name<<endl;
            }
        }
    }
    system("pause");
    return 0;
}
小明和他的同学们

题目考察了优先队列的应用,重载小于符号,让时间长的(后吃完的)排在前面,相同时间吃一个花费时间长的(吃的慢的)排在前面,优先队列中是大于的在前,正好反过来,符合题意。
最容易想到的就是,同等时间下,如果吃的快,应该先给他(初始同学们的时间都是零,谁都没有开始吃)。这样放到优先队列中正好符合我们的思想。如果选了他,这位同学应该开始吃了,那需要把他吃一块巧克力花费的时间加到他的总时间内,反复操作,直到巧克力吃没了或者时间到了。最后在吃的过程中,如果该同学吃了一块,就把他吃的块数加一,特判一下这块吃完之前会不会到时间,若到时间,就把没吃完的数量加一,然后重新入队,传递下去。最后记录一下那两个量,得到答案。

#include<bits/stdc++.h>
using namespace std; 
typedef long long ll;
const int maxn=101;
int n,m,x,t,cost;
struct node{
    int time,cost,num,f;
    bool operator < (const node &a) const{
        if(time==a.time) return cost>a.cost;
        return time>a.time;
    }
}st;
int ans,res;
priority_queue<node> q;
int main(){
    cin>>t;
    while(t--){
        ans=0;res=0;
        cin>>n>>m>>x;
        for(int i=1;i<=n;i++){
            cin>>cost;
            q.push((node){0,cost});
        }
        while(q.top().time<x&&m){
            st=q.top();
            q.pop();
            st.time+=st.cost;
            if(st.time>x) st.f=1;
            else st.num++;
            q.push(st);
            m--;
        }
        while(!q.empty()){
            st=q.top();
            q.pop();
            ans+=st.f;
            res+=st.num;
        }
        cout<<res<<" "<<ans<<endl;
    }
    //system("pause");
    return 0;
}
曼哈顿的噪音

在这里插入图片描述

果然超时了。还以为能卡过去。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pi;
vector<pair<ll,ll> >ve;
ll a,b;
ll ans=-1;
int n;
ll dis(pi p1,pi p2){
    if(p1.first-p2.first>=0&&p1.second-p2.second>=0)
        return (p1.first+p1.second)-(p2.first+p2.second);
    else if(p1.first-p2.first<0&&p1.second-p2.second>=0)
        return (p2.first-p2.second)-(p1.first-p1.second);
    else if(p1.first-p2.first>=0&&p1.second-p2.second<0)
        return (p1.first-p1.second)-(p2.first-p2.second);
    else if(p1.first-p2.first<0&&p1.second-p2.second<0)
        return (p2.first+p2.second)-(p1.first+p1.second);
}
int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a>>b;
        ve.push_back(make_pair(a,b));
    }
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            ans=max(ans,dis(ve[i],ve[j]));
        }
    }
    cout<<ans<<endl;
    //system("pause");
    return 0;
}

接下来就考虑怎么把二维优化成一维。根据题解可以看出,最终的式子只有两种情况,要么是 { x i − y i } \{x_i-y_i\} {xiyi},要么是 { x i + y i } \{x_i+y_i\} {xi+yi},那么可以在遍历的过程中找出 m a x { x i − y i } max\{x_i-y_i\} max{xiyi} m i n { x i − y i } min\{x_i-y_i\} min{xiyi} m a x { x i + y i } max\{x_i+y_i\} max{xi+yi} m i n { x i + y i } min\{x_i+y_i\} min{xi+yi}。最后在用最大减最小,求出最长的曼哈顿距离。(也可以先存起来在排序,排序写好的话复杂度比一个个维护要低)。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pi;
vector<pair<ll,ll> >ve;
ll a,b;
int n;
vector<ll> ans[2];
ll t1,t2;
int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a>>b;
        t1=a-b;
        t2=a+b;
        ans[0].push_back(t1);
        ans[1].push_back(t2);
    }
    sort(ans[0].begin(),ans[0].end());
    sort(ans[1].begin(),ans[1].end());
    cout<<max(ans[1][n-1]-ans[1][0],ans[0][n-1]-ans[0][0])<<endl;
    //system("pause");
    return 0;
}
圆形巧克力

1.#IO 错误,表示double大概率遇到了除以0的情况。
在除之前特判一下即可。
具体的题意就是有多个点,找最大的三角形(选三个点)外接圆半径。

#include<bits/stdc++.h>
using namespace std;
const int MAXN =655;
const double PI =acos(-1.0);
double dis[MAXN][MAXN];
int x[MAXN],y[MAXN];
double distance(double x1, double y1,double x2, double y2) {
	return sqrt((double)((x2- x1)* (x2- x1)+ (y2- y1)* (y2- y1)));
}
//s = abs( x1*y2+x2*y3+x3y1-x2*y1-x3*y2-x1*y3 )/2 
double triangleArea(int i, int j, int k) {
    double kk=fabs((double)(x[i]- x[k])*(y[j] - y[k]) - (x[j] - x[k]) * (y[i] - y[k]))*(0.5);
	return kk!=0?kk:-1;
}
//外接圆半径 R=a*b*c/(4*s )
double Radius(int i, int j, int k) {
	double radius = dis[i][j] *dis[i][k] * dis[j][k] / triangleArea(i, j, k) *(0.25);
    if(radius==0) return 0;
	return radius;
}
int main(){
	int casn;
	scanf("%d", &casn);
	while(casn --){
		int i,j, k,n;
		scanf("%d", &n);
		for(i = 0; i < n; i ++){
			scanf("%d%d",&x[i], &y[i]);
		}
		for(i = 0; i < n; i ++)
			for(j = i+ 1; j < n; j ++)
				dis[i][j] = dis[j][i] = distance(x[i], y[i],x[j],y[j]);
		double maxRadius = 0;
		for(i = 0; i < n; i ++)
			for(j = i+ 1; j < n; j ++)
				for(k = j+ 1; k < n; k ++){
					double radius = Radius(i ,j, k);
					if(radius > maxRadius) maxRadius =radius;
				}
				printf("%.3lf\n",maxRadius);
	}
    //system("pause");
	return 0;
}
矩形的数量V6

(容斥原理——我不会)
如果不考虑题目特殊要求,总共的格子数为 ( ( n + 1 ) ∗ n / 2 ) ∗ ( ( m + 1 ) ∗ m / 2 ) ((n+1)*n/2)*((m+1)*m/2) ((n+1)n/2)((m+1)m/2)个。
再去掉包含第u行第v列的格子:

  • 矩形的上边界有 u u u 种,下边界有 m − u + 1 m-u+1 mu+1 种,左边界有 v v v 种,右边界有 n − v + 1 n-v+1 nv+1 种。
  • 根据乘法原理,总共有 u ∗ v ∗ ( m − u + 1 ) ∗ ( n − v + 1 ) u*v*(m-u+1)*(n-v+1) uv(mu+1)(nv+1) 种。
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
typedef long long ll;
ll m,n,u,v;
int main(){
    cin>>m>>n>>u>>v;
    ll t1,t2,t3,t4;
    t1=((m+1)*m/2)%mod;
    t2=((n+1)*n/2)%mod;
    t3=(u*v)%mod;
    t4=(m-u+1)*(n-v+1)%mod;
    ll t=(t1*t2-t3*t4)%mod;
    cout<<(t+mod)%mod;
    //system("pause");
    return 0;
}

再吐槽一句,千万别乱取模呀。

中缀表达式
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
typedef long long ll;
stack<double> num;
stack<char> opp;
char s[205];
int prior(char x){
    if(x=='(') return 0;
    else if(x==')') return 3;
    if(x=='+'||x=='-') return 1;
    else return 2;
}
void cal(char t){
    double x=num.top();num.pop();
    double y=num.top();num.pop();
    if(t=='+') num.push(x+y);
    else if(t=='-') num.push(y-x);
    else if(t=='*') num.push(x*y);
    else if(t=='/') num.push(y/x);
}
int main(){
    while(~scanf("%s",s)){
        char p=s[0];
        if(s[1]||p>='0'&&p<='9'){
            double x;
            sscanf(s,"%lf",&x);
            num.push(x);
        }else if(p==')'){
            while(opp.top()!='('){
                cal(opp.top());
                opp.pop();
            }
            opp.pop();
        }else if(p=='('){
            opp.push(p);
        }else{
			if(opp.empty()) opp.push(p);
            else if(prior(p) > prior(opp.top())){
				opp.push(p);
			}else{
                while(!opp.empty() && prior(p)<=prior(opp.top())){
                    cal(opp.top());
                    opp.pop();
                }
                opp.push(p);
            }
        }
    }
    while(opp.size()){
        cal(opp.top());
        opp.pop();
    }
    printf("%.6lf\n",num.top());
    system("pause");
    return 0;
}
后缀表达式

后缀表达式就是不用判断优先级的中缀表达式。

#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
typedef long long ll;
stack<double> num;
char s[205];
void cal(char t){
    double x=num.top();num.pop();
    double y=num.top();num.pop();
    if(t=='+') num.push(x+y);
    else if(t=='-') num.push(y-x);
    else if(t=='*') num.push(x*y);
    else if(t=='/') num.push(y/x);
}
int main(){
    while(~scanf("%s",s)){
        char p=s[0];
        if(s[1]||p>='0'&&p<='9'){
            double x;
            sscanf(s,"%lf",&x);
            num.push(x);
        }else{
			cal(p);
        }
    }
    printf("%.6lf\n",num.top());
    //system("pause");
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值