首页 > 其他分享 >洛谷 P3034 Cow Photography G/S——题解

洛谷 P3034 Cow Photography G/S——题解

时间:2024-09-07 10:13:32浏览次数:4  
标签:10 20 int 题解 Photography 30 Downarrow P3034 奶牛

洛谷P3034题解


传送锚点


摸鱼环节

[USACO11DEC] Cow Photography G/S

题面翻译

题目描述

今天的奶牛们特别调皮!Farmer John 想做的只是给排成一排的奶牛拍照,但是在他拍下照片之前,奶牛们一直在移动。

具体地说,FJ 有 \(N\) 头奶牛(\(1 \leq N \leq 20\,000\)),每头奶牛都有一个唯一确定的编号。FJ 想要以一个特定的顺序拍下一张奶牛排成一排的照片,这个顺序用数组 \(A[1 \ldots N]\) 表示,其中 \(A[i]\) 代表排在 \(i\) 位置的奶牛的编号。

他按照这样的顺序将奶牛们排列好,但在他按下快门之前,有些奶牛(可能是零头或任意多头奶牛,位置也不一定连续)将移到一个新的位置。更准确地说,一些奶牛离开队列,剩下的奶牛靠拢,这些离开的奶牛再将自己重新插入到队列中的任意位置(不一定是他们之前的位置)。FJ 感到非常沮丧,他再次按照 \(A\) 数组的顺序重新安排了队列。但在他再次按下快门之前,又有一些奶牛移动到了新的位置。

就这样,FJ 拍了五张照片。给出每张照片拍摄的内容(即 FJ 按下快门时奶牛的顺序),请你尝试推算出 FJ 最初为奶牛们排的顺序(即 \(A\) 数组)。由于可能有奶牛移动,照片显示的顺序与原来的顺序可能有所不同。但是,一头奶牛最多只会移动一次:即如果一头奶牛在拍其中一张照片时移动了,它在拍其他四张照片的时候都不会移动。当然,由于其他奶牛也在移动,它在不同照片中的顺序并不一定相同。

输入格式

第一行包含一个整数 \(N\)。

接下来 \(5N\) 行,每 \(N\) 行描述了一张照片中各奶牛的顺序。每行包含一个奶牛的编号,保证所有编号都是不超过 \(1\,000\,000\,000\) 的非负整数。

输出格式

输出 \(N\) 行,每行一个整数 \(A[i]\),即 FJ 最初为奶牛排好的顺序。

样例解释

FJ 拍的五张照片分别为:

  • 10 20 30 40 50
  • 20 10 30 40 50
  • 30 10 20 40 50
  • 40 10 20 30 50
  • 50 10 20 30 40

题目描述

The cows are in a particularly mischievous mood today! All Farmer John wants to do is take a photograph of the cows standing in a line, but they keep moving right before he has a chance to snap the picture.

Specifically, each of FJ's N (1 <= N <= 20,000) cows has a unique integer ID number. FJ wants to take a picture of the cows standing in a line in a very specific ordering, represented by the contents of an array A[1...N], where A[j] gives the ID number of the jth cow in the ordering. He arranges the cows in this order, but just before he can press the button on his camera to snap the picture, a group of zero or more cows (not necessarily a contiguous group) moves to a set of new positions in the lineup. More precisely, a group of zero or more cows steps away from the line, with the remaining cows shifting over to close the resulting gaps in the lineup. The cows who stepped away then re-insert themselves at different positions in the lineup (not necessarily at the locations they originally occupied). Frustrated but not deterred, FJ again arranges his cows according to the ordering in A, but again, right before he can snap a picture, a different group of zero or more cows moves to a set of new positions in the lineup.

The process above repeats for a total of five photographs before FJ gives up. Given the contents of each photograph, see if you can reconstruct the original intended ordering A. Each photograph shows an ordering of the cows that differs from A in that some group of zero or more cows has moved. However, a cow only moves in at most one photograph: if a cow is part of the group that moves in one photograph, she will not actively move in any of the other four photographs (although she could end up at a different index as a consequence of other cows around her moving, of course).

