当前位置:网站首页>Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2) - B, C, D
Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2) - B, C, D
2022-07-18 15:21:00 【Dimple】
D. Restore Permutation
The question
Suppose there is a full permutation p [ ] p[] p[].
Given length is n n n Sequence s [ ] s[] s[], among s i s_i si by : p [ ] p[] p[] in , The first i i i Than before p i p_i pi All small elements p j p_j pj The sum of .
requirement Restore the full arrangement p [ ] p[] p[] And the output .
The original full permutation is unique , 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^{5} 1≤n≤2⋅105.
Ideas
I thought so at first :
First, the last occurrence in the sequence 0 The position of must be in full arrangement 1 The position of .1 The position of is determined .
Because the rear position is smaller than 1 Large full array elements s[i] It's all added 1, therefore :
Put the number on all positions after this position s[i] All minus the current number 1, The last time 0 The position of must be in full arrangement 2 The position of .
Put the number on all positions after this position s[i] All minus the current number 2, The last time 0 The position of must be in full arrangement 3 The position of .
…
In this way, the original arrangement can be restored .
however , The time complexity of this idea is N^2.
Subtract a value from all the elements in the following interval , And save it and subtract it to become 0 The location of , I didn't expect any algorithm to be optimized , So let's go .
The positive solution is like this :
Add the total permutation number All unfilled numbers Put it in a collection .
Consider each position from back to front , Then all the numbers in the set will be filled in 1~ Go to the current position . So if the current position is filled x, Then the ratio in the set x Small numbers must be in front , So the current s[i] If the sum of these numbers .
So according to the current location s[i] Then we can find the set neutralization as s[i] The minimum values of , Then the first number in the set that is larger than these values is p[i]. find p[i] Then delete it from the set , Ensure that there are unselected numbers in the set .
That is to say , Find the first satisfaction from the set every time The sum of all elements in the set less than or equal to this value is greater than s[i] The elements of , Then delete it from the set .
You can use tree arrays to maintain prefixes and , Each query logn, Delete logn.
So the time complexity is O(nlogn).
Code
#include<bits/stdc++.h>
using namespace std;
#define Ios ios::sync_with_stdio(false),cin.tie(0)
const int N = 200010, mod = 1e9+7;
int T, n, m;
int a[N], c[N];
int ans[N];
int lbit(int x){
return x & -x;
}
void add(int x, int y)
{
for(int i=x;i<=n;i+=lbit(i)){
c[i] += y;
}
}
int query(int x){
int sum = 0;
for(int i=x;i>=1;i-=lbit(i)) sum += c[i];
return sum;
}
bool check(int mid, int x)
{
if(query(mid) > x) return 1;
return 0;
}
signed main(){
Ios;
cin >> n;
for(int i=1;i<=n;i++) cin >> a[i];
for(int i=1;i<=n;i++) add(i, i);
for(int i=n;i>=1;i--)
{
int l = 1, r = n;
while(l < r)
{
int mid = l+r>>1;
if(check(mid, a[i])) r = mid;
else l = mid+1;
}
ans[i] = l;
add(l, -l);
}
for(int i=1;i<=n;i++) cout << ans[i] << " ";
return 0;
}
If you change the meaning of the topic , say :
Give the result of finding the reverse chronology c [ ] c[] c[] Array , c i c_i ci Indicates the position in the full arrangement i i i All the following are better than p i p_i pi The number of small positions .
ask , What is the original full arrangement ?
The same is true , Just walk from front to back , Then in the tree array , If a number is in the set, set it to 1, Otherwise, it is modified to 0.
#include<bits/stdc++.h>
using namespace std;
#define Ios ios::sync_with_stdio(false),cin.tie(0)
const int N = 200010, mod = 1e9+7;
int T, n, m;
int a[N], c[N];
int ans[N];
int lbit(int x){
return x & -x;
}
void add(int x, int y)
{
for(int i=x;i<=n;i+=lbit(i)){
c[i] += y;
}
}
int query(int x){
int sum = 0;
for(int i=x;i>=1;i-=lbit(i)) sum += c[i];
return sum;
}
bool check(int mid, int x)
{
if(query(mid) > x) return 1;
return 0;
}
signed main(){
Ios;
cin >> n;
for(int i=1;i<=n;i++) cin >> a[i];
for(int i=1;i<=n;i++) add(i, 1);
for(int i=1;i<=n;i++)
{
int l = 1, r = n;
while(l < r)
{
int mid = l+r>>1;
if(check(mid, a[i])) r = mid;
else l = mid+1;
}
ans[i] = l;
add(l, -1);
}
for(int i=1;i<=n;i++) cout << ans[i] << " ";
return 0;
}
Experience
I will encounter such a full array of problems in the future , You can think of putting all the undetermined numbers into a set .
For a given sequence , From front to back or from back to front , Each time you choose from this set , Delete after confirmation .
C. Magic Grid
The question
Construct a n*n Matrix , Satisfy the XOR and of each line , The XOR and of each column are the same .
4 ≤ n ≤ 1000 , by 4 Of times Count 4 \leq n \leq 1000, by 4 Multiple 4≤n≤1000, by 4 Of times Count
Ideas
Consider special circumstances : 0 0 0
XOR property : continuity 4 The XOR value of the number is 0.
Then construct in sequence 4 ∗ 4 4*4 4∗4 After the matrix, it is found that the XOR sum of each column is also 0, But sequential construction is important for 12 ∗ 12 12*12 12∗12, 20 ∗ 20 20*20 20∗20 I'm not satisfied .
So for x , x + 4 , x + 8 , x + 12 x, x+4, x+8, x+12 x,x+4,x+8,x+12 The XOR value of such four numbers is also 0, Try to construct such data .
So you can put n ∗ n n*n n∗n The matrix of is divided into several 4 ∗ 4 4*4 4∗4 Matrix , Take the difference between two adjacent rows of each column as 4.
For simplicity , It can be structured like this : Every time 4 Columns are treated as part , Each part is constructed in sequence .
8
0 1 2 3 | 32 33 34 35
4 5 6 7 | 36 37 38 39
8 9 10 11 | 40 41 42 43
12 13 14 15 | 44 45 46 47
16 17 18 19 | 48 49 50 51
20 21 22 23 | 52 53 54 55
24 25 26 27 | 56 57 58 59
28 29 30 31 | 60 61 62 63
set up x = n/4,
The first i Xing di j The number of columns is :4*(i-1) + j-1 + ((j-1)/4)*16*x - (j-1)/4*4.
Code:
#include<bits/stdc++.h>
using namespace std;
#define Ios ios::sync_with_stdio(false),cin.tie(0)
const int N = 2010, mod = 1e9+7;
int T, n, m;
int a[N][N];
signed main(){
Ios;
cin >> n;
int x = n/4;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
a[i][j] = 4*(i-1) + j-1 + ((j-1)/4)*16*x - (j-1)/4*4;
cout << a[i][j] << " ";
}
cout << endl;
}
// for(int j=1;j<=n;j++)
// {
// int x = 0;
// for(int i=1;i<=n;i++)
// {
// x ^= a[i][j];
// }
// cout << x << endl;
// }
return 0;
}
B. Uniqueness
The question
Given a length n n n Sequence of numbers , To delete a continuous interval so that the numbers in the sequence are different .
ask , What is the minimum length of the deleted interval ?
1 ≤ n ≤ 2000 , 1 ≤ a i ≤ 1 0 9 1 \le n \le 2000,\ 1 \le a_{i} \le 10^{9} 1≤n≤2000, 1≤ai≤109
Ideas
Method 1:
Half length , then n^2 Traversal position .
use map Mark , Overtime .
Change it to sort Post traversal judgment , O ( n 2 l o g n ) O(n^2logn) O(n2logn).
Method 2:
Traverse every position , Then traverse the length extending back from this position , If you extend to a position and find that there are no duplicates , Then stop .
The length and answer are min.
The key is how to judge whether there is no repetition number when extending to a position ?
Use all the numbers map Mark , Record the number of duplicates sum.
Go to a position , If the current number x There are numbers in the whole sequence mp[x] Not for 1, This indicates that the number is repeated , Delete the number , The number of repetitions of this number mp[x]-1, Number of repetitions sum-1.
If sum Reduced to 0 了 , It means that there is no repetition in the whole sequence , Then you can break, The current length and answer are min.
Time complexity O ( n 2 ) + n 2 Time m p fuck do Consumption when O(n^2) + n^2\ Time \ mp\ Operating time O(n2)+n2 Time mp fuck do Consumption when .
Code
Method 1:
#include<bits/stdc++.h>
using namespace std;
#define Ios ios::sync_with_stdio(false),cin.tie(0)
const int N = 200010, mod = 1e9+7;
int T, n, m;
int a[N], t[N];
bool check(int mid)
{
for(int i=1;i<=n-mid+1;i++)
{
int cnt = 0, flag = 0;
for(int j=1;j<i;j++){
t[++cnt] = a[j];
}
for(int j=i+mid;j<=n;j++)
{
t[++cnt] = a[j];
}
sort(t+1, t+cnt+1);
for(int j=2;j<=cnt;j++){
if(t[j] == t[j-1]) flag = 1;
}
if(!flag) return 1;
}
return 0;
}
signed main(){
Ios;
cin >> n;
for(int i=1;i<=n;i++) cin >> a[i];
int l = 0, r = n;
while(l < r)
{
int mid = l+r>>1;
if(check(mid)) r = mid;
else l = mid+1;
}
cout << l;
return 0;
}
Method 2:
#include<bits/stdc++.h>
using namespace std;
#define Ios ios::sync_with_stdio(false),cin.tie(0)
map<int,int> tmp, mp;
const int N = 200010, mod = 1e9+7;
int T, n, m;
int a[N];
signed main(){
Ios;
cin >> n;
for(int i=1;i<=n;i++) cin >> a[i];
int sum = 0;
for(int i=1;i<=n;i++)
{
tmp[a[i]] ++;
if(tmp[a[i]] != 1) sum ++;
}
int ans = 1e9;
for(int i=1;i<=n;i++)
{
mp = tmp;
int t = sum;
for(int j=i;j<=n;j++)
{
if(mp[a[j]] != 1){
mp[a[j]] --;
t --;
if(t == 0){
ans = min(ans, j-i+1);
break;
}
}
}
}
if(ans != 1e9) cout << ans;
else cout << 0;
return 0;
}
边栏推荐
- Address problem when Xilinx FPGA starts configuration data from SPI flash
- 基于NvidiaGPU的AI模型结构优化
- RGB图像上的密文--违规数据隐藏
- JVM memory model
- Go environment installation
- Comprehensive evaluation method
- Go 原生插件使用问题全解析
- STM32 application development practice tutorial: multi computer communication application development based on RS-485 bus
- Mipi CSI, DSI, UFS, c-phy, d-phy, m-phy concept understanding
- RMAN详细教程(二) —— 备份、检查、维护、恢复
猜你喜欢

