- 使数组唯一的最小增量
给定整数数组 A,每次 move 操作将会选择任意 A[i],并将其递增 1。
返回使 A 中的每个值都是唯一的最少操作次数。
示例 1:
输入:[1,2,2]
输出:1
解释:经过一次 move 操作,数组将变为 [1, 2, 3]。
示例 2:
输入:[3,2,1,2,1,7]
输出:6
解释:经过 6 次 move 操作,数组将变为 [3, 4, 1, 2, 5, 7]。
可以看出 5 次或 5 次以下的 move 操作是不能让数组的每个值唯一的。
提示:
0 <= A.length <= 40000
0 <= A[i] < 40000
解法一 :
- 注意到数据规模只有
40000
,所以开2
倍空间标记是否出现过 - 定义一个指针
pos
,pos只能递增 - 把
A
排序,枚举A[i]
,当A[i]
是重复元素时,pos=大于A[i]
且未出现过的数字, - a n s + = ( p o s − A [ i ] ) ans+=(pos-A[i]) ans+=(pos−A[i])
解法二:
- 排个序,当后一个小于等于前一个时,答案加等于(前后差值+1)
- 更新当前
#define debug
#ifdef debug
#include <time.h>
#include "win_majiao.h"
#endif
#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#define MAXN ((int)1e5+7)
#define ll long long int
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)
typedef vector<vector<int> > VVI;
using namespace std;
#define show(x...) \
do { \
cout << "\033[31;1m " << #x << " -> "; \
err(x); \
} while (0)
void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }
namespace FastIO{
char print_f[105];
void read() {}
void print() { putchar('\n'); }
template <typename T, typename... T2>
inline void read(T &x, T2 &... oth) {
x = 0;
char ch = getchar();
ll f = 1;
while (!isdigit(ch)) {
if (ch == '-') f *= -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - 48;
ch = getchar();
}
x *= f;
read(oth...);
}
template <typename T, typename... T2>
inline void print(T x, T2... oth) {
ll p3=-1;
if(x<0) putchar('-'), x=-x;
do{
print_f[++p3] = x%10 + 48;
} while(x/=10);
while(p3>=0) putchar(print_f[p3--]);
putchar(' ');
print(oth...);
}
} // namespace FastIO
using FastIO::print;
using FastIO::read;
bool vis[MAXN], vis2[MAXN];
class Solution {
public:
#if 1
//对于每个重复数字找比他大的且第一个未出现的
int minIncrementForUnique(vector<int>& A) {
memset(vis, false, sizeof(vis));
memset(vis2, false, sizeof(vis2));
int n = A.size(), ans = 0;
if(!n) return ans;
// sort(A.begin(), A.end());
int pos = 0;
for(auto x : A) vis[x] = true;
// forvec(A);
for(auto x : A) {
if(!vis2[x]) { vis2[x] = true; continue ; }
while(vis[pos] || pos<=x) pos ++;
// show(x, pos, pos-x);
vis[pos] = true;
ans += (pos - x);
}
return ans;
}
#else
//排序,如果第i个等于前一个,就
int minIncrementForUnique(vector<int>& A) {
sort(A.begin(), A.end());
int ans = 0, n = A.size();
// forvec(A);
for(int i=1; i<n; i++)
if(A[i] <= A[i-1]) {
ans += (A[i-1]-A[i])+1;
A[i] = A[i-1] + 1;
}
return ans;
}
#endif
};
#ifdef debug
signed main() {
Solution s;
vector<int> vec = { 3,2,1,2,1,7 };
cout << s.minIncrementForUnique(vec) << endl;
return 0;
}
#endif
java
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
class Solution {
// public int minIncrementForUnique(int[] A) {
// Arrays.sort(A);
// boolean vis[] = new boolean[Main.MAXN],
// vis2[] = new boolean[Main.MAXN];
// for (int x : A) vis[x] = true;
// int pos = 0, ans = 0;
// for(int x : A) {
// if(!vis2[x]) {
// vis2[x] = true;
// continue ;
// }
// while(pos<=x || vis[pos]) pos ++;
// vis[pos] = true;
// ans += (pos - x);
// }
// return ans;
// }
public int minIncrementForUnique(int[] A) {
Arrays.sort(A);
boolean vis[] = new boolean[Main.MAXN],
vis2[] = new boolean[Main.MAXN];
for (int x : A) vis[x] = true;
int ans = 0, n = A.length;
for(int i=1; i<n; i++)
if(A[i] <= A[i-1]) {
ans += (A[i-1] - A[i]) + 1;
A[i] = A[i-1] + 1;
}
return ans;
}
}
public class Main {
public static final boolean debug = false;
public static String INPATH = "C:\\Users\\majiao\\Desktop\\test.txt",
OUTPATH = "C:\\Users\\majiao\\Desktop\\out.txt";
public static StreamTokenizer tok;
public static BufferedReader cin;
public static PrintWriter cout;
public static long start_time = 0, out_time = 0;
public static int n, m, K, Q, MAXN = (int)1e5+7, INF = 0x3f3f3f3f;
public static byte buf[] = new byte[MAXN];
public static void main(String[] args) throws IOException {
main_init();
if(debug) { start_time = System.currentTimeMillis(); }
if(false) { System.setOut(new PrintStream(OUTPATH)); }
Solution solu = new Solution();
int a[] = { 3,2,1,2,1,7 };
int ans = solu.minIncrementForUnique(a);
cout.printf("%d\n", ans);
if(debug) {
out_time = System.currentTimeMillis();
cout.printf("run time : %d ms\n", out_time-start_time);
}
cout.flush();
}
public static void show(List<Object> list, Object... obj) {
cout.printf("%s : ", obj.length>0 ? obj[0] : "");
for(Object x : list) {
cout.printf("[%s] ", x);
}
cout.printf("\n");
}
public static void show(Map<Object, Object> mp, Object... obj) {
cout.printf("%s : ", obj.length>0 ? obj[0] : "");
Set<Map.Entry<Object, Object>> entries = mp.entrySet();
for (Map.Entry<Object, Object> en : entries) {
cout.printf("[%s,%s] ", en.getKey(), en.getValue());
}
cout.printf("\n");
}
public static<T> void forarr(T arr[], int ...args) {
int lef = 0, rig = arr.length - 1;
if(args.length > 0) { lef = args[0]; rig = args[1]; }
cout.printf(" : ");
for( ; lef<=rig; lef++) {
cout.printf("[%s] ", args[lef]);
}
cout.printf("\n");
}
public static void main_init() {
try {
if (debug) {
cin = new BufferedReader(new InputStreamReader(
new FileInputStream(INPATH)));
} else {
cin = new BufferedReader(new InputStreamReader(System.in));
}
cout = new PrintWriter(new OutputStreamWriter(System.out));
// cout = new PrintWriter(OUTPATH);
tok = new StreamTokenizer(cin);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String next_str() {
try {
tok.nextToken();
if (tok.ttype == StreamTokenizer.TT_EOF)
return null;
else if (tok.ttype == StreamTokenizer.TT_NUMBER) {
return String.valueOf((int)tok.nval);
} else if (tok.ttype == StreamTokenizer.TT_WORD) {
return tok.sval;
} else return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static int read_int() {
String tmp_next_str = next_str();
return null==tmp_next_str ? -1 : Integer.parseInt(tmp_next_str);
}
public static long read_long() { return Long.parseLong(next_str()); }
public static double read_double() { return Double.parseDouble(next_str()); }
public static BigInteger read_big() { return new BigInteger(next_str()); }
public static BigDecimal read_dec() { return new BigDecimal(next_str()); }
class Pair implements Comparable<Pair>{
int fst, sec;
public Pair() { }
public Pair(int fst, int sec) {
this.fst = fst;
this.sec = sec;
}
@Override
public int compareTo(Pair o) {
return fst - o.fst == 0 ? sec - o.sec : fst - o.fst;
}
}
}