const int MAXN = 1000100; vector<int> vt[MAXN]; typedef pair<int, int> pii; vector<pii> ans; int main() { // freopen("in.txt", "r", stdin); int n, k, t; while (~RII(n, k)) { int Max = -INF; REP(i, MAXN) vt[i].clear(); ans.clear(); FE(i, 1, n) { RI(t); Max = max(Max, t); vt[t].push_back(i); } if (vt[0].size() != 1) puts("-1"); else { if (Max >= 1 && vt[1].size() > k) { puts("-1"); goto end; } FF(i, 0, Max) { int cur = vt[i].size(); int next = vt[i + 1].size(); if (i != 0 && next > (LL)(k - 1) * cur) { puts("-1"); goto end; } REP(j, next) ans.push_back(MP(vt[i][j % cur], vt[i + 1][j])); } cout << ans.size() << endl; REP(i, ans.size()) cout << ans[i].first << ‘ ‘ << ans[i].second << endl; } end:; } return 0; }
Codeforces Round #237 (Div. 2)__Restore Graph,布布扣,bubuko.com
Codeforces Round #237 (Div. 2)__Restore Graph
原文:http://blog.csdn.net/wty__/article/details/21871987