Operation of simulated examination platform for 2022 G2 boiler stoker examination

(cvpr-2022) Lagrangian motion analysis and perspective embedding for improved gait recognition
![[communication] [1] amplitude modulation, frequency modulation, double sideband and single sideband, IQ and PSK and QAM - must sampling meet Nyquist theorem](/img/c1/f54d5d5cd4cfd64e8c32751ec0923d.jpg)
[communication] [1] amplitude modulation, frequency modulation, double sideband and single sideband, IQ and PSK and QAM - must sampling meet Nyquist theorem

Jinglianwen technology data acquisition company: strictly control the construction period and deliver high-quality data for AI enterprises

Boyun was selected as the representative manufacturer of Gartner China cloud management tool market guide

Establishment of APP automated test framework (VII) -- airtest basic operation

Vulnhub-dc9 learning notes

CVPR 2022 | improve the utilization efficiency of small data sets, Fudan et al. Proposed layered cascaded vit network

Operation of ES

Deutsche Bank listed on the world Hong Kong Stock Exchange: with a market value of HK $3.9 billion, Shaanxi Automobile Group is the major shareholder
随机推荐
Vulnhub-DC9学习笔记
重新认识你的NFT
angr原理与实践(一)——原理
ACL 2022 | argument pair extraction based on reading comprehension
What is a recommendation system?
Know Baidu AI development platform
[sdx62] SBL stage read GPIO status operation
AcWing 3619. Date date processing
移远通信助力夏粮存储新招式,科技手段更有效
Preparation for Bao Yan machine test XIV: BFS
Background running program method
03 gulimall development environment configuration
RGB图像上的密文--违规数据隐藏
Value problem in watch
Go 原生插件使用问题全解析
【动态内存分配】
Talking about brain computer interface
Preparation for the computer test of Baosteel 15: a master of string processing
Sudo cannot find the command command not found solution
watch时的value问题