当前位置:网站首页>Acwing50+acwing51 weeks +acwing3493 Maximum sum (open)
Acwing50+acwing51 weeks +acwing3493 Maximum sum (open)
2022-06-11 12:10:00 【A little boat without confusion】
Acwing50:
The first question is :

Punch in once a week , The following uses the idea of hashing .
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
int a[N],n;
int main()
{
int num;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>num;
a[num]++;
}
for(int i=1;i<=n;i++)
{
if(!a[i]) cout<<i<<endl;
}
return 0;
}The second question is :

The time complexity of violent enumeration O(n*m) Time limit exceeded
Ideas : stay n Select a fixed interval and start the discussion by case , Suppose the second endpoint is on the right side of the interval , Then the left end point of the selected interval is closer to the right, the better ( For the first time, the left and right endpoints of the interval are fixed ). Similarly, suppose that the second endpoint is on the left of the interval , Then the right endpoint of the selected interval is closer to the left, the better .
It can be seen that , When looking for the second group of intervals, the two intervals are relatively fixed , That is, the selected interval of the second group will not be changed , So we can enumerate the first set of intervals
Here is the code :
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int INF=1e9;
int main()
{
int n,m;
cin>>n;
int a=INF,b=-INF;//a Represents the leftmost right endpoint of the first group of interspecific species ,b Represents the leftmost right endpoint
while (n -- )
{
int l,r;
scanf("%d%d", &l, &r);
a=min(a,r);
b=max(b,l);
}
// Select two intervals
int res=0;
cin>>m;
while (m -- )
{
int l,r;
scanf("%d%d", &l, &r);
if(b>r) res=max(res,b-r);
if(a<l) res=max(res,l-a);
}
// Judge the above situation
cout<<res;
return 0;
}Third question : Need to use dp, Not for the time being ;
Acwing51 Weekly game
The first question is :

Punch in question .
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
int n;
int p,q;
int res=0;
cin>>n;
while(n--)
{
cin>>p>>q;
if(q-p>=2) res++;
}
cout << res;
return 0;
}The second question is :

