当前位置:网站首页>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;
}边栏推荐
- Variables (automatic variables, static variables, register variables, external variables) and memory allocation of C malloc/free, calloc/realloc
- CCF CSP 202109-1数组推导
- TcaplusDB君 · 行业新闻汇编(六)
- 罗永浩:我要是负责人 能让苹果产品上去三个台阶不止
- [MySQL] summary of common data types
- A number that appears only once in an array
- [MySQL] Table constraints
- Array to sum the heights of rising intervals
- [applet] the vant wearp radio radio radio component cannot trigger the bind:change event
- How to do well in the top-level design of informatization in the process of informatization upgrading of traditional enterprises
猜你喜欢
![[tcapulusdb knowledge base] tcapulusdb transaction management introduction](/img/cd/dc23cb8bddcbf04efeaf60dfadaec9.png)
[tcapulusdb knowledge base] tcapulusdb transaction management introduction
![[MySQL] Table constraints](/img/13/b97679cd4ca36d4b8e19acf23df404.png)
[MySQL] Table constraints

【TcaplusDB知识库】TcaplusDB巡检统计介绍

【TcaplusDB知识库】TcaplusDB查看进程所在机器介绍

【TcaplusDB知识库】TcaplusDB刷新tbus通道介绍
![[tcapulusdb knowledge base] tcapulusdb machine initialization and launch introduction](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[tcapulusdb knowledge base] tcapulusdb machine initialization and launch introduction

【TcaplusDB知识库】TcaplusDB TcapDB扩缩容介绍

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

(11) Tableview

鲸会务智慧景区管理解决方案
随机推荐
Record (III)
[tcapulusdb knowledge base] tcapulusdb transaction management introduction
笔记(二)
笔记(五)- JVM
torch_geometric
《暗黑破坏神不朽》数据库资料站地址 暗黑不朽资料库网址
[py] the failure of interface signature verification may be due to ensure_ ASCII problems
修改SpriteMask 的 frontSortingLayer 变量
中银证券开户安全吗?它和中国银行是什么关系呢?
[tcapulusdb knowledge base] Introduction to tcapulusdb process startup
鲸会务会议分享:大会难办怎么办?
(11) Tableview
torch_ geometric
【TcaplusDB知识库】TcaplusDB TcapDB扩缩容介绍
[generation confrontation network learning part I] classic Gan and its existing problems and related improvements
【TcaplusDB知识库】TcaplusDB TcapProxy扩缩容介绍
[tcapulusdb knowledge base] Introduction to tcapulusdb push configuration
Diablo immortality database station address Diablo immortality database website
C use s7 Net connected to Siemens s1200plc, C # directly connected to Siemens PLC
Pytorch 安装超简单