雷火4.25

1.用1×1×1的立方体组合的物体的表面积(物体按照坐标轴摆放,可以挨着也可以不挨着)

题目
第一行输入立方体的个数n,然后接下来的n行输入每个立方体的坐标
(找到相邻的然后总面积减去二即可)

#include<iostream>
using namespace std;
int main (){
	int n;
	while(cin>>n){
		int x[n],y[n],z[n];
		int count=0;
		for(int i=0;i<n;i++){
			cin>>x[i]>>y[i]>>z[i];
			for(int j=0;j<i;j++){
				if(x[i]==x[j]&&y[i]==y[j]&&(z[i]-z[j])*(z[i]-z[j])==1)
					count+=2;
				else if(x[i]==x[j]&&z[i]==z[j]&&(y[i]-y[j])*(y[i]-y[j])==1)
					count+=2;
				else if(z[i]==z[j]&&y[i]==y[j]&&(x[i]-x[j])*(x[i]-x[j])==1)
					count+=2;
			}
		}
		cout<<n*6-count;
	}
}

2.求最大的广告牌面积(最大直方图)

第一行输入直方图数量,接下来的每第二行输入直方图的高度,用空格隔开
单调栈的应用,维护一个栈始终单调递增,每次遇到不递增的值计算最大面积,栈内始终保存了当前遇到的最小值


#include<iostream>
#include<stack>
using namespace std;
int main(){
	int n;
	while(cin>>n){
		stack<int> sta;
		int a[n];
		int max=0;
		for(int i=0;i<n;i++){
			int res=0;
			cin>>a[i];
			if((sta.empty()||a[sta.top()]<=a[i])&&(i!=n-1)){
				sta.push(i);
			}
			else{
				if(i==n-1){
					sta.push(i);
					res=1;
				}
				while(a[sta.top()]>a[i]||i==n-1){
					int high=a[sta.top()];
					sta.pop();
					int right;
					if(i==n-1)
						right=i;
					else 
						right=i-1 ;
					int left;
					if(sta.empty()){
						left=0;
					} 
					else{
						left=sta.top()+1;
					}
					if(max<high*(right-left+1))
						max=high*(right-left+1);
					if(sta.empty()||(res==1&&sta.empty()))
						break;
				}
				sta.push(i);
			}
		}
		cout<<max;
	}
}

3. 找出满足下列条件的数的个数:

a)该十进制数由n个0-9的数字组成。

b)每三位可以被x整除。

c)所有位上的数字之和为s。
结果可能数据较大,需要模1000009

#include <iostream>
#include <cmath>
#include <cstring>
#include <stack>
#include <map>
using namespace std;
typedef long long ll;
const ll maxn = 1e5 + 5;
ll dp[51][1005][460];
bool vis[1005];
ll num[1005];
const ll mod=1000009;
int main() {
    ll n, sum, last, w, x, s, i, j, t,k, ss,ans = 0, cnt;
    while (cin >> n >> s >> x) {
        cnt = 1;
        map<ll,ll>mp;
        memset(vis, false, sizeof(vis));
        memset(dp, 0, sizeof(dp));
        for (i = 0; i < 1000; i++)
            if (i % x == 0){
            	mp[i]=cnt;num[cnt++] = i; vis[i] = true;
			}
        for(i=1;i<cnt;i++)
        {
             w=num[i]%10;
             t=(num[i]/10)%10;
             ss=num[i]/100;
             sum=ss*100+t*10+w;
            dp[3][i][ss+t+w]=1;
        }
        for(i=3;i<=n-1;i++)
        {
            for(j=1;j<cnt;j++)
            {
                sum=num[j]%100;
                for(t=0;t<10;t++)
                {
                    if(vis[sum*10+t])
                    {
                        w=mp[sum*10+t];
                        for(k=0;k<=450;k++)
                        {
                            dp[i+1][w][k+t]=(dp[i][j][k]+dp[i+1][w][k+t])%mod;

                        }
                    }
                }
            }
        }
        sum=0;
        for(i=1;i<cnt;i++)
            sum=(dp[n][i][s]+sum)%mod;
       cout<<sum<<endl;

    }
    return 0;
}

4.模拟lru过程,想到了使用优先级队列实现,实现过程太长了,写不出。

猜你喜欢

转载自blog.csdn.net/yang15309214213/article/details/105800055