Farmer John居然拍了五张照片,每张表示初始化牛位置的移动后的状态。然后求出FJ这五头牛初始化的样子。

输入格式

* Line 1: The number of cows, N (1 <= N <= 20,000).

* Lines 2..5N+1: The next 5N lines describe five orderings, each one a block of N contiguous lines. Each line contains the ID of a cow, an integer in the range 0...1,000,000,000.

输出格式

* Lines 1..N: The intended ordering A, one ID per line.

样例 #1

样例输入 #1

5 
10 
20 
30 
40 
50 
20 
10 
30 
40 
50 
30 
10 
20 
40 
50 
40 
10 
20 
30 
50 
50 
10 
20 
30 
40

样例输出 #1

10 
20 
30 
40 
50

提示

There are 5 cows, with IDs 10, 20, 30, 40, and 50. In each of the 5 photos, a different cow moves to the front of the line (at most one cow moves in each photo here, but it is possible in other inputs that multiple cows could move in a particular photo).

The correct original ordering A[1..5] is 10, 20, 30, 40, 50.


又是经典的题面,john与cow的爱情故事john与cow的oi故事,也不知道为啥要给奶牛拍照,咱也不敢问,问了john发力给黑题就完了。于是为了祭拜膜拜john这位oi鼻祖,咱就来写个蓝题。


正片开始

1.草率代码

仔细分析样例可以知道,对于5张照片,只要有奶牛\(A\)在奶牛\(B\)之前出现三次,则奶牛\(A\)一定在奶牛\(B\)之前。由于\(奶牛编号\leq 1000000000\),所以需要用map搞一下。很简单就能得到以下代码。

code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=5e6+1;
int n,a[N];
map<int,ll>m[10];//照片编号,奶牛编号——>位置编号
int cmp(int x,int y)
{
    int ans=0;//统计次数
    for(int i=1;i<=5;i++)//遍历每张照片中x,y的大小关系。
        if(m[i][x]<m[i][y]) ans++;
    return ans>=3;
}
int main()
{
    cin>>n;
    for(int i=1;i<=5;i++)
    {
        for(int j=1;j<=n;j++)
        {
            int x;cin>>x;
            a[j]=x;//每张照片中的元素不变,直接覆盖即可
            m[i][x]=j;//用map映射关系
        }
    }
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=n;i++) cout<<a[i]<<endl;
    return 0;
}

2.懵逼部分

  1. 浅浅分析下复杂度,排序自带\(5 \cdot nlogn\),基于map内用红黑树实现,每次查询\(logn\),总复杂就是\(5\cdot nlogn \cdot2\cdot logn=10 \cdot n\cdot (logn)^{2}\),将小\(n=20000\)带入浅算一下,诶嘛直接炸了(但不知道为啥我居然没祭,难不成是数据太水了是我RP值太高了?)。优化算法,很明显用哈希,这里可以用unordered_map,内部基于哈希实现,体验\(o(1)\)查询的快感。
  2. 实测了一下,最后一个大数据,用原始版map跑了\(334ms\),用上unordered_map的话只需要\(94ms\),大大提升了代码的B格容错率。

完整代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=5e6+1;
int n,a[N];
unordered_map<int,ll>m[10];//照片编号,奶牛编号——>位置编号
int cmp(int x,int y)
{
    int ans=0;
    for(int i=1;i<=5;i++)
        if(m[i][x]<m[i][y]) ans++;
    return ans>=3;
}
int main()
{
    cin>>n;
    for(int i=1;i<=5;i++)
    {
        for(int j=1;j<=n;j++)
        {
            int x;cin>>x;
            a[j]=x;
            m[i][x]=j;
        }
    }
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=n;i++) cout<<a[i]<<endl;
    return 0;
}

完结收工!!!!!

个人主页

看完点赞,养成习惯

\(\Downarrow\Downarrow\Downarrow\Downarrow\Downarrow\Downarrow\Downarrow\Downarrow\Downarrow\Downarrow\Downarrow\Downarrow\Downarrow\Downarrow\Downarrow\Downarrow\Downarrow\Downarrow\Downarrow\Downarrow\Downarrow\Downarrow\Downarrow\Downarrow\Downarrow\)

