省赛练习题

Maximal submatrix

Given a matrix of n rows and m columns,find the largest area submatrix which is non decreasing on each column

Input
The first line contains an integer T(1≤T≤10)representing the number of test cases.
For each test case, the first line contains two integers n,m(1≤n,m≤2∗103)representing the size of the matrix
the next n line followed. the i-th line contains m integers vij(1≤vij≤5∗103)representing the value of matrix
It is guaranteed that there are no more than 2 testcases with n∗m>10000

Output
For each test case, print a integer representing the Maximal submatrix

Sample Input
1
2 3
1 2 4
2 3 3
Sample Output
4

题意:
求最大沿列非递减矩阵面积

#include<bits/stdc++.h>
using namespace std;
const int N=2*1e3+2;
int cnt[N][N];//存储数据 
int dp[N][N];//也可以转化为一维 

int main(){
	int t;
	scanf("%d",&t); 
	while(t--){
		int n,m;
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++){
				dp[i][j]=1;
				scanf("%d",&cnt[i][j]);
				if(i>1 && cnt[i][j] >= cnt[i-1][j]) 
					dp[i][j] = dp[i-1][j]+1;//如果上一个小于此时,那么dp+1,不然重新变为1 
			}
		
		int res=0,con=0,Min=N;	
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				if(dp[i][j]!=1){//表示上一级可以连续
					con++;//连续个数 
					Min=min(Min,dp[i][j]); 
					if(j==m&&con){//循环最后也判断 
						res=max(res,con*Min);//更新最大值 
						con=0;//重置 
						Min=N;//重置			
					}
				}
				else{//碰到一说明此时最小矩阵断开,与上面相似 
					res=max(res,con*Min);
					con=0;
					Min=N;
				}
			}			
		}	
		
		res=max(res,m);//如果小于行数的个数,改变res
		printf("%d\n",res);	
	}
	return 0;
}

Yes, Prime Minister

Mr. Hacker’s Department of Administrative Affairs (DAA) has infinite civil servants. Every integer is used as an id number by exactly one civil servant. Mr. Hacker is keen on reducing overmanning in civil service, so he will only keep people with consecutive id numbers in [l,r][l,r] and dismiss others.

However, permanent secretary Sir Humphrey’s id number is xx and he cannot be kicked out so there must be l≤x≤r. Mr. Hacker wants to be Prime Minister so he demands that the sum of people’s id number
在这里插入图片描述
i must be a prime number.

You, Bernard, need to make the reduction plan which meets the demands of both bosses. Otherwise, Mr. Hacker or Sir Humphrey will fire you.

Mr. Hacker would be happy to keep as few people as possible. Please calculate the minimum number of people left to meet their requirements.

A prime number pp is an integer greater than 1 that has no positive integer divisors other than 1 and p.

Input
The first line contains an integer T(1≤T≤10^6) - the number of test cases. Then T test cases follow.

The first and only line of each test case contains one integer xi(−10^7≤xi≤10 ^7) - Sir Humphrey’s id number.

Output
For each test case, you need to output the minimal number of people kept if such a plan exists, output -1 otherwise.

Sample Input
10
-2
-1
0
1
2
3
4
5
6
7
Sample Output
6
4
3
2
1
1
2
1
2
1

题意:
找到一个最小长度区间[l, r],区间和是质数,l<=x<=r。

思路:
这个大佬写得超级清楚!!!好棒!!!
https://blog.csdn.net/laysan/article/details/119518351
![在这里插入图片描述](https://img-blog.csdnimg.cn/1cd19b4fab284ab6990e895dd45bba50.png

#include<iostream>
#include<cmath>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<map>
#include<algorithm>
#define bug(a) cout << a << "*" << endl;
#define bugg(a, b) cout << a << " " << b  << "*" << endl;
#define buggg(a, b, c) cout << a << " " << b << " " << c << "*" << endl;
using namespace std;

typedef long long ll;
const int N = 2e7 + 5;
int prime[N], cnt = 0, i, j;
bool p[N] = {false};

void isprime(){
	p[0] = p[1] = 1;
	for(i = 2; i < N; ++i){
		if(!p[i])
			prime[cnt++] = i;
		for(j = 0; j < cnt; ++j){
			if(i * prime[j] > N)
				break;
			p[i * prime[j]] = true;
			if(i % prime[j] == 0)
				break;
		}
	}
}

void solve(){
	int x;
	scanf("%d", &x);
	if(x > 0 && !p[x]){
		printf("1\n");
		return ;
	}
	if(x > 0 && !p[x*2+1]){
		printf("2\n");
		return ;
	}
	if(x > 0 && !p[x*2-1]){
		printf("2\n");
		return ;
	}
	if(x < 0)
		x = -x;
	for(int i = x+1; ; ++i){
		if(!p[i]){
			printf("%d\n", (i-1)*2+2);
			break;
		}
		else{
			if(!p[i*2+1]){
				printf("%d\n", (i-1)*2+3);
				break;
			}
		}
	}
}

signed main(){
	isprime();
	int T;
	scanf("%d", &T);
	while(T--){
		solve();
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值