题意:
给n个数染色,这n个数组成一个环,相邻不同数的颜色必须不同,问最少需要的颜色种类数和具体的染色方案。
题解:
·如果n个数都是相同的,那么只要选择一种颜色即可。
·如果n是偶数且n个数不全相同,那么只要按照121212…染色即可,因为相同的数可以不同颜色。
·如果n是奇数且n个数不全相同,那么只要寻找有没有a[k]=a[k+1]即可,如果有,那么,从1到k,k+1到n的染色顺序为1,2,1,2,…,1(k),1(k+1),(或者2(k),2(k+1),根据k奇偶判断),…1,2;如果没有,那么就需要三种元素,前n-1个元素是1,2,1,2…,1,2,最后一个元素为3,因为n为奇数,那么第1个元素和第n个元素按照之前的染色方法会染上相同颜色,不符合题意。
AC_CODE(和题解思路有点出入==因为过完才发现更简单的做法):
/*
* Author: lwktxdy
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define UFOR(I, A, B) for (int I = (A); I >= (B); --I)
#define UREPP(I, A, B) for (int I = (A); I > (B); --I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long ll,LL;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(LL &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const LL &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
template <typename A, typename B> string to_string(pair<A, B> p);
template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {return '"' + s + '"';}
string to_string(const char* s) {return to_string((string) s);}
string to_string(bool b) {return (b ? "true" : "false");}
string to_string(vector<bool> v) { bool first = true;string res = "{";for (int i = 0; i < static_cast<int>(v.size()); i++) {if (!first) {res += ", ";}first = false;res += to_string(v[i]);}res += "}";return res;}
template <size_t N> string to_string(bitset<N> v) {string res = "";for (size_t i = 0; i < N; i++) {res += static_cast<char>('0' + v[i]);}return res;}
template <typename A> string to_string(A v) {bool first = true;string res = "{";for (const auto &x : v) {if (!first) {res += ", ";}first = false;res += to_string(x);}res += "}";return res;}
template <typename A, typename B> string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {cerr << " " << to_string(H);debug_out(T...);}
#define in freopen("in.txt","r",stdin);
#define out freopen("out.txt","w",stdout);
const ll mod = 998244353;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
LL C(LL n,LL m){if(m > n) return 0;LL ans = 1;FOR(i,1,m){LL a = (n + i - m) % mod;LL b = i % mod;ans = ans * (a * powmod(b, mod-2) % mod) % mod;}return ans;}
const int maxn = 4e6+5;
/*
struct node{
}k[maxn];
*/
void solve(){
int n;
R(n);
VI v(n);
set<int>s;
REPP(i,0,n) R(v[i]), s.insert(v[i]);
VI ans(n);
ans[0] = 1;
bool flag = 0;
if(SZ(s) ==1) flag = 1;
int a = 1;
REPP(i,0,n-1){
if(v[i] == v[i-1]) {
ans[i] = a;
} else {
if(a == 1) a = 2;
else a = 1;
ans[i] = a;
}
}
if(v[n-2] == v[0] && v[n-1] == v[0]) ans[n-1] = a;
else if(v[n-2] != v[n-1] && v[n-1] != v[0] && v[n-2] != v[0]) {
if(ans[n-2] == ans[0]) ans[n-1] = (a == 1 ? 2 : 1);
else {
bool pp = 0;
for(int i=n-2; i>0; --i) {
if(v[i] == v[i-1]) {
ans[i] = (ans[i] == 1 ? 2 : 1);
pp = 1;
break;
} else {
ans[i] = (ans[i] == 1 ? 2 : 1);
}
}
if(!pp) {
ans[0] = (ans[0] == 1 ? 2 : 1);
ans[n-1] = 3;
} else {
ans[n-1] = (ans[n-2] == 1 ? 2 : 1);
}
}
}
else if(v[n-2] != v[n-1] && v[n-2] == v[0]) {
if(ans[0] == ans[n-2]) ans[n-1] = (a == 1 ? 2 : 1);
else {
bool ok = 0;
for(int i=n-2; i>0; --i) {
if(v[i] != v[i-1]) {
if(ans[i] == 1) ans[i] = 2;
else if(ans[i] == 2) ans[i] = 1;
} else {
if(ans[i] == 1) ans[i] = 2;
else if(ans[i] == 2) ans[i] = 1;
ok = 1;
break;
}
}
if(ok) ans[n-1] = (ans[n-2] == 1 ? 2 : 1);
else ans[n-1] = 3, ans[0] = (ans[0] == 1 ? 2 : 1);
}
}
else if(v[n-2] != v[n-1] && v[n-1] == v[0]) ans[n-1] = (a == 1 ? 2 : 1);
else if(v[n-2] == v[n-1] && v[n-1] != v[0]) ans[n-1] = (ans[0] == 1 ? 2 : 1);
if(flag) {
W(1);
W(VI(n,1));
return;
}
else if(ans[n-1] == 3) W(3);
else W(2);
W(ans);
}
int main(){
CASET{solve();}
//solve();
}
题意:
给定一颗顶点为1的树,有q组询问,每次询问给出k个顶点,询问在顶点出发的某一条链上,是否可以让这k个顶点到链的距离d<=1(即点可以在链上)。
题解:
要使这k个顶点到链的距离d<=1,那么这k个点(除了根节点)的父节点必定在同一条链上,dfs记录每个点的父节点,以及每个节点的入栈和出栈顺序,然后有两种判断方法:第一种是将这k个节点的父节点按照深度排序,那么在同一条链上的点,深度小的节点的入栈顺序一定小于深度大的入栈顺序,并且深度小的节点的出栈顺序一定大于深度大的节点。第二种是维护k个节点的父节点的入栈顺序最大值maxx以及出栈顺序最小值minn,当且仅当maxx <= minn才可保证所有节点处于同一链上。
AC_CODE:
1.
/*
* Author: lwktxdy
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define UFOR(I, A, B) for (int I = (A); I >= (B); --I)
#define UREPP(I, A, B) for (int I = (A); I > (B); --I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long ll,LL;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(LL &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const LL &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
template <typename A, typename B> string to_string(pair<A, B> p);
template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {return '"' + s + '"';}
string to_string(const char* s) {return to_string((string) s);}
string to_string(bool b) {return (b ? "true" : "false");}
string to_string(vector<bool> v) { bool first = true;string res = "{";for (int i = 0; i < static_cast<int>(v.size()); i++) {if (!first) {res += ", ";}first = false;res += to_string(v[i]);}res += "}";return res;}
template <size_t N> string to_string(bitset<N> v) {string res = "";for (size_t i = 0; i < N; i++) {res += static_cast<char>('0' + v[i]);}return res;}
template <typename A> string to_string(A v) {bool first = true;string res = "{";for (const auto &x : v) {if (!first) {res += ", ";}first = false;res += to_string(x);}res += "}";return res;}
template <typename A, typename B> string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {cerr << " " << to_string(H);debug_out(T...);}
#define in freopen("in.txt","r",stdin);
#define out freopen("out.txt","w",stdout);
const ll mod = 998244353;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
LL C(LL n,LL m){if(m > n) return 0;LL ans = 1;FOR(i,1,m){LL a = (n + i - m) % mod;LL b = i % mod;ans = ans * (a * powmod(b, mod-2) % mod) % mod;}return ans;}
const int maxn = 4e6+5;
char s[maxn];
int n,m,cnt;
VI v[maxn];
int pre[maxn], dep[maxn], ldf[maxn], rdf[maxn], q[maxn];
/*
struct node{
}k[maxn];
*/
void dfs(int u, int fa){
dep[u] = dep[pre[u] = fa] + 1;
ldf[u] = ++cnt;
for(auto x: v[u]) {
if(x != fa) dfs(x, u);
}
rdf[u] = cnt;
}
void solve(){
R(n,m);
FOR(i,1,n-1){
int a,b;
R(a,b);
v[a].PB(b);
v[b].PB(a);
}
dfs(1,0);
while(m--){
int k;
R(k);
FOR(i,1,k){
int a;
R(a);
q[i] = (a == 1 ? 1 : pre[a]);
}
sort(q+1,q+1+k,[](int i, int j){return dep[i] < dep[j];});
bool ok = 1;
FOR(i,2,k){
if(ldf[q[i]] < ldf[q[i-1]] || rdf[q[i]] > rdf[q[i-1]]) ok = 0;
}
if(ok) W("YES");
else W("NO");
}
}
int main(){
//CASET{solve();}
solve();
}
/*
* Author: lwktxdy
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define UFOR(I, A, B) for (int I = (A); I >= (B); --I)
#define UREPP(I, A, B) for (int I = (A); I > (B); --I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long ll,LL;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(LL &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const LL &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
template <typename A, typename B> string to_string(pair<A, B> p);
template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {return '"' + s + '"';}
string to_string(const char* s) {return to_string((string) s);}
string to_string(bool b) {return (b ? "true" : "false");}
string to_string(vector<bool> v) { bool first = true;string res = "{";for (int i = 0; i < static_cast<int>(v.size()); i++) {if (!first) {res += ", ";}first = false;res += to_string(v[i]);}res += "}";return res;}
template <size_t N> string to_string(bitset<N> v) {string res = "";for (size_t i = 0; i < N; i++) {res += static_cast<char>('0' + v[i]);}return res;}
template <typename A> string to_string(A v) {bool first = true;string res = "{";for (const auto &x : v) {if (!first) {res += ", ";}first = false;res += to_string(x);}res += "}";return res;}
template <typename A, typename B> string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {cerr << " " << to_string(H);debug_out(T...);}
#define in freopen("in.txt","r",stdin);
#define out freopen("out.txt","w",stdout);
const ll mod = 998244353;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
LL C(LL n,LL m){if(m > n) return 0;LL ans = 1;FOR(i,1,m){LL a = (n + i - m) % mod;LL b = i % mod;ans = ans * (a * powmod(b, mod-2) % mod) % mod;}return ans;}
const int maxn = 4e6+5;
char s[maxn];
int n,m,cnt;
VI v[maxn];
int pre[maxn], dep[maxn], ldf[maxn], rdf[maxn], q[maxn];
/*
struct node{
}k[maxn];
*/
void dfs(int u, int fa){
dep[u] = dep[pre[u] = fa] + 1;
ldf[u] = ++cnt;
for(auto x: v[u]) {
if(x != fa) dfs(x, u);
}
rdf[u] = cnt;
}
void solve(){
R(n,m);
FOR(i,1,n-1){
int a,b;
R(a,b);
v[a].PB(b);
v[b].PB(a);
}
dfs(1,0);
while(m--){
int k;
R(k);
int nx=1,mx=n;
FOR(i,1,k){
int a;
R(a);
q[i] = (a == 1 ? 1 : pre[a]);
nx=max(nx,ldf[q[i]]);
mx=min(mx,rdf[q[i]]);
}
if(nx<=mx) W("YES");
else W("NO");
}
}
int main(){
//CASET{solve();}
solve();
}
题意:
给定一个数组,每次可以将数组最小的数+1或者最大的数-1,问最少需要多少次操作使得数组有k个数相等。
题解:
如果原数组已有k个或以上的数相等,直接输出0。否则,
设经过x次操作后数组有k个数相同,值为y,那么y一定是原数组中的某个数,(这个动手画一画就好了),将原数组排序,然后有三种情况:
·1到i-1个数全部转换成a[i],再从中挑选(i-k)个数变成a[i]-1。
·i+1到n个数全部转换成a[i],再从中挑选(n-k+1-i)个数变成a[i]+1。
·将1到i-1个数全部转换成a[i],i+1到n个数全部转换成a[i],然后去除(n-k)个数即可。
用前缀和记录前i个数转换成ai需要的步数,后缀和记录后i个数转换成ai需要的步数,然后按照上面步骤维护最小值即可。
AC_CODE:
/*
* Author: lwktxdy
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define UFOR(I, A, B) for (int I = (A); I >= (B); --I)
#define UREPP(I, A, B) for (int I = (A); I > (B); --I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long ll,LL;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(LL &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const LL &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
template <typename A, typename B> string to_string(pair<A, B> p);
template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {return '"' + s + '"';}
string to_string(const char* s) {return to_string((string) s);}
string to_string(bool b) {return (b ? "true" : "false");}
string to_string(vector<bool> v) { bool first = true;string res = "{";for (int i = 0; i < static_cast<int>(v.size()); i++) {if (!first) {res += ", ";}first = false;res += to_string(v[i]);}res += "}";return res;}
template <size_t N> string to_string(bitset<N> v) {string res = "";for (size_t i = 0; i < N; i++) {res += static_cast<char>('0' + v[i]);}return res;}
template <typename A> string to_string(A v) {bool first = true;string res = "{";for (const auto &x : v) {if (!first) {res += ", ";}first = false;res += to_string(x);}res += "}";return res;}
template <typename A, typename B> string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {cerr << " " << to_string(H);debug_out(T...);}
#define in freopen("in.txt","r",stdin);
#define out freopen("out.txt","w",stdout);
const ll mod = 998244353;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
LL C(LL n,LL m){if(m > n) return 0;LL ans = 1;FOR(i,1,m){LL a = (n + i - m) % mod;LL b = i % mod;ans = ans * (a * powmod(b, mod-2) % mod) % mod;}return ans;}
const int maxn = 4e6+5;
char s[maxn];
ll a[maxn];
ll f[maxn], g[maxn];
/*
struct node{
}k[maxn];
*/
void solve(){
int n,k;
R(n,k);
FOR(i,1,n) R(a[i]);
sort(a+1,a+1+n);
{
int num = 1;
int maxx = 1;
FOR(i,2,n){
if(a[i] == a[i-1]) num += 1;
else maxx = max(maxx, num), num = 1;
}
maxx = max(maxx, num);
if(maxx >= k){
W(0);
return;
}
}
FOR(i,1,n){
f[i] = (i-1) * (a[i]-a[i-1]) + f[i-1];
}
for(int i=n; i>=1; --i){
g[i] = (n-i) * (a[i+1]-a[i]) + g[i+1];
}
ll minn = 1LL<<60;
FOR(i,k,n) minn = min(minn, f[i]-(i-k));
for(int i=n-k+1; i>0; --i) minn = min(minn, g[i]-(n-k+1-i));
FOR(i,1,n){
minn = min(minn, f[i]+g[i]-(n-k));
}
W(minn);
}
int main(){
//CASET{solve();}
solve();
}