当前位置:网站首页>CCF CSP 202109-2 non zero segment division [100 points]
CCF CSP 202109-2 non zero segment division [100 points]
2022-06-10 22:36:00 【Brienzz】
C++70 Code division
#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 Upper limit of value range of
// 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); // Judge and choose p Post new array ss Number of non-zero segments
cnt=max(geshu,cnt); // Count the non-zero segments of the new array ss Compare with the maximum number of non-zero segments of the array
}
cout << cnt;
return 0;
}
Java70 Code division
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 branch
#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;
}How to optimize ?1、 Space for time ;2、 Optimization of processing logic , Such as reducing some repetitive operations , Use more historical information .
#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;
}边栏推荐
- GMPNN:Drug-drug interaction prediction with learnable size-adaptive molecular substructures.
- TcaplusDB君 · 行业新闻汇编(四)
- [tcapulusdb knowledge base] tcapulusdb daily inspection introduction
- Add, delete, query and modify [MySQL] table data (DML)
- 记录(二)
- Model construction of mmdetection
- (11) Tableview
- 1.Tornado简介&&本专栏搭建tornado项目简介
- Web3生态去中心化金融平台——Sealem Finance
- How small and micro enterprises build micro official websites at low cost
猜你喜欢
![[debug] could not find ref wiht POC XXX](/img/01/5fdbd3e9902458be754280464bd923.png)
[debug] could not find ref wiht POC XXX

鲸会务智慧景区管理解决方案

【TcaplusDB知识库】TcaplusDB事务管理介绍

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

How small and micro enterprises build micro official websites at low cost

What are MySQL clustered indexes and nonclustered indexes?

Sealem Finance打造Web3去中心化金融平台基础设施
![[tcapulusdb knowledge base] tcapulusdb transaction management introduction](/img/cd/dc23cb8bddcbf04efeaf60dfadaec9.png)
[tcapulusdb knowledge base] tcapulusdb transaction management introduction

【TcaplusDB知识库】TcaplusDB查看线上运行情况介绍

Latex error: file ‘xxx. sty‘ not found
随机推荐
Bitwise and shift operators
[debug] could not find ref wiht POC XXX
Tcapulusdb Jun · industry news collection (III)
GMPNN:Drug-drug interaction prediction with learnable size-adaptive molecular substructures.
leetcode:333. 最大 BST 子树
鯨會務智慧景區管理解决方案
C use s7 Net connected to Siemens s1200plc, C # directly connected to Siemens PLC
A number that appears only once in an array
[tcapulusdb knowledge base] Introduction to tcapulusdb process startup
QT custom delegation
[generation confrontation network learning part I] classic Gan and its existing problems and related improvements
[tcapulusdb knowledge base] tcapulusdb daily inspection introduction
[tcapulusdb knowledge base] Introduction to tcapulusdb push configuration
记录(三)
leetcode 130. Surrounded Regions 被围绕的区域(中等)
[tcapulusdb knowledge base] Introduction to the machine where the tcapulusdb viewing process is located
String search in C
Whale conference empowers intelligent epidemic prevention
[tcapulusdb knowledge base] Introduction to tcapulusdb patrol inspection statistics
笔记(四)- 多线程