Go straight to the code , Here we use and look up sets , Pay attention to weight judgment later
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010,M=N*N;
int n,m;
int p[M],s[M];
char g[N][N];
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};// Offset
int get(int x,int y)// Each square corresponds to a unique integer
{
return x*m+y;
}
int find(int x) // Union checking set
{
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
int main()
{
scanf("%d%d", &n, &m);
for(int i=0;i<n;i++) scanf("%s",g[i]);// Read in map
for(int i=0;i<n*m;i++) p[i]=i,s[i]=1;// Initialize and query set
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)// Enumerate each element and its four directions
if(g[i][j]=='.')// Judge whether it is an open space
for(int k=0;k<4;k++)
{
int x=i+dx[k],y=j+dy[k];
if(x>=0&&x<n&&y>=0&&y<m&&g[x][y]=='.')// Judge whether you crossed the line
{
int a=get(i,j),b=get(x,y);
a=find(a),b=find(b);
if(a!=b)
{
s[b]+=s[a];
p[a]=b;
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
if(g[i][j]=='.') printf(".");// Judge the four surrounding elements , And sentenced to heavy
else
{
int father[4],cnt=0;
for(int k=0;k<4;k++)
{
int x=i+dx[k],y=j+dy[k];
if(x>=0&&x<n&&y>=0&&y<m&&g[x][y]=='.')
{
int a=get(x,y);
father[cnt++]=find(a);
}
}
int sum=1;
if(cnt)// Start weight judgment
{
sort(father,father+cnt);
cnt=unique(father,father+cnt)-father;
for(int k=0;k<cnt;k++)
sum+=s[father[k]];
}
printf("%d",sum%10);
}
puts("");
}
return 0;
}
Next is 3493

First explain the meaning of the topic , Select an integer sequence with a length of k The range of , Replace the non optional number with the optional number , Plus optional outside the interval , Then sum up . So the title becomes , Optional + What changes in the interval is the answer .
There are two solutions to this problem , Let's start with the first , Double pointer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// Remember to write ll, Or it will explode
const int N=100010;
int a[N],b[N];//a Used for storing numbers ,b For storage status
int main(){
int n,k;
cin>>n>>k;
ll sum=0,v=0,s=0;//sum Is the sum of optional numbers ,v Is the maximum sum after changing the state in the window ,s Is the sum of the current interval
for(int i=0;i<n;i++) scanf("%d",&a[i]);// Input a
for(int i=0;i<n;i++){
scanf("%d",&b[i]);// Input b
if(b[i]) sum+=a[i];// Calculation sum
}
for(int i=0;i<n;i++){
if(b[i]==0) s+=a[i];// If the number status is 0 That is, not optional , Then its state changes and
if(i>=k&&b[i-k]==0) s-=a[i-k];// When i Greater than or equal to k when , The window starts sliding to the right , Each slide minus the left state is 0 Number of numbers
v=max(v,s);// Window maximum and
}
printf("%lld",sum+v);
return 0;
}Then the prefix and
#include<iostream>
#include<algorithm>
using namespace std;
const int N= 1000010;
typedef long long ll;
ll a[N],s[N],ans;
int n,k;
int main()
{
cin>>n>>k;
for(int i=1;i<=n;i++)
cin>>a[i];
int state=0;
for(int i=1;i<=n;i++)
{
cin>>state;// Prefix and operation , If the status is optional, add , And change to not optional
if(state)
{
ans+=a[i];
a[i] = 0;
}
s[i] = s[i-1]+a[i];
}
ll res = 0;
// Enumerate the left endpoint of each sliding window
for(int i=1;i<=n;i++)
{
int r = i+k-1;
if(r>n) r=n;
ll now = s[r]-s[i-1];
if(now>res) res = now;
}
cout<<ans+res;
return 0;
}
There are some problems in this solution , I'm not satisfied after writing , I'll make it up later .
边栏推荐
- (解决)Splunk 之 kv-store down 问题
- 刷题笔记(十三)--二叉树:前中后序遍历(复习)
- Zhouhongyi's speech at the China Network Security Annual Conference: 360 secure brain builds a data security system
- 01_ Description object_ Class diagram
- Notes on topic brushing (XIV) -- binary tree: sequence traversal and DFS, BFS
- gocron 定时任务管理平台
- Software project management 7.1 Basic concept of project schedule
- Notes on brushing questions (13) -- binary tree: traversal of the first, middle and last order (review)
- POJ 3278 catch the cow (width first search, queue implementation)
- JVM优化
猜你喜欢

Splunk 健康检查之关闭THP

Use of Chinese input method input event composition

反射真的很耗时吗,反射 10 万次,耗时多久。

flink 控制窗口行为(触发器、移除器、允许延迟、将迟到的数据放入侧输出流)

JVM优化
Use cache to reduce network requests

Iframe value transfer

flink 数据流图、并行度、算子链、JobGraph与ExecutionGraph、任务和任务槽

Is reflection really time-consuming? How long does it take to reflect 100000 times.
![[JUC supplementary] immutable object, shared meta mode, final principle](/img/c1/c29229108a3f66b83d13b4d90d49f7.jpg)
[JUC supplementary] immutable object, shared meta mode, final principle
随机推荐
Is reflection really time-consuming? How long does it take to reflect 100000 times.
flink 多流转换(侧输出流分流、Union、Connect) 实时对账app 的支付操作和第三方的支付操作的双流 Join
Gestion de projets logiciels 7.1. Concept de base du calendrier du projet
The secret behind the Splunk bucket
Jest unit test description config json
Uncaught typeerror: cannot set property 'next' of undefined
What is a Gerber file? Introduction to PCB Gerber file
读取geo表达矩阵
你管这破玩意儿叫 MQ?
JMeter 学习心得
Thread five states (thread life cycle)
微信web开发者,如何学习web开发
arguments.callee 实现函数递归调用
记一次codis内存清理
纯数据业务的机器打电话进来时回落到了2G/3G
typescript 编译选项和配置文件
Take you to know about direct insertion sorting (C language)
C reads TXT file to generate word document
JS 加法乘法错误解决 number-precision
centos安装mysql5.7