結果

問題 No.366 ロボットソート
ユーザー threepipes_sthreepipes_s
提出日時 2016-04-29 23:38:50
言語 Java21
(openjdk 21)
結果
AC  
実行時間 132 ms / 2,000 ms
コード長 5,250 bytes
コンパイル時間 2,705 ms
コンパイル使用メモリ 93,704 KB
実行使用メモリ 42,084 KB
最終ジャッジ日時 2024-06-09 10:23:04
合計ジャッジ時間 5,484 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
37,136 KB
testcase_01 AC 55 ms
37,104 KB
testcase_02 AC 53 ms
36,992 KB
testcase_03 AC 56 ms
37,316 KB
testcase_04 AC 53 ms
37,136 KB
testcase_05 AC 53 ms
37,228 KB
testcase_06 AC 56 ms
36,920 KB
testcase_07 AC 53 ms
36,800 KB
testcase_08 AC 54 ms
37,020 KB
testcase_09 AC 54 ms
37,092 KB
testcase_10 AC 61 ms
37,196 KB
testcase_11 AC 108 ms
39,764 KB
testcase_12 AC 125 ms
40,652 KB
testcase_13 AC 99 ms
38,744 KB
testcase_14 AC 105 ms
38,732 KB
testcase_15 AC 87 ms
37,900 KB
testcase_16 AC 88 ms
38,776 KB
testcase_17 AC 99 ms
38,608 KB
testcase_18 AC 125 ms
40,304 KB
testcase_19 AC 105 ms
38,412 KB
testcase_20 AC 55 ms
37,084 KB
testcase_21 AC 132 ms
41,612 KB
testcase_22 AC 130 ms
42,084 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
 
public class Main {
	public static void main(String[] args) throws NumberFormatException,
	IOException {Main solve = new Main();solve.solve();}
	void dump(int[]a){for(int i=0;i<a.length;i++)System.out.print(a[i]+" ");System.out.println();}
	void dump(int[]a,int n){for(int i=0;i<a.length;i++)System.out.printf("%"+n+"d",a[i]);System.out.println();}
	void dump(long[]a){for(int i=0;i<a.length;i++)System.out.print(a[i]+" ");System.out.println();}
	void dump(char[]a){for(int i=0;i<a.length;i++)System.out.print(a[i]);System.out.println();}
	long pow(long a,int n){long r=1;while(n>0){if((n&1)==1)r*=a;a*=a;n>>=1;}return r;}
	String itob(int a,int l){return String.format("%"+l+"s",Integer.toBinaryString(a)).replace(' ','0');}
	void sort(int[]a){m_sort(a,0,a.length,new int[a.length]);}
	void m_sort(int[]a, int s, int sz, int[]b){
		if(sz<7){for(int i=s;i<s+sz;i++)for(int j=i;j>s&&a[j-1]>a[j];j--)swap(a, j, j-1);return;}
		m_sort(a,s,sz/2,b);m_sort(a,s+sz/2,sz-sz/2,b);int idx=s;int l=s,r=s+sz/2;final int le=s+sz/2,re=s+sz;
		while(l<le&&r<re){if(a[l]>a[r])b[idx++]=a[r++];else b[idx++]=a[l++];}
		while(r<re)b[idx++]=a[r++];while(l<le)b[idx++]=a[l++];for(int i=s;i<s+sz;i++)a[i]=b[i];
	} /* qsort(3.5s)<<msort(9.5s)<<<shuffle+qsort(17s)<Arrays.sort(Integer)(20s) */
	void swap(int[] a,int i,int j){final int t=a[i];a[i]=a[j];a[j]=t;}
	int binarySearchSmallerMax(int[]a,int v) // get maximum index which a[idx]<=v
	{int l=0,r=a.length-1,s=0;while(l<=r){int m=(l+r)/2;if(a[m]>v)r=m-1;else{l=m+1;s=m;}}return a[s]>v?-1:s;}
	void solve() throws NumberFormatException, IOException{
		final ContestScanner in = new ContestScanner();
		Writer out = new Writer();
		int n = in.nextInt();
		int k = in.nextInt();
		int[] a = new int[n];
		for(int i=0; i<n; i++){
			a[i] = in.nextInt();
		}
		HashMap<Integer, Integer> nmap = new HashMap<>();
		int[] b = a.clone();
		Arrays.sort(b);
		for(int i=0; i<n; i++){
			nmap.put(b[i], i);
		}
		for(int i=0; i<n; i++){
			a[i] = nmap.get(a[i]);
		}
		int cnt = 0;
		for(int i=0; i<k; i++){
			BIT bit = new BIT(n/k+1);
			for(int j=i; j<n; j+=k){
				if(a[j]%k!=i){
					System.out.println(-1);
					return;
				}
				cnt += j/k-bit.sum(a[j]/k);
				bit.add(a[j]/k, 1);
			}
		}
		System.out.println(cnt);
	}
}

