【Codeforces 986B】Petr and Permutations
阿新 • • 發佈:2019-03-09
inpu 奇數 black ref class ger catch ans exti
【鏈接】 我是鏈接,點我呀:)
【題意】
題意
【題解】
n為奇數時3n和7n+1奇偶性不同
n為偶數時也是如此
然後交換任意一對數
逆序對的對數的奇偶性會發生改變一次
求出逆序對 對n討論得出答案。
【代碼】
import java.io.*; import java.util.*; public class Main { static InputReader in; static PrintWriter out; public static void main(String[] args) throws IOException{ //InputStream ins = new FileInputStream("E:\\rush.txt"); InputStream ins = System.in; in = new InputReader(ins); out = new PrintWriter(System.out); //code start from here new Task().solve(in, out); out.close(); } static int N = (int)1e6; static class Task{ int n; int a[] = new int[N+10]; int temp[] = new int[N+10]; int ans = 0; public void mergesort(int l,int r) { if (l>=r) return; int mid = (l+r)/2; mergesort(l,mid);mergesort(mid+1,r); int k = l,i = l,j = mid+1; while (i <= mid && j <= r) { if (a[i]>a[j]) { ans = ans + mid-i+1; ans = ans % 2; temp[k++] = a[j]; j++; }else { temp[k++] = a[i]; i++; } } while (i<=mid) { temp[k++] = a[i]; i++; } while (j<=r) { temp[k++] = a[j]; j++; } for (i = l;i <= r;i++) a[i] = temp[i]; } public void solve(InputReader in,PrintWriter out) { n = in.nextInt(); for (int i = 1;i <= n;i++) { a[i] = in.nextInt(); } mergesort(1,n); if (n%2==1) { if (ans==1) { out.println("Petr"); }else { out.println("Um_nik"); } }else { if (ans==1) { out.println("Um_nik"); }else { out.println("Petr"); } } } } static class InputReader{ public BufferedReader br; public StringTokenizer tokenizer; public InputReader(InputStream ins) { br = new BufferedReader(new InputStreamReader(ins)); tokenizer = null; } public String next(){ while (tokenizer==null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(br.readLine()); }catch(IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
【Codeforces 986B】Petr and Permutations