当前位置:网站首页>Logarithmic calculation in reverse order, logarithmic calculation in sequence -- merge sort
Logarithmic calculation in reverse order, logarithmic calculation in sequence -- merge sort
2022-06-29 02:48:00 【Madness makes freedom】
/ If this number of the second sequence ( First number not compared ,
// Because the sequence is already ordered , This number must be the smallest number in the second sequence that has not been compared ,)
// Smaller than any number in the first sequence that has not been compared , Then all the numbers of the first sequence that have not been compared can be compared with
// This number of the second segment of the sequence forms an inverse pair
void my_merge(int *a,int l1,int r1,int l2,int r2)// Two ordered sequences , Merge into an ordered sequence
{
int temp[r2-l1+1],index=0,i=l1,j=l2;
while(i<=r1&&j<=r2)
{
if(a[i]<=a[j])
temp[index++]=a[i++];
else
{
temp[index++]=a[j++];
res+=r1-i+1; // If this number of the second sequence ( First number not compared ,
// Because the sequence is already ordered , This number must be the smallest number in the second sequence that has not been compared ,)
// Smaller than any number in the first sequence that has not been compared , Then all the numbers of the first sequence that have not been compared can be compared with
// This number of the second segment of the sequence forms an inverse pair
}
}while(i<=r1)
temp[index++]=a[i++];
while(j<=r2)
temp[index++]=a[j++];
for(i=0;i<index;++i)
a[l1+i]=temp[i];
}
Complete code :
/**
Find the reverse logarithm with merge sort
*/
/**
Merge sort
tow point : Combine two ordered sequences into one ,O(n)
*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;
int res=0;
void my_merge(int *a,int l1,int r1,int l2,int r2)// Two ordered sequences , Merge into an ordered sequence
{
int temp[r2-l1+1],index=0,i=l1,j=l2;
while(i<=r1&&j<=r2)
{
if(a[i]<=a[j])
temp[index++]=a[i++];
else
{
temp[index++]=a[j++];
res+=r1-i+1;
}
}
while(i<=r1)
temp[index++]=a[i++];
while(j<=r2)
temp[index++]=a[j++];
for(i=0;i<index;++i)
a[l1+i]=temp[i];
}
void mergesort(int *a,int l,int r)
{
if(l>=r)
return;
else
{
int mid=l+(r-l)/2;
mergesort(a,l,mid);
mergesort(a,mid+1,r);
my_merge(a,l,mid,mid+1,r);
}
}
int main()
{
int n;
cin >> n;
int a[n];
for(int i=0;i<n;++i)
cin >> a[i];
int sum=0;
for(int i=0;i<n;++i)
for(int j=i+1;j<n;++j)
if(a[i]>a[j])// Enumeration algorithm
++sum;
printf("sum: %d\n",sum );
mergesort(a,0,n);
for(auto b: a)
cout << b << ' ';
cout << endl;
printf("%d\n",res);
cout << "Hello world!" << endl;
return 0;
}Now that the reverse order pairs have been calculated , So the order is different , Sequence pair definition :dp[i] < dp[j] (i<j);
Here is a topic : In reverse order , Calculate each one in sequence :
Description
Given a number string , The length of the string is nnn , Now define the sum of each number of a substring as the sum of the numbers of the substring , Please find out how many substrings in the string are positive .
Input
Number one on the first line nnn , Indicates the length of the string . The second line is a total of nnn Number , It means that each number in the string outputs one number , The sum of the strings indicating the number of substrings in the string is a positive number .
Output
A positive number is the answer .
Sample Input 1
3
8 -9 2Sample Output 1
3Hint
100% nnn ≤ 100000 The sum of all numbers is int Within limits
analysis : The problem of finding a string of numbers in reverse order , Find the suffix sum of the sequence , To get a positive sum of substrings , namely
dp[i]>dp[j](i<j), That is, find the logarithm of the reverse pair in a sequence
/**
The problem of finding a string of numbers in reverse order , Find the suffix sum of the sequence , To get a positive sum of substrings , namely
dp[i]>dp[j](i<j), That is, find the logarithm of the reverse pair in a sequence
*/
/**
#include <iostream>
using namespace std;
long long res=0; // here res Yes, it needs to be defined as long long, Otherwise, the answer will be wrong
void my_merge(int *a,int l1,int r1,int l2,int r2)// Two ordered sequences , Merge into an ordered sequence
{
int temp[r2-l1+1],index=0,i=l1,j=l2;
while(i<=r1&&j<=r2)
{
if(a[i]<=a[j])
temp[index++]=a[i++];
else
{
temp[index++]=a[j++];
res+=r1-i+1; // If this number of the second sequence ( First number not compared ,
// Because the sequence is already ordered , This number must be the smallest number in the second sequence that has not been compared ,)
// Smaller than any number in the first sequence that has not been compared , Then all the numbers of the first sequence that have not been compared can be compared with
// This number of the second segment of the sequence forms an inverse pair
}
}
while(i<=r1)
temp[index++]=a[i++];
while(j<=r2)
temp[index++]=a[j++];
for(i=0;i<index;++i)
a[l1+i]=temp[i];
}
void mergesort(int *a,int l,int r)
{
if(l>=r)
return;
else
{
int mid=l+(r-l)/2;
mergesort(a,l,mid);
mergesort(a,mid+1,r);
my_merge(a,l,mid,mid+1,r);
}
}
int main()
{
int n;
scanf("%d",&n);
int a[n+1],dp[n+2],val;
dp[n+1]=0;
for(int i=1;i<=n;++i)
{
scanf("%d",&a[i]);
}
for(int i=n;i>0;--i)
dp[i]=dp[i+1]+a[i]; //dp Denotes suffixes and
mergesort(dp,1,n+1);// The parameter here is n+1, because dp[n]-dp[n+1] It means a[i] The value of this number ,
// But the parameters you pass are n Words , The last number is removed
printf("%lld\n",res);
return 0;
}
*/ So can we use the prefix sum to calculate ?
Prefix and calculation i.e dp[i] < dp[j] (i<j), That is, find the number of sequence pairs in a sequence ,
Also use recursive sorting to find
/**
So can we use the prefix sum to calculate ?
Prefix and calculation i.e dp[i] < dp[j] (i<j), That is, find the number of sequence pairs in a sequence ,
Also use recursive sorting to find
*/
#include <iostream>
using namespace std;
long long res=0; // here res Yes, it needs to be defined as long long, Otherwise, the answer will be wrong
void my_merge(int *a,int l1,int r1,int l2,int r2)// Two ordered sequences , Merge into an ordered sequence
{
int temp[r2-l1+1],index=0,i=l1,j=l2;
while(i<=r1&&j<=r2)
{
if(a[i]<a[j])
{
temp[index++]=a[i++];
res+=r2-j+1; // If this number of the first sequence ( First number not compared ,
// Because the sequence is already ordered , This number must be the smallest number in the first sequence that has not been compared ,)
// Smaller than any number of the second sequence that has not been compared , Then all the numbers of the second sequence that have not been compared can be compared with
} // This number of the first sequence forms a sequence pair
else
temp[index++]=a[j++];
}
while(i<=r1)
temp[index++]=a[i++];
while(j<=r2)
temp[index++]=a[j++];
for(i=0;i<index;++i)
a[l1+i]=temp[i];
}
void mergesort(int *a,int l,int r)
{
if(l>=r)
return;
else
{
int mid=l+(r-l)/2;
mergesort(a,l,mid);
mergesort(a,mid+1,r);
my_merge(a,l,mid,mid+1,r);
}
}
int main()
{
int n;
scanf("%d",&n);
int dp[n+1],val;
dp[0]=0;
for(int i=1;i<=n;++i)
{
scanf("%d",&val);
dp[i]=dp[i-1]+val; //dp Represents the prefix and of a sequence of numbers
}
mergesort(dp,0,n);/** The second parameter here is 0, because dp[1]-dp[0] It means a[1] The value of this number ,
But the parameters you pass are 1 Words , The first number is removed */
printf("%lld\n",res);
return 0;
}
边栏推荐
猜你喜欢

PWN attack and defense world level2
![[线性代数] 1.1 二阶与三阶行列式](/img/ea/70b59c64d3287a887e371a9181fe45.png)
[线性代数] 1.1 二阶与三阶行列式

Concise words tell about technical people who must master basic IT knowledge and skills. Part 1

矩阵特征值和特征向量求解——特征值分解(EVD)

LabVIEW generate application (exe) and installer

The thinkphp5.1 runtime file has been changed to 777 permission, but cannot be written

Wechat applet custom component

"The first share of endoscope" broke into IPO two times. Last year, it lost 500million yuan. The commercialization of core products is still in doubt | IPO Express

SQL continuous login problem

Redis master-slave replication
随机推荐
Ctfhub web password default password
PWN新手入门Level0
[linear algebra] 1.1 second and third order determinants
China's flexible employment has reached 200million
PWN攻防世界Level2
"The first share of endoscope" broke into IPO two times. Last year, it lost 500million yuan. The commercialization of core products is still in doubt | IPO Express
18. `bs object Node name next_ Sibling` get sibling nodes
String segment combination
PWN攻防世界guess_num
层次分析法(AHP)
Have you learned the common SQL interview questions on the short video platform?
Table implements alternative adaptation through pseudo classes
pvcreate asm disk导致asm磁盘组异常恢复---惜分飞
99 multiplication table
對補wasm環境的一些測試
Regular expression (?: pattern)
PHP system function
PHP database ODBC
[issue 259] uncover how count is executed in MySQL?
Understanding and design of high concurrency
