当前位置:网站首页>Xiao Sha's pain (thinking problem)
Xiao Sha's pain (thinking problem)
2022-06-30 15:01:00 【Rabbit doesn't like radish】
link :https://ac.nowcoder.com/acm/contest/27740/G
source : Cattle from

Correct code :
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int a[N];
int n,p;
int main()
{
scanf("%d%d",&n,&p);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
a[i]=a[i]%p;
}
sort(a,a+n);
int maxn=(a[n-1]+a[n-2])%p;// After the above treatment ,a[i] Each number represented is less than p Number of numbers ,
// And the sum of the last two numbers (a[n-1]+a[n-2]) Again %p, Will be the remainder of the ceiling , No one is older than him
int l=0,r=n-1;
while(l<r)
{
if(a[l]+a[r]>=p)
r--;
else
{
maxn=max(maxn,a[l]+a[r]);
l++;
}
}
printf("%d\n",maxn);
return 0;
}
Error timeout code :
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int a[N];
int n,p;
int main()
{
scanf("%d%d",&n,&p);
for(int i=0;i<n;i++)
{
cin>>a[i];
a[i]=a[i]%p;
}
sort(a,a+n);
long long minn=0;
for(int i=0;i<n;)
{
for(int j=n-1;j>i;)
{
long long mm=(long long)(a[i]+a[j])%p;
minn=max(minn,mm);
if(mm<p)
{
i++;
break;
}
else
{
j--;
}
}
}
printf("%lld\n",minn);
return 0;
}
边栏推荐
- 1133: output family and friends string
- @PathVariable
- Experiment 2: stack
- CCF numerical sorting (Full Score code + problem solving ideas + skill summary) 201503-2
- Solve the problem that codeblocks20.03 on win11 cannot run for the first time
- Sum of squares of two pointers
- Invalid argument during startup: Failed to open the . conf file: redis-window
- Text matching - [naacl 2021] augsbert
- K - rochambau (joint search, enumeration)
- PS cutting height 1px, Y-axis tiling background image problem
猜你喜欢
![[matlab] 2D drawing summary](/img/de/6bb5385f440a2997dbf9cbb9a756eb.jpg)
[matlab] 2D drawing summary

PS cutting height 1px, Y-axis tiling background image problem

CCF date calculation (Full Score code + skill summary) February 2, 2015

Not satisfied with markdown native code block style? Try this beautify code screenshot tool~~

Solve the problem that codeblocks20.03 on win11 cannot run for the first time

Repair of incorrect deletion of win10 boot entry

CCF command line options (Full Score code + problem solving ideas + skill summary) March 3, 2014

Knowledge learned from the water resources institute project

PS dynamic drawing

CCF access control system (Full Score code + problem solving idea) 201412-1
随机推荐
2021-08-05 leetcode notes
I - constructing roads
Sorting by character frequency
Non decreasing column
Location of dichotomy
Forward declaration of classes
Finding the median of two arrays by dichotomy
Invalid argument during startup: Failed to open the . conf file: redis-window
K - or unblocked project (minimum spanning tree)
Text matching - [naacl 2021] augsbert
[extensive reading of papers] multi modal sarcasm detection and human classification in code mixed conversations
[untitled]
Double pointer circular linked list
One dimensional and two dimensional array addresses
Double pointer letter matching
Hbuilder most commonly used and full shortcut key set
1062 talent and virtue (25 points)
Summary of C language interview questions
1019 general palindromic number (20 points)
1150 traveling salesman problem (25 points)