当前位置:网站首页>CCF CSP 202109-2 非零段划分【100分】
CCF CSP 202109-2 非零段划分【100分】
2022-06-10 21:21:00 【Brienzz】
C++70分代码
#include <iostream>
using namespace std;
const int N = 1e6 + 10;
int arr[N],n;
int ss[N];
int judge(int num[], int n) {
int cnt = 0;
for (int i = 0; i < n; i++) {
if (num[i] != 0) {
cnt++;
while (num[i] != 0) i++;
}
}
return cnt;
}
int maxn(int num[], int n) {
int maxn = -1;
for (int i = 0; i < n; i++) {
if (num[i] > maxn) maxn = num[i];
}
return maxn;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int maxnn = maxn(arr, n); //p的取值范围上限
// cout<<"maxnn ="<<maxnn<<endl;
int cnt = 0,geshu=0;
for (int p = 1; p <= maxnn; p++) {
for (int j = 0; j < n; j++) {
if (arr[j] < p) ss[j] = 0;
else ss[j] = arr[j];
}
geshu=judge(ss,n); //判断选取p后新数组ss的非零段个数
cnt=max(geshu,cnt); //将新数组非零段个数ss和之前得到的数组最大非零段个数比较
}
cout << cnt;
return 0;
}
Java70分代码
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
public class Main {
static int[] a = new int[10000+5];
static int[] temp = new int[10000+5];
public static void main(String[] args) {
HashSet<Integer> set = new HashSet<>();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
a[i] = sc.nextInt();
set.add(a[i]);
}
Iterator<Integer> iterator = set.iterator();
int max = 0;
while (iterator.hasNext()){
Integer p = iterator.next();
for (int i = 1; i <= n; i++) {
if(a[i]<p) temp[i] = 0;
else temp[i] = a[i];
}
int fld = FLD(temp);
max = Math.max(max,fld);
}
System.out.println(max);
}
private static int FLD(int[] temp) {
int flag = 1,count = 0;
for (int i = 1; i < temp.length; i++) {
if(temp[i]!=0 && flag==1){
count++;
flag = 0;
}
if(temp[i]==0) flag = 1;
}
return count;
}
}
C++70分
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
int A[500001];
int NZnum(int n){
int NZ = 0;
for(int i=1;i<=n;i++){
if(A[i]>0 && A[i-1]==0) NZ++;
}
return NZ;
}
void PA(int p,int n){
for(int i=1;i<=n;i++){
if(A[i]<p) A[i] = 0;
}
}
int main() {
int n;
int NZmax;
A[0] = 0;
cin>>n;
set<int> s;
for(int i=1;i<=n;i++){
cin>>A[i];
s.insert(A[i]);
}
int temp = 0;
NZmax = 0;
int p;
set<int>::iterator it1 = s.begin();
while(it1!=s.end()){
p = *it1;
PA(p,n);
temp = NZnum(n);
if(NZmax<temp) NZmax = temp;
++it1;
}
cout<<NZmax<<endl;
return 0;
}如何优化?1、空间换时间;2、处理逻辑的优化,如减少一些重复的操作,多利用一些历史的信息。
#include<iostream>
#include<algorithm>
#include<set>
#include<vector>
using namespace std;
int A[500002];
vector<int> pos[10001];
int main() {
int n;
A[0] = 0;
cin>>n;
set<int> s;
for(int i=1;i<=n;i++){
cin>>A[i];
s.insert(A[i]);
pos[A[i]].push_back(i);
}
A[n+1] = 0;
int NZ = 0;
for(int i=1;i<=n;i++){
if(A[i]>0 && A[i-1]==0) NZ++;
}
int temp;
int NZmax;
temp = NZmax = NZ;
int p;
set<int>::iterator it1 = s.begin();
if(*it1==0) ++it1;
while(it1!=s.end()){
vector<int> &cp = pos[*it1];
for(int i=0;i<cp.size();i++){
int idx = cp[i];
A[idx] = 0;
if((A[idx-1]!=0)&&(A[idx+1]!=0)) temp++;
if((A[idx-1]==0)&&(A[idx+1]==0)) temp--;
}
++it1;
NZmax = max(NZmax,temp);
}
cout<<NZmax<<endl;
return 0;
}边栏推荐
- [debug] could not find ref wiht POC XXX
- Notes (IV) - multithreading
- 中小型会议如何进行数字化升级?
- 【TcaplusDB知识库】TcaplusDB日常巡检介绍
- Sealem Finance打造Web3去中心化金融平台基础设施
- Model construction of mmdetection
- Kdd2022 | neural network compression of depth map based on antagonistic knowledge distillation
- Notes (V) - JVM
- Add, delete, query and modify [MySQL] table data (DML)
- 数组 删除数组中的重复项
猜你喜欢

Sealem finance builds Web3 decentralized financial platform infrastructure

Solution de gestion de la zone pittoresque intelligente pour la réunion des baleines

oc swift 混编

Differences between disk serial number, disk ID and volume serial number

Array union set

TcaplusDB君 · 行业新闻汇编(五)

【MySQL】表的约束

【TcaplusDB知识库】TcaplusDB刷新tbus通道介绍

Icml2022 | sharp maml: model independent meta learning for sharpness perception
![[tcapulusdb knowledge base] tcapulusdb refresh tbus channel introduction](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[tcapulusdb knowledge base] tcapulusdb refresh tbus channel introduction
随机推荐
Business based precipitation component = & gt; manage-table
笔记(五)- JVM
KDD2022 | 基于对抗性知识蒸馏的深度图神经网络压缩
数组 求上升区间的高度和
Array union set
【TcaplusDB知识库】TcaplusDB查看进程状态介绍
【phpstorm】 No data sources are configured to run this SQL and provide advanced c
Apache相关的几个安全漏洞修复
To do desktop plug-in, a good helper for office workers
【TcaplusDB知识库】TcaplusDB TcapDB扩缩容介绍
C language - quick sorting in sorting
Mysql的回表查询?如何避免?
[MySQL] Table constraints
数组 删除数组中的重复项
How to stimulate the vitality and driving force of cultural innovation
自己做了个相亲交友App,有兴趣的朋友可以看看
GMPNN:Drug-drug interaction prediction with learnable size-adaptive molecular substructures.
19 MySQL optimizations commonly used in projects
IDEA出现“XXX has broken path”报错解决方法
Array plus one