标签:10,20,int,题解,Photography,30,Downarrow,P3034,奶牛
From: https://www.cnblogs.com/qc0817/p/18401398

相关文章

  • 题解:AT_abc369_e [ABC369E] Sightseeing Tour 详细版
    题目大意给定一个NNN个点,MMM条边的无向图。其中边有边权。有......
  • MySQL 日期函数语法介绍和案例示范以及常见问题解决
    本文将以电商交易系统为例,详细讲解MySQL日期类型及其转化,常用的日期函数,以及一些解决常见问题的方案。一、MySQL日期数据类型MySQL提供了多种日期数据类型,适用于不同的使用场景。常见的日期类型包括DATE、DATETIME、TIMESTAMP、TIME和YEAR。DATE:只存储日期,不包含......
  • 题解:SP3693 KGSS - Maximum Sum
    原题传送门思路分析线段树。这道题让我们进行两种操作,分别是单点修改和区间查询,结合数据范围,很明显是一道线段树。区间里最大的\(A_i+A_j\),其实就是求区间里的最大值和次大值,我们用线段树维护最大值和次大值。建树voidbuild(intnow,inttl,inttr){ if(tl==tr){ tmax......
  • P6638 「JYLOI Round 1」常规 题解
    题目传送门前置知识可持久化线段树|前缀和&差分解法进行差分,区间查询转化成前缀和相减。先将\(\{a\}\)升序排序。设当前询问的区间为\([1,r]\),在\(\{a\}\)中找到一个最大的\(pos\)使得\(a_{pos}\ler\),则\([1,r]\)中所做常规的次数为\(\sum\limits_{i=1......
  • 使用flask进行Mock Server模拟接口操作及问题解决
    1.flask介绍flask是一个轻量级的pythonweb微框架2.MockServer介绍MockServer是一个开源的模拟服务器,它可以定义和记录API交互,支持各种http方法(get、post、put、delete),可以自定义响应内容,例如返回静态文件可以使用flask来搭建一个mock模拟服务3.模拟接口先安装flaskpip......
  • P1928 外星密码题解
    初看这题时,感觉就是一个简简单单的递归,便有了以下代码:#include <bits/stdc++.h>using namespace std;string re(){    string s="",s1="";    char c;    int n;    while(cin>>c){        if(c=='['){            cin>>n;......
  • AT_keyence2019_e Connecting Cities 题解
    B算法萌萌题。题解看到完全图求最小生成树,必然是要考虑一下B算法能不能做的。发现这个题的联通块最小值是可以维护的。我们发现。假如我们钦定\(i\)往前面连。那么前面的最小权值必然是一个固定的值。我们一定会连到\(\min(a_j-j\timesD)\)上。由于不能连到自己......
  • AT_aising2019_e Attack to a Tree 题解
    挺有意思的树型dp。思路发现直接求解很难对限制下手。但我们可以注意到答案最多为\(n\)。考虑将答案记录dp状态。我们可以记\(dp_{i,j}\)为子树\(i\)合法并且断了\(j\)条边的状态。由于合法状态有两种,并且不好一起考虑,所以可以再在dp状态中加一维。令\(dp_{i,......
  • P8139 [ICPC2020 WF] Sweep Stakes 题解
    思路容易发现,题目要求我们动态维护这样一个多项式。\[\prod_{i}(1-p_i+p_ix)\]如何维护。由于精度问题,我们很难去进行一个多项式除法将其暴力求出。考虑\(p_i\le0.2\)。可以得知,我们的多项式的数的增减是比较大的。那么在一定数量后,一些可能有值的系数在当前精度下是可以......
  • ORCLE数据库语言设置原因查询不出数据的问题解决
    问题现象:oracle数据库视图中存在数据,但是jdbc查询视图查询不出来,后发现视图中有根据默认语言的过滤,如下图: 客户端查询环境语言为 web服务器查询环境语言为US,所以数据查询不出来。解决方案:修改应用端的NLS_LANG的配置与SQL保持一致linux执行下面代码exportNLS_LANG="......