题意:初始有一个序列[1,2,...N],一次操作可以将任意两个位置的值互换,Petr做3*n次操作:Alxe做7*n+1次操作.给出最后生成的新序列,问是由谁操作得到的. 分析:一个序列的状态可以归为:由原序列操作奇数次得到(简称奇序列):和操作偶数次(偶序列)得到.显然奇序列中,逆序对的个数为奇数:偶序列中,逆序对的个数为偶.当n为奇数时,3*n为奇,7*n+1为偶:n为偶数时正好相反. 用树状数组或归并排序求逆序对即可. #include<iostream> #include<st…
题目连接: Petr and Permutations 题意:给出一个1到n的序列,Petr打乱了3n次,Um_nik打乱了7n+1次,现在给出被打乱后的序列,求是谁打乱的. 题解:因为给出了一个3*n和一个7*n+1,发现这两个当一个为奇数另一个一定为偶数,所以可以联想和奇偶性质有关.但是这里面要算最短几步能把当前的序列变成1-n.这里我算错~~顺便学了一下如何将置换序列复原. #include<bits/stdc++.h> using namespace std; typedef pair…
题目链接 题目大意 给你一个长度为n的字符串,可以交换相邻两个元素,使得这个字符串翻转,求最少多少种次数改变 题目思路 如果要求数组排序所需要的冒泡次数,那其实就是逆序对 这个也差不多,但是如果是相同字符,用的应该是对应的最近的这个字母. 代码 #include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=2e5+5; int n,cnt[30],ans[maxn],tree[maxn<…
题意:给你一个字符串,每次可以调换现字符串的相邻两个字符,问最少操作多少次使得这个字符串等于其反转过来的字符串. 题解:先考虑字符串中没有相同字符的情况,那么我们每次将目前字符串的最后一个字符一直调换到前面就行,如果出现相同字符的话,先让最靠前的字符调换到对应位置一定是最优的.我们先记录原字符串中每个字符的位置,然后将字符串反转,根据反转后的字符位置弄一个新数组,注意相同字符一定要把小的位置放在前面,然后用树状数组求个逆序对即可. 代码: #define int long long int n;…
每次交换:逆序对的数量+1或者-1: 假设最后逆序对数量为 sum; ①x+y=3n; ②x-y=sum; -> 3n+sum为偶数: 所以 n 和 sum 必须奇偶一样: #include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #include<cstring> #include<string> #include<cmath>…
Problem UVA1620-Lazy Susan Accept: 81  Submit: 375Time Limit: 3000 mSec Problem Description There are N marbles, which are labeled 1,2,...,N. The N marbles are put in a circular track in an arbitrary order. In the top part of the track there is a “la…
A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #define TS printf("!!!\n") #define pb push_back #define inf 0x3f3f3f3f //std::ios::sync_with_stdio(false); using namespace std; //priority_queue<i…
这题真的只能靠直觉了,我没法给出详细证明. 解题思路: 1.交换3n次或者7n+1次,一定会出现一个为奇数,另一个为偶数. 2.用最朴素的方法,将n个数字归位,计算交换次数. 3.判断交换次数是否与3n的奇偶性相同,相同输出Petr: 不相同则一定与7n+1的奇偶性相同,输出Um_nik. 代码: #include <bits/stdc++.h> using namespace std; typedef long long ll; ]; ]; int main(){ ios::sync_wit…
Description\text{Description}Description Given an array a[], swap random 2 number of them for 3n or (7n+1) times.\text{Given an array }a[],\text{ swap random 2 number of them for }3n\text{ or }(7n+1)\text{ times.}Given an array a[], swap random 2 num…
题意: 给出一个长度为n的序列,求出是谁操作的(原序列为从小到大的序列),Peter的操作次数为3n,Alex的操作次数为7n+1 解析: 我们来看这个序列中的逆序对,逆序对的个数为偶数则操作次数为偶数,逆序对的个数为奇数,则操作次数为奇数 然后树状数组求逆序对即可 #include <bits/stdc++.h> #define mem(a, b) memset(a, b, sizeof(a)) using namespace std; , INF = 0x7fffffff; int c[m…