class BIT{
	int n;
	int[] bit;
	public BIT(int n){
		this.n = n;
		bit = new int[n+1];
	}
	
	void add(int idx, int val){
		for(int i=idx+1; i<=n; i+=i&(-i)) bit[i-1] += val;
	}
	
	int sum(int idx){
		int res = 0;
		for(int i=idx+1; i>0; i-=i&(-i)) res += bit[i-1];
		return res;
	}
	
	int sum(int begin, int end){
		if(begin == 0) return sum(end);
		return sum(end)-sum(begin-1);
	}
	
	int find(int sum){
		// find minimum index which sum equals given value
		int low = 0;
		int up  = n;
		int tmp = -1;
		int oldIdx = -1;
		if(sum == 0) return 0;
		while(low < up){
			int mid = (low+up)/2;
			tmp = sum(mid);
			if(tmp < sum){
				if(oldIdx > 0 && low == mid) break;
				low = mid;
			}else{
				if(tmp == sum) oldIdx = mid;
				up = mid;
			}
		}
		if(oldIdx > 0 && sum(oldIdx-1) == sum) oldIdx--;
		return oldIdx;
	}
}
 
class MultiSet<T> extends HashMap<T, Integer>{
	@Override
	public Integer get(Object key){return containsKey(key)?super.get(key):0;}
	public void add(T key,int v){put(key,get(key)+v);}
	public void add(T key){put(key,get(key)+1);}
	public void sub(T key)
	{final int v=get(key);if(v==1)remove(key);else put(key,v-1);}
}
class Timer{
	long time;
	public void set(){time=System.currentTimeMillis();}
	public long stop(){return time=System.currentTimeMillis()-time;}
	public void print()
	{System.out.println("Time: "+(System.currentTimeMillis()-time)+"ms");}
	@Override public String toString(){return"Time: "+time+"ms";}
}
class Writer extends PrintWriter{
	public Writer(String filename)throws IOException
	{super(new BufferedWriter(new FileWriter(filename)));}
	public Writer()throws IOException{super(System.out);}
}
class ContestScanner {
	private InputStreamReader in;private int c=-2;
	public ContestScanner()throws IOException 
	{in=new InputStreamReader(System.in);}
	public ContestScanner(String filename)throws IOException
	{in=new InputStreamReader(new FileInputStream(filename));}
	public String nextToken()throws IOException {
		StringBuilder sb=new StringBuilder();
		while((c=in.read())!=-1&&Character.isWhitespace(c));
		while(c!=-1&&!Character.isWhitespace(c)){sb.append((char)c);c=in.read();}
		return sb.toString();
	}
	public String readLine()throws IOException{
		StringBuilder sb=new StringBuilder();if(c==-2)c=in.read();
		while(c!=-1&&c!='\n'&&c!='\r'){sb.append((char)c);c=in.read();}
		return sb.toString();
	}
	public long nextLong()throws IOException,NumberFormatException
	{return Long.parseLong(nextToken());}
	public int nextInt()throws NumberFormatException,IOException
	{return(int)nextLong();}
	public double nextDouble()throws NumberFormatException,IOException 
	{return Double.parseDouble(nextToken());}
}
0