solution
stringlengths
11
983k
difficulty
int64
0
21
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; char buf[1 << 21], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf; inline int read() { int x = 0, sign = 0; char s = getchar(); while (!isdigit(s)) sign |= s == '-', s = getchar(); while (isdigit(s)) x = (x << 1) + (x << 3) + (s - '0'), s = getchar(); return sign ? -x : x; } void print(int x) { if (x > 9) print(x / 10); (*O++ = x % 10 + '0'); } const int N = 3e5 + 5; long long n, l, r; void solve() { cin >> n >> l >> r; long long cnt = n, now = 1, tmp = 1; while (--cnt) { long long nw = tmp + cnt * 2 - 1; long long nl = max(l, tmp), nr = min(nw, r); if (nl <= nr) for (long long i = nl; i <= nr; i++) { if ((i - tmp) & 1) cout << now + (i - tmp + 1) / 2 << " "; else cout << now << " "; } tmp = nw + 1, now++; } if (r == tmp) cout << "1 "; puts(""); } int main() { int testcase = 1; cin >> testcase; while (testcase--) solve(); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; template <typename T> void trace(const char* name, T&& arg1) { cout << name << " : " << arg1 << endl; } template <typename T, typename... Args> void trace(const char* names, T&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; trace(comma + 1, args...); } const long long MOD = 1000 * 1000 * 1000 + 7; const long long NMAX = 10000 * 1000 + 111111; long long arr[NMAX]; long long n, m, p, q, k; vector<long long> ans; string str; char ch; vector<long long> v; void solve() { v.clear(); cin >> n; cin >> p >> q; long long left = 0; long long st; for (long long i = (n - 1); i >= 1; i--) { if (left + 2 * i < p) { left = left + 2 * i; continue; } else { st = (n - i); break; } } p = p - left; q = q - left; for (long long i = st; i < n; i++) { long long idx = i + 1; for (long long j = idx; j <= n; j++) { v.push_back(i); v.push_back(j); if (v.size() >= q) break; } } if (v.size() != q) v.push_back(1); for (long long i = p; i <= q; i++) { cout << v[i - 1] << " "; } cout << "\n"; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1; cin >> t; while (t--) { solve(); } }
10
CPP
#include <bits/stdc++.h> using namespace std; int t, n, i; long long l, r; void afis(int n, int i, int l, int r) { int val = n - i; for (int j = l; j <= r; j++) { if (j & 1) cout << val << ' '; else cout << val + (j / 2) << ' '; } return; } int main() { cin >> t; for (; t; t--) { cin >> n >> l >> r; for (i = n - 1; i >= 1; i--) if (l > 2 * i) l -= 2 * i, r -= 2 * i; else break; while (i) { if (r <= 2 * i) { afis(n, i, l, r); r = 0; break; } else { afis(n, i, l, 2 * i); l = 1, r -= 2 * i; } i--; } if (r) cout << 1; cout << '\n'; } return 0; }
10
CPP
n_tests = int(input()) for _ in range(n_tests): n_vertices, l, r = list(map(int, input().split())) index = 0 i_v = None for i_v in range(1, n_vertices): n_indexes_here = (n_vertices - i_v) * 2 if l <= index + n_indexes_here: break else: index += (n_vertices - i_v) * 2 else: print(1) continue index += 1 next_print = i_v + 1 while True: if index > r: break if index % 2 == 1: if index >= l: print(str(i_v), end=" ") else: if index >= l: print(next_print, end=" ") next_print += 1 if next_print > n_vertices: i_v += 1 if i_v == n_vertices: i_v = 1 next_print = i_v + 1 index += 1 print("")
10
PYTHON3
from types import GeneratorType def bootstrap(f, stack=[]): def wrappedfunc(*args, **kwargs): if stack: return f(*args, **kwargs) else: to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) else: stack.pop() if not stack: break to = stack[-1].send(to) return to return wrappedfunc def generateForStartVertex(startVertex,n): if startVertex==n: return [1] res=[] other=startVertex+1 while other<=n: res.append(startVertex) res.append(other) other+=1 return res @bootstrap def calc(l,r,startVertex,startIndex,n,res): nextStartIndex=startIndex+2*(n-startVertex) if startVertex==n: nextStartIndex+=1 currIdx=startIndex if l<nextStartIndex: #run calculation for this startVertex, else skip for x in generateForStartVertex(startVertex,n): if l<=currIdx<=r: res.append(x) currIdx+=1 if startVertex+1<=n and r>=nextStartIndex: # need to run next startVertex yield calc(l,r,startVertex+1,nextStartIndex,n,res) yield res def main(): t=int(input()) allans=[] for _ in range(t): n,l,r=readIntArr() res=[] calc(l,r,1,1,n,res) allans.append(res) multiLineArrayOfArraysPrint(allans) return #import sys #input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok) import sys input=lambda: sys.stdin.readline().rstrip("\r\n") #FOR READING STRING/TEXT INPUTS. def oneLineArrayPrint(arr): print(' '.join([str(x) for x in arr])) def multiLineArrayPrint(arr): print('\n'.join([str(x) for x in arr])) def multiLineArrayOfArraysPrint(arr): print('\n'.join([' '.join([str(x) for x in y]) for y in arr])) def readIntArr(): return [int(x) for x in input().split()] inf=float('inf') MOD=10**9+7 main()
10
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long mod = (long long)1e9 + 7; int t; long long n, L, R; long long a[500500], p[500500]; void solve() { cin >> n >> L >> R; a[1] = 2 * n - 2; for (int i = 2; i <= n - 1; ++i) a[i] = a[i - 1] - 2; a[n] = 1; for (int i = 1; i <= n; ++i) p[i] = p[i - 1] + a[i]; long long l = 0, r = n, res = -1; while (l <= r) { long long mid = (l + r) / 2; if (p[mid] >= L) { r = mid - 1; res = mid; } else { l = mid + 1; } } long long cnt = R - L + 1; bool done = false; long long start = L - p[res - 1]; if (res == n) { cout << 1 << "\n"; return; } for (long long i = start; i <= 2 * (n - res); ++i) { if (cnt == 0) { done = true; break; } if (i % 2 == 1) cout << res << " "; else cout << res + i / 2 << " "; --cnt; } if (done) { cout << "\n"; return; } for (long long level = res + 1; level <= n; ++level) { if (level == n) { cout << 1 << " "; cout << "\n"; return; } for (long long i = 1; i <= 2 * (n - level); ++i) { if (i % 2 == 1) cout << level << " "; else cout << level + i / 2 << " "; --cnt; if (cnt == 0) { cout << "\n"; return; } } } } int main() { ios_base ::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> t; while (t--) solve(); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int tcase; cin >> tcase; for (int tc = 1; tc <= tcase; tc++) { long long int n, l, r, count = 0, i = 0, j; cin >> n >> l >> r; while (count < l and count < n * (n - 1)) { count += 2 * (n - i - 1); i++; } if (count > l) { --i; count -= 2 * (n - i - 1); } if (count == n * (n - 1)) { if (l == count + 1) cout << "1\n"; else if (l == r) cout << n << '\n'; else cout << n << " 1\n"; } else { long long int dif = l - count, start; count = 0; if (dif == 0) { cout << n << ' '; count++; start = ++i + 1; } else if (dif % 2) start = (++i) + dif / 2 + 1; else { start = (++i) + dif / 2; cout << start << ' '; start++; count++; } for (j = start; j <= n; j++) { if (count == r - l + 1) break; cout << i << ' '; count++; if (count == r - l + 1) break; cout << j << ' '; count++; } for (i = i + 1; i <= n; i++) { for (j = i + 1; j <= n; j++) { if (count == r - l + 1) break; cout << i << ' '; count++; if (count == r - l + 1) break; cout << j << ' '; count++; } if (count == r - l + 1) break; } if (count != r - l + 1) cout << "1"; cout << '\n'; } } }
10
CPP
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long t; cin >> t; while (t--) { long long n, l, r; cin >> n >> l >> r; if (n == 1) { cout << "1" << endl; continue; } long long x = 2 * (n - 1); long long c = 0; r = r - l + 1; while (x > 0 && l - x > 0) { l -= x; c++; x -= 2; } long long f; for (long long i = c + 1;; i++) { f = 0; long long k = i; if (i == c + 1) { if (l % 2 == 1) { k += (l + 1) / 2; } else { k += l / 2; f = 1; } } else k++; while (k <= n) { if (!f) cout << i << " "; else { cout << k << " "; k++; } r--; if (r == 0) break; f = !f; } if (r == 0) break; if (i == n) { cout << "1"; break; } } cout << endl; } }
10
CPP
import sys t=int(sys.stdin.readline()) for _ in range(t): n,l,r=map(int,sys.stdin.readline().split()) prev=0 cur=0 start=1 if l==r and l==n*(n-1)+1: print(1) else: ans=[] while(True): cur+=(n-start)*2 if l<=cur: pos=l-prev total=r-l+1 if(pos%2==1): ans.append(start) total-=1 x=start+pos//2 +1 while(total>0): ans.append(x) if x==n: start+=1 if start==n: start=1 x=start total-=1 if total>0: ans.append(start) total-=1 x+=1 else: x=start+pos//2 while(total>0): ans.append(x) if x==n: start+=1 if start==n: start=1 x=start total-=1 if total>0: ans.append(start) total-=1 x+=1 break prev=cur start+=1 print(*ans)
10
PYTHON3
#include <bits/stdc++.h> using namespace std; template <typename T> inline T abs(T t) { return t < 0 ? -t : t; } const long long modn = 1000000007; inline long long mod(long long x) { return x % modn; } const int MAXN = 212345; int n, m, k; long long l, r; int s[MAXN]; vector<int> ans; long long stp; void add(int x) { if (stp >= l && stp <= r) ans.push_back(x); stp++; } int main() { int t; scanf("%d", &t); for (int tt = 1; tt <= t; tt++) { ans.clear(); scanf("%d%lld%lld", &n, &l, &r); stp = 1; int look = 2; while (look < n) { add(1); add(look); look++; } add(1); add(n); int base = 2; while (base < n) { add(base); if (stp > r) break; if (stp + 2ll * (n - 1 - base - 1 + 1) >= l) { for (int o = base + 1; o < n; o++) { add(o); add(base); } } else { stp += (long long)2ll * (n - 1 - base - 1 + 1); } add(n); base++; } add(1); for (int x : ans) printf("%d ", x); puts(""); } }
10
CPP
T = int(input()) for _ in range(T): n, left, right = map(int, input().split()) l = [0]*n l[1] = 2*n-2 for i in range(2, n): l[i] = l[i-1]-2 l[-1] += 1 left_n = -1 right_n = -1 acc = 0 for i in range(1, n): if acc+1<=left: left_n = i acc += l[i] if right<=acc: right_n = i break #print(l) #print(left_n) #print(right_n) ans = [] for i in range(left_n, right_n+1): now = [] for j in range(l[i]): if j%2==0: now.append(i) else: if j==1: now.append(i+1) else: now.append(now[-2]+1) if i==n-1: now[-1] = 1 ans += now sta = left-sum(l[:left_n])-1 print(*ans[sta:sta+right-left+1])
10
PYTHON3
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC optimize("no-stack-protector") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native") const long long MOD = 998244353; long long gcd(long long a, long long b) { if (a < b) { swap(a, b); } while (b != 0) { long long tmp = b; b = a % b; a = tmp; } return a; } signed main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); long long t = 1; cin >> t; while (t--) { long long n, l, r; cin >> n >> l >> r; l--; long long cur_plus = 0; long long org = (n - 1) * 2; long long prev_org = 0; while (org < l) { n--; prev_org = org; org += (n - 1) * 2; cur_plus++; } while (l < r) { if (l % 2 == 1) { cout << (l - prev_org) / 2 + 2 + cur_plus << ' '; l++; } for (l = l; l < r && l < org; l += 2) { cout << 1 + cur_plus << ' '; if (l + 1 < r) { cout << (l - prev_org) / 2 + 2 + cur_plus << ' '; } } n--; prev_org = org; org += (n - 1) * 2; if (n == 1 && l < r) { cout << 1 << ' '; break; } if (n == 1) { break; } cur_plus++; } cout << "\n" << endl; } }
10
CPP
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; long long powmod(long long a, long long b) { long long res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } const int N = 1e5 + 10; int n; long long l, r; long long sum[N]; int cal(long long x) { if (x > sum[n - 1]) return 1; int idx = lower_bound(sum, sum + n, x) - sum; int num = x - sum[idx - 1]; if (num & 1) return idx; return idx + num / 2; } int main() { int t; scanf("%d", &t); while (t--) { scanf("%d%lld%lld", &n, &l, &r); for (int i = 1; i < n; i++) { sum[i] = sum[i - 1] + 2ll * (n - i); } for (long long i = l; i <= r; i++) { printf("%d ", cal(i)); } puts(""); } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; void upgrade() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); } int main() { upgrade(); int tc; cin >> tc; while (tc--) { long long n, l, r; cin >> n >> l >> r; long long add = 2 * n - 2, cnt = 0, h = 1; while (add != 0 && cnt + add < l) { cnt += add; add -= 2; h++; } if (add == 0) { cout << 1 << '\n'; continue; } long long diff = l - cnt; bool f = (diff % 2) == 1; long long st = (diff - 1) / 2 + h + 1; for (int i = 0; i < r - l + 1; i++) { if (f) { cout << h << ' '; } else { cout << st << ' '; if (st == n) { h++; st = h + 1; if (h == n && i != r - l) { cout << 1; break; } } else st++; } f = !f; } cout << '\n'; } }
10
CPP
import math # решена def task_1343_c(): b = int(input()) array = [int(num) for num in input().split()] maxPositive = 0 minNegative = -10000000000 res = 0 for i in range(b): if array[i] < 0: if i != 0 and array[i - 1] >= 0: res += maxPositive maxPositive = 0 minNegative = max(minNegative, array[i]) else: if i != 0 and array[i - 1] < 0: res += minNegative minNegative = -10000000000 maxPositive = max(maxPositive, array[i]) if minNegative == -10000000000: res += maxPositive else: res += maxPositive + minNegative print(res) # не работает от слова совсем def task_1341_b(): heightLen, doorSize = map(int, input().split()) heights = [int(num) for num in input().split()] perf = [0 for i in range(heightLen)] a = 0 for i in range(heightLen - 1): if i == 0: perf[i] = 0 else: if heights[i - 1] < heights[i] and heights[i] > heights[i + 1]: a += 1 perf[i] = a perf[heightLen - 1] = a max_global = 0 left_global = 0 for i in range(heightLen - doorSize): max_local = perf[i + doorSize - 1] - perf[i] if max_local > max_global: max_global = max_local left_global = i print(max_global + 1, left_global + 1) # решил, чтоб её def task_1340_a(): n = int(input()) array = [int(i) for i in input().split()] for i in range(n - 1): if array[i] < array[i + 1]: if array[i] + 1 != array[i + 1]: print("No") return print("Yes") #решил def task_1339_b(): n = int(input()) array = [int(num) for num in input().split()] array.sort() output = [0 for i in range(0, n)] i = 0 h = 0 j = n - 1 while i <= j: output[h] = array[i] h += 1 i += 1 if h < n: output[h] = array[j] h += 1 j -= 1 for val in reversed(output): print(val, end=' ') # решена def task_1338_a(): n = int(input()) inputArr = [int(num) for num in input().split()] max_sec = 0 for i in range(1, n): local_sec = 0 a = inputArr[i - 1] - inputArr[i] if a <= 0: continue else: b = math.floor(math.log2(a)) local_sec = b + 1 for j in range(b, -1, -1): if a < pow(2, j): continue inputArr[i] += pow(2, j) a -= pow(2, j) if local_sec > max_sec: max_sec = local_sec print(max_sec) def task_1334_d(): n, l ,r = map(int, input().split()) res = [] count = 0 start_pos = l for i in range(1, n + 1): count += (n - i) * 2 if count >= l: for j in range(n - i): res.append(i) res.append(j + i + 1) else: start_pos -= (n - i) * 2 if count >= r: break res.append(1) for i in range(start_pos - 1, start_pos + (r - l)): print(res[i], end=" ") print() a = int(input()) for i in range(a): task_1334_d()
10
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long N = 3e5 + 5, K = 11, M = N * 4; const long long MOD = 998244353, oo = 1e9, OO = 1e18, mod = MOD; const double pi = acos(-1), eps = 1e-17; long long di[] = {0, 0, 1, -1}; long long dj[] = {1, -1, 0, 0}; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); ; long long t; cin >> t; while (t--) { long long n, l, r; cin >> n >> l >> r; long long now = 0, st = 1; long long sum = l; for (long long i = n - 1; i > 0; i--) { now = i * 2; l -= now; r -= now; if (l <= 0) { l += now; r += now; sum = i; break; } st++; } long long ans = 0; long long p = 0; for (long long i = sum; "Hello"; i--) { long long tmp = i * 2; long long dor = st; if (st == n) dor = 1, tmp = 1; long long cnt = st + 1; while (tmp--) { ans++; if (ans >= l && ans <= r) { cout << dor << " "; } if (dor == st) dor = cnt++; else dor = st; if (ans > r) goto A; } st++; if (ans > r) goto A; } A: cout << endl; } }
10
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = (int)1e6 + 100; const int mod = (int)1e9 + 7; void solve() { long long n, l, r; scanf("%lld%lld%lld", &n, &l, &r); long long pos = -1, pre = 0; for (auto i = (1); i <= (n); ++i) { long long now = (n - i) * 2; if (now == 0) now = 1; if (pre < l && pre + now >= l) { pos = i; break; } pre += now; } long long num = r - l + 1, st = l - pre, fr = pos == n ? 1 : pos, se = (st + 1) / 2 + pos; for (auto i = (1); i <= (num); ++i) { if (st % 2) printf("%lld ", fr > n ? 1 : fr); else printf("%lld ", se > n ? 1 : se), se++; st++; if (se > n) { pos++; st = 1; fr = pos; se = pos + 1; if (fr == n) fr = 1; if (se > n) se = 1; } } puts(""); } int main() { int T; cin >> T; while (T--) solve(); }
10
CPP
from sys import stdin,stdout from math import gcd,sqrt,factorial,pi,inf from collections import deque,defaultdict from bisect import bisect,bisect_left from time import time from itertools import permutations as per input=stdin.readline R=lambda:map(int,input().split()) I=lambda:int(input()) S=lambda:input().rstrip('\r\n') L=lambda:list(R()) P=lambda x:stdout.write(str(x)+'\n') lcm=lambda x,y:(x*y)//gcd(x,y) nCr=lambda x,y:(f[x]*inv((f[y]*f[x-y])%N))%N inv=lambda x:pow(x,N-2,N) sm=lambda x:(x**2+x)//2 N=10**9+7 for _ in range(I()): n,l,r=R() if l==r==(n-1)*n+1:print(1);continue p=0 for i in range(n-1,0,-1): p+=i*2 if p>=l: k=n-i if l%2==0: x=(n-i)+(l-(p-i*2))//2 print(x,end=' ') l+=1 j=x+1 if x==n: k+=1 j=k+1 else: j=(n-i)+(l+1-(p-i*2))//2 for i in range(r-l+1): if i%2: print(j,end=' ') if j==n: j=k+1 k+=1 j+=1 else: if k!=n:print(k,end=' ') else:print(1,end=' ') break print()
10
PYTHON3
import sys import heapq def input(): return sys.stdin.readline().strip() def iinput(): return int(input()) def rinput(): return map(int, input().split()) def rlinput(): return list(map(int, input().split())) def YES(flag): if flag: return "YES" return "NO" def main(): def F(): return l <= w and w <= r #n = int(sys.stdin.readline().strip()) n, l, r = rinput() res = [] w = 1 if F(): res.append(1) for i in range(2, n): w += 1 if F(): res.append(i) w += 1 if F(): res.append(1) w += 1 if F(): res.append(n) for i in range(2, n): w += 1 if F(): res.append(i) for o in range(max(w + 1, l), min(w + (n - i - 1) * 2 + 1, r + 1)): if (o - w) % 2 == 0: res.append(i) else: res.append((o - w - 1) // 2 + i + 1) w += (n - i - 1) * 2 + 1 if F(): res.append(n) w += 1 if F(): res.append(1) print(*res) for i in range(iinput()): main()
10
PYTHON3
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; ifstream fin("input.txt"); ofstream fout("output.txt"); long long fast_exp(long long base, long long exp) { long long res = 1; while (exp > 0) { if (exp % 2 == 1) res = (res * base) % 1000000007; base = (base * base) % 1000000007; exp /= 2; } return res % 1000000007; } int palindromecheck(string s) { int n = s.size(); for (int i = 0; i < n / 2; ++i) { if (s[i] != s[n - i - 1]) { return 0; } } return 1; } int gcd(int a, int b) { while (a && b) a > b ? a %= b : b %= a; return a + b; } int val(char c) { if (c >= '0' && c <= '9') return (int)c - '0'; else return (int)c - 'A' + 10; } long long pows(int a, int b) { long long res = 1; for (int i = 0; i < b; ++i) { res *= a; } return res; } long long logx(long long base, long long num) { int cnt = 0; while (num != 1) { num /= base; ++cnt; } return cnt; } long long divisibles(long long a, long long b, long long m) { if (a % m == 0) return (b / m) - (a / m) + 1; else return (b / m) - (a / m); } string bitstring(int n, int size) { string s; while (n) { s += (n % 2) + '0'; n /= 2; } while (s.size() < size) { s += '0'; } reverse(s.begin(), s.end()); return s; } vector<int> root(200001, 0); vector<int> size(200001, 1); int find(int x) { while (x != root[x]) x = root[x]; return x; } bool same(int a, int b) { return find(a) == find(b); } void unite(int a, int b) { a = find(a); b = find(b); if (size[a] < size[b]) swap(a, b); size[a] += size[b]; root[b] = a; } vector<int> vis(200001, 0); vector<int> adj[200001]; int main() { std::ios::sync_with_stdio(false); int t; cin >> t; while (t--) { int n; long long l, r; cin >> n >> l >> r; vector<long long> vec(n + 1); long long sum = 0; int j = 1; for (int i = n; i >= 1; --i) { sum += (i - 1) * 2; vec[j] = sum; if (i == 1) ++vec[j]; ++j; } long long idxl = lower_bound(vec.begin(), vec.end(), l) - vec.begin(); long long idxr = lower_bound(vec.begin(), vec.end(), r) - vec.begin(); if (vec[idxl] > l && idxl > 1) { --idxl; } vector<pair<long long, long long>> ans; int temp = vec[idxl]; if (idxl == 1) temp = 1; long long start; if (idxl != 1) start = vec[idxl - 1] + 1; for (int i = idxl; i <= idxr; ++i) { for (int j = i + 1; j <= n; ++j) { ans.push_back(make_pair(i, start++)); ans.push_back(make_pair(j, start++)); } if (i + 1 > n) { ans.push_back(make_pair(1, start)); } } long long begin; if (idxl == 1) { begin = l - 1; } else { for (int i = 0; i < ans.size(); ++i) { if (ans[i].second == l) { begin = i; break; } } } for (int i = begin; i <= begin + (r - l); ++i) cout << ans[i].first << " "; cout << "\n"; } }
10
CPP
import sys def minp(): return sys.stdin.readline().strip() def mint(): return int(minp()) def mints(): return map(int, minp().split()) def stupid(x, n): k = (n-1)*2 r = 0 while x >= k: x -= k r += 1 k -= 2 return r def findl(x, n): l, r = 0, n while r - l > 1: c = (l + r) // 2 if (2*n - 1 - c)*c <= x: l = c else: r = c #if l != stupid(x, n): # print(l, stupid(x, n), x, n); # raise 123 return l def solve(): n, l, r = mints() res = [] f = (r == n*(n-1) + 1) r -= l + f - 1 l -= 1 x = findl(l, n) l -= (2*n-1-x)*x k = (n-1-x)*2 x += 1 while False:#l >= k: print(l, k) l -= k x += 1 k -= 2 y = x + 1 + l // 2 if l % 2: while r >= 2: res.append(y) if y == n: x += 1 y = x + 1 else: y += 1 res.append(x) r -= 2 if r != 0: res.append(y) else: while r >= 2: res.append(x) res.append(y) if y == n: x += 1 y = x + 1 else: y += 1 r -= 2 if r != 0: res.append(x) if f: res.append(1) print(' '.join(map(str,res))) for i in range(mint()): solve()
10
PYTHON3
#include <bits/stdc++.h> using namespace std; using ll = long long int; int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; ll l, r; cin >> l >> r; ll idx = 1; bool done = false; for (int start = 1; start <= n; start++) { if (idx <= l && idx + (n - start) * 2ll > l) { int now = start; int nxt = start + 1; for (; idx < l; idx++) { int prev_now = now; now = nxt; nxt = (prev_now == start ? start : prev_now + 1); if (nxt == n + 1) { assert(now == start); now = ++start; nxt = start + 1; } } for (; idx <= r; idx++) { cout << now << " "; int prev_now = now; now = nxt; nxt = (prev_now == start ? start : prev_now + 1); if (nxt == n + 1) { assert(now == start); now = ++start; nxt = start + 1; if (now == n) { now = 1; } } } cout << endl; done = true; break; } else { idx += (n - start) * 2ll; } } if (!done) cout << 1 << '\n'; } }
10
CPP
#include <bits/stdc++.h> using namespace std; long long next() { long long x; cin >> x; return x; } const int maxn = 2e5 + 2, intf = 1e9; const long long inf = 2e18, mod = 1e9; const long double eps = 1e-6; long long n; void _print(long long bucket_id, long long l, long long r) { if (l > r) return; if (bucket_id == n) { cout << 1; return; } for (long long i = l; i <= r; i++) { if (i % 2 == 0) { cout << bucket_id << ' '; } else { cout << (i / 2) + 1 + bucket_id << ' '; } } } void solve() { long long l, r; cin >> n >> l >> r; l--, r--; long long sum = 0; for (long long bucket_id = 1; bucket_id <= n; bucket_id++) { long long cur_l = sum, cur_r = sum + 2 * (n - bucket_id) - 1; if (bucket_id == n) { if (r >= cur_l) { _print(bucket_id, 0, 1); } continue; } cur_l = max(cur_l, l) - sum; cur_r = min(cur_r, r) - sum; _print(bucket_id, cur_l, cur_r); sum += 2 * (n - bucket_id); } cout << '\n'; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout.precision(20); for (int t = next(); t--;) { solve(); } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; long long t, n, i, j, k, x, y, z, l, r, mid, q, m, ans, s; vector<int> v[200005]; long long bs(long long l, long long r, long long z) { long long ans = -1; while (l <= r) { mid = (l + r) >> 1; if ((n - 1 + n - mid) * (mid) >= z) { ans = mid; r = mid - 1; } else l = mid + 1; } if (ans == -1) x = 1; else { x = ans; z -= (n - 1 + n - ans + 1) * (ans - 1); y = (z + 1) / 2 + x; } return ans; } int main() { ios::sync_with_stdio(NULL); cin.tie(0); cout.tie(0); cin >> t; while (t--) { cin >> n >> l >> r; ans = bs(1, n - 1, l); while (l <= r) { if (l % 2) cout << x << " "; else cout << y << " ", y++; l++; if (y > n) { x++; y = x + 1; if (x == n) x = 1; } } cout << "\n"; } }
10
CPP
#!/usr/bin/env python # coding: utf-8 # In[48]: #from __future__ import print_function #from sys import stdin # In[52]: cases = int( input() ) # In[53]: def ecycle(n,l,r): l-=1 cnt = r - l p = n-1 start = 1 while(l >= 2*p and p>0): l -= 2*p p -= 1 start += 1 if(start==n): start = 1 flag = (l+1)%2 nextn = start + 1 + l//2 while(cnt>0): cnt-=1 if(flag==1): print(start,end=" ") if(flag==0): print(nextn, end=" ") nextn += 1 if(nextn>n): start += 1 nextn = start + 1 flag = 0 if(start==n): start = 1 flag = 1-flag # In[54]: while(cases>0): n,l,r = map( int, input().split() ) ecycle(n,l,r) cases -= 1 # In[ ]:
10
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t, i, j; long long n, l, r, pos; cin >> t; while (t--) { cin >> n >> l >> r; pos = 1; for (i = 1; i < n && pos < l; i++) { pos += 2 * (n - i); if (pos > l) { pos -= 2 * (n - i); break; } } for (; i < n; i++) { for (j = i + 1; j <= n; j++) { if (pos > r) break; if (pos < l) { if (pos == l - 1) cout << j << " "; pos += 2; } else { if (pos == r) { cout << i << " "; pos++; } else { cout << i << " " << j << " "; pos += 2; } } if (pos > r) break; } } if (r == pos) cout << "1"; cout << "\n"; } return 0; }
10
CPP
def check(x,n,l): val = 2*n*x-(x*x)-x if val < l: return True return False def solve(n,l,r,ans): low = 1 high = n-1 x = 0 while low <= high: mid = (low+high)//2 if check(mid,n,l): x = mid low = mid+1 else: high = mid-1 val = 2*n*x-(x*x)-x rem = l-val #print(x) temp = True prev = True for i in range(rem-1): if prev: if temp: x += 1 y = x else: y += 1 temp = False if y == n: temp = True prev = not prev #print(x,y) #print(temp,prev,y) arr = [] for i in range(r-l+1): if prev: if temp: x += 1 y = x if x == n: x = 1 arr.append(x) else: temp = False y += 1 arr.append(y) if y == n: temp = True prev = not prev ans.append(arr) def main(): t = int(input()) ans = [] for i in range(t): n,l,r = map(int,input().split()) solve(n,l,r,ans) for i in ans: for j in i: print(j,end = ' ') print() main()
10
PYTHON3
#include <bits/stdc++.h> using namespace std; long long n, t, l, r, start, star, have; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> t; while (t--) { cin >> n >> l >> r; have = 0; star = start = n + 1; for (long long i = 1; i <= n; i++) if (2LL * n * i - i * (i + 1LL) >= l) { start = i; have = 2LL * n * (i - 1LL) - (i - 1LL) * i; break; } for (long long i = start + 1; i <= n; i++) if (have + 2 < l) have += 2LL; else { have += 2LL; if (have == l) { cout << i << " "; l++; star = i + 1; } else star = i; break; } for (long long i = start; i <= n; i++) for (long long j = (i == start) ? star : i + 1; j <= n; j++) { if (l == r) { cout << i << " "; i = n + 1; break; } if (l > r) { i = n + 1; break; } l += 2; cout << i << " " << j << " "; } if (r == n * (n - 1LL) + 1) cout << "1"; cout << "\n"; } }
10
CPP
t = int(input()) for _ in range(t): n, l, r = map(int, input().split()) l -= 1 r -= 1 start_i = 1 pos = 0 while start_i < n: cur_i_len = 2 * (n-start_i) if pos + cur_i_len > l: break pos += cur_i_len start_i += 1 wanted_len = r-l+1 s = [] while len(s) < l-pos+wanted_len: if start_i < n: for i in range(start_i+1, n+1): s += [str(start_i), str(i)] else: s += ["1"] start_i += 1 print(*s[l-pos:l-pos+wanted_len])
10
PYTHON3
R = lambda:map(int, input().split()) t = int(input()) def block(b, pos, n): if pos%2 == 0: return b return b + (pos+1)//2 def binary(k, n): left, right = 1, n+1 while left <= right: m = (left + right)//2 if m*(m+1) >= k > m*(m-1): return m if m*(m+1) < k: left = m + 1 if m*(m-1) >= k: right = m - 1 def whatis(k, n): k = n*(n-1) + 1 - k if k == 0: return 1 i = binary(k, n) return block(n - i, - k + i*(i+1), n) for _ in range(t): n, l, r = R() ans = [] for i in range(l, r+1): ans.append(whatis(i, n)) print(*ans)
10
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int t; cin >> t; while (t--) { long long int n, l, r; cin >> n >> l >> r; long long int count = 0; long long int i = 1; while (i < n) { if (count + (2 * (n - i)) >= l) { long long int check = l - count; long long int start = i + ((check + 1) / 2); while (l <= r && i < n) { if (l % 2 == 0) { cout << start << " "; start++; } else { cout << i << " "; } if (start == n + 1) { i++; start = i + 1; } l++; } i = n; } count += 2 * (n - i); i++; } if (l <= r) { cout << 1; } cout << endl; } }
10
CPP
#include <bits/stdc++.h> const double PI = 3.141592653589793238462643383279502884197169399375105820974944; using namespace std; long long ModExp(long long x, long long y, long long m) { long long res = 1; x = x % m; while (y > 0) { if (y & 1) res = (res * x) % m; y = y >> 1; x = (x * x) % m; } return res; } void solve() { long long n, l, r; cin >> n >> l >> r; long long f = (n - 1) * 2; long long arr[((n - 1) * 2) + 1]; for (long long i = 1; i < f + 1; i++) { if (i % 2 == 1) { arr[i] = 1; } else { arr[i] = (i / 2) + 1; } } if (!(f < l || 1 > r)) { long long i1 = max(l - 1, 0LL); long long i2 = min(r - 1, f - 1); for (long long i = i1; i < i2 + 1; i++) { cout << arr[i + 1] << " "; } } long long ind = f + 1; for (long long i = 2; i < n + 1; i++) { long long sz = (n - i) * 2; long long st = ind; long long en = st + sz - 1; if (en < l) { } else if (st > r) { break; } else { long long i1 = max(l - st, 0LL); long long i2 = min(r - st, en - st); while (i1 <= i2) { if (i1 % 2 == 0) { cout << i << " "; } else { cout << ((i1 + 1) / 2) + i << " "; } i1++; } } ind = en + 1; } if (r == ((n * (n - 1)) + 1)) { cout << "1"; } cout << endl; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; long long t = 1; cin >> t; while (t--) { solve(); } }
10
CPP
#include <bits/stdc++.h> using namespace std; const long long INF = 1e17 + 9; long long n, l, r; void calc(long long ly, long long faltl, long long faltr) { if (ly == n) { r = 1; } else if (faltl >= 2 * n - ly * 2) { calc(ly + 1, faltl - 2 * n + ly * 2, faltr); } else { long long ind = ly + 1; long long guard = 0; for (long long i = 0; i < 2 * n - ly * 2 && faltl > 0; i++) { if (i % 2) ind++; faltl--; guard = i + 1; } for (long long i = guard; i < 2 * n - ly * 2 && faltr > 0; i++) { if (i % 2) cout << ind++ << " "; else cout << ly << " "; faltr--; } if (faltr > 0 && ly < n - 1) { calc(ly + 1, faltl, faltr); } else { r = faltr; } } } int main() { ios::sync_with_stdio(false); cin.tie(0); long long t; cin >> t; while (t--) { cin >> n >> l >> r; calc(1, l - 1, r - l + 1); if (r == 1) cout << 1 << '\n'; else cout << '\n'; } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; long long int power_mod(long long int a, long long int x) { if (x == 0) return 1; long long int y = power_mod(a, x / 2); long long int ans = (y * y) % 1000000007; if (x % 2) ans = (ans * a) % 1000000007; return ans; } long long int inv(long long int a) { return power_mod(a, 1000000007 - 2); } long long int power(long long int a, long long int x) { if (x == 0) return 1; long long int y = power(a, x / 2); long long int ans = (y * y); if (x % 2) ans *= a; return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { long long int n, l, r; cin >> n >> l >> r; long long int curr = 1; vector<long long int> v(n + 1); long long int sum = 0; long long int index = 1; vector<long long int> ans; for (long long int i = 1; i <= n - 1; i++) { if (sum + 2 * (n - i) >= l) { long long int curr = i; long long int curr_next = i + 1; long long int count = 2 * (n - i); for (long long int j = sum + 1; j <= min(n * (n - 1), r); j++) { if (j >= l) { ans.push_back(curr); } count--; if (count == 0) { i++; curr = i; curr_next = i + 1; count = 2 * (n - i); continue; } j++; if (j > r) break; if (j >= l) { ans.push_back(curr_next); } curr_next++; count--; if (count == 0) { i++; curr = i; curr_next = i + 1; count = 2 * (n - i); continue; } } break; } else { sum = sum + (2 * (n - i)); } } if (r == (n * (n - 1)) + 1) ans.push_back(1); for (auto i : ans) cout << i << " "; cout << endl; } }
10
CPP
#include <bits/stdc++.h> using namespace std; inline long long read() { register char c = ' '; register long long v = 0, x = 1; while ((c < '0') || (c > '9')) { if (c == '-') x = -1; c = getchar(); } while ((c >= '0') && (c <= '9')) { v = (v << 1) + (v << 3) + (c ^ 48); c = getchar(); } return v * x; } inline void write(register long long x) { if (x < 0) x = -x, putchar('-'); if (x > 9) write(x / 10); putchar(x % 10 ^ 48); } long long t, n, l, r, x, y; inline void calc(register long long m) { if (m > n * (n - 1) / 2) return (void)(x = y = 1ll); register long long a = 0, b = 0; while (a < m) a += n - 1 - b, b++; a -= n - b, x = b, y = m - a + b; } inline void nxt() { (y != n) ? (y++) : (x++, y = x + 1); if (x == n) x = 1; } int main() { t = read(); while (t--) { n = read(), l = read(), r = read(), calc((l + 1) >> 1); for (register long long i = l; i <= r; i++, putchar(' ')) (i & 1) ? write(x) : (write(y), nxt()); putchar('\n'); } return ~~(0 - 0); }
10
CPP
for _ in range(int(input())): n, left, right = tuple(map(int, input().split())) maximal = n * (n - 1) + 1 if left == maximal: print('1') continue if right == maximal: end_1 = True else: end_1 = False a = [] summa = 0 i = (n - 1) * 2 cnt = 0 while summa + i < left: summa += i i -= 2 cnt += 1 left -= summa right -= summa summa = 0 # if cnt == 0: while left <= right: while left <= summa + i and left <= right: if left % 2 == 1: print(cnt + 1, end=' ') else: print(cnt + left // 2 + 1, end=' ') left += 1 left -= i right -= i summa = 0 i -= 2 cnt += 1 # print('!', i, sep='', end=' ') if i == 0 and end_1: print(1, end=' ') break print() # else: if left > right: continue # while left <= right: # start =
10
PYTHON3
def main(): for j in range(int(input())): n,l,r=map(int,input().split()) b=1 for i in range(1,n): a=b b+=2*(n-i) if l < b: break x,y=i,(l-a)//2+i+1 b=(l-a)%2 for _ in range(r-l):# Imprimo solo la cantidad necesaria segun la entrada que me pasen if b: print(y,end=" ") y +=1 if y==n+1: x+=1 y=x+1 else: print(x,end=" ") b^=1 if r==n*(n-1)+1: print(1) else: print(y if b else x) if __name__=="__main__": main()
10
PYTHON3
#include <bits/stdc++.h> using namespace std; int gcdExtended(int a, int b, int *x, int *y) { if (a == 0) { *x = 0, *y = 1; return b; } int x1, y1; int gcd = gcdExtended(b % a, a, &x1, &y1); *x = y1 - (b / a) * x1; *y = x1; return gcd; } long long int modInverse(int a, int m) { int x, y; int g = gcdExtended(a, m, &x, &y); long long int res = ((long long int)x % m + (long long int)m) % m; return res; } long long int poww(long long int x, long long int n) { if (n == 0) return 1; else if (n == 1) return x; else if (n % 2 == 0) return poww(x * x, n / 2); else return x * poww(x * x, (n - 1) / 2); } long long int modularExponentiation(long long int x, long long int n, long long int M) { long long int result = 1; while (n > 0) { if (n % 2 == 1) result = (result * x) % M; x = (x * x) % M; n = n / 2; } return result; } template <typename T> T maxx(T a, T b) { if (a < b) return b; return b; } template <typename T, typename... Args> T maxx(T a, T b, Args... args) { return (maxx(maxx(a, b), args...)); } template <typename T> T minn(T a, T b) { if (a < b) return a; return b; } template <typename T, typename... Args> T minn(T a, T b, Args... args) { return (minn(minn(a, b), args...)); } int gcd(int a, int b) { if (a == 0) return b; if (b == 0) return a; if (a == b) return a; if (a > b) return gcd(a - b, b); else return gcd(a, b - a); } long long int ncr(int n, int r) { long long int ans = 1; r = min(r, n - r); for (int i = 0; i < r; i++) { ans = (ans * (n - i) / (i + 1)) % 1000000007; } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); int t = 1; cin >> t; while (t--) { long long int n, m, k, l, r; cin >> n >> l >> r; l--; m = 0; k = 1; while (m + (n - k) * 2 <= l && k < n) { m += (n - k) * 2; k++; } long long int temp = m; vector<long long int> v; while (m <= r) { long long int z = k; if (k != n) { for (int i = 0; i < (n - k); i++) { m += 2; v.push_back(k); v.push_back(++z); } k++; } else { v.push_back(1); break; } } for (int i = 0; i < r - l; i++) { cout << v[l - temp + i] << " "; } cout << '\n'; } return 0; }
10
CPP
#!/usr/bin/env python from __future__ import division, print_function import os import sys from io import BytesIO, IOBase if sys.version_info[0] < 3: from __builtin__ import xrange as range from future_builtins import ascii, filter, hex, map, oct, zip def solve(): n, l, r = [int(s) for s in input().strip().split()] if l == n * (n - 1) + 1: print(1) return i = 1 cnt = 0 while l > cnt + (n - i) * 2: cnt += (n - i) * 2 i += 1 A = [] while r > cnt + len(A): if i == n: A.append(1) else: A.extend([x for j in range(i + 1, n + 1) for x in [i, j]]) i += 1 result = A[l - cnt - 1:r - cnt] print(" ".join(str(x) for x in result)) def main(): for _ in range(int(input().strip())): solve() # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") def print(*args, **kwargs): """Prints the values to a stream, or to sys.stdout by default.""" sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": main()
10
PYTHON3
#include <bits/stdc++.h> using namespace std; const int SIZE = 1e5 + 9; long long arr[SIZE]; int main() { int t; cin >> t; while (t--) { long long n, l, r; cin >> n >> l >> r; long long x = 2 * (n - 1); arr[1] = x; for (int i = 2; i < n; i++) { x -= 2; arr[i] = arr[i - 1] + x; } arr[n] = arr[n - 1] + 1; for (int i = 1; i <= n; i++) { if (l <= arr[i]) { long long d = arr[i] - l + 1; long long st = n - d / 2; if (d % 2 == 0) { cout << i << " "; st++; l++; } while (l <= r) { if (l % 2 != 0) { if (i == n) i = 1; cout << i << " "; st++; } else { cout << st << " "; if (st == n) { i = (i + 1 == n ? 1 : i + 1); st = i; } } l++; } break; } } cout << endl; } }
10
CPP
import sys # 2 -> 1 2 1 # 3 -> 1 2 1 3 2 3 1 # 4 -> 1 2 1 3 1 4 2 3 2 4 3 4 1 # 5 -> 1 2 1 3 1 4 1 5 2 3 2 4 2 5 3 4 3 5 4 5 1 # for X(k, 1): first 2(k-1) are 1, i; then (k-1)(k-2) are X(k-1, 2) ; then 1 # 2(k-1), 2(k-2), 2(k-3) ... def binsearch(i, n): last = n*(n-1) ip = n*(n-1) - i #print(f"binsearch({i},{n}) last={last} ip={ip}") jprev = 0 j = n-1 while True: #print(f"j={j}, jprev={jprev}") #print(f"{j} {last-j*(j-1)} {last-(j-2)*(j-1)} ") step = max(1, abs(jprev-j)//2) jprev = j if j*(j-1) < ip: j += step elif j > 1 and (j-1)*(j-2) >= ip: j -= step else: break k = i - (last - j*(j-1)) return j, k def segment(n, l, r): res = [] j1, k1 = binsearch(l-1, n) j2, k2 = binsearch(r-1, n) # print(f"from {j1, k1} to {j2, k2}") for j in range(j1, j2-1, -1): for k in range(2*(j-1)): if not (j==j1 and k<k1) and not (j==j2 and k>k2): if k % 2 == 0: res.append(n-j+1) else: res.append(k // 2 + n-j + 2) if j2 == 1 and k2 == 0: res.append(1) return res t = int(input().strip()) for _ in range(t): n, l, r = list(map(int, input().strip().split())) print(' '.join(map(str, segment(n, l, r))))
10
PYTHON3
#!/usr/bin/env python3 import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n, l, r = map(int, input().split()) num = r - l + 1 init_num = 1 index = 0 for i in range(1, n): init_num = i if l <= (n - i) * 2: index = l l = 0 break else: l -= (n - i) * 2 # Last block if l > 0: print(1) continue ans = [] for i in range(1, n): if i < init_num: continue for j in range((n - i) * 2): if i == init_num and j < index - 1: continue elif j % 2 == 0: ans.append(i) num -= 1 else: ans.append((j+1) // 2 + i) num -= 1 if num == 0: break if num == 0: break if num > 0: ans.append(1) print(*ans)
10
PYTHON3
# Legends Always Come Up with Solution # Author: Manvir Singh import os import sys from io import BytesIO, IOBase from math import floor,sqrt def main(): for _ in range(int(input())): n,l,r=map(int,input().split()) if l== n*(n-1)+1: print(1) continue i=1 cnt=0 while l > cnt + (n - i) * 2: cnt += (n - i) * 2 i += 1 A = [] while r > cnt + len(A): if i == n: A.append(1) else: A.extend([x for j in range(i + 1, n + 1) for x in [i, j]]) i += 1 result = A[l - cnt - 1:r - cnt] print(" ".join(str(x) for x in result)) # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") if __name__ == "__main__": main()
10
PYTHON3
t = int(input()) for i in range(t): n, l, r = map(int, input().split()) if l == n * (n - 1) + 1: print(1) else: x = 1 summa = x * 2 * n rasn = x * (x + 1) while summa - rasn < l: summa += 2 * n rasn = (rasn // x) * (x + 2) x += 1 x -= 1 summa -= 2 * n rasn = (rasn // (x + 2)) * x first = x + 1 second = (l - summa + rasn + 1) // 2 + first if (l - summa + rasn) % 2 == 0: print(second, end=" ") if second == n: first += 1 second = first + 1 else: second += 1 ind = l + 1 else: ind = l while ind + 1 <= r: print(first, second, end=" ") if second == n: first += 1 second = first + 1 else: second += 1 ind += 2 if ind == r: if r == n * (n - 1) + 1: print(1) elif second == n: print(first) else: print(first) else: print()
10
PYTHON3
from sys import stdin, gettrace from math import sqrt if not gettrace(): def input(): return next(stdin)[:-1] # def input(): # return stdin.buffer.readline() def main(): def solve(): n,l,r = map(int, input().split()) lv = int((2*n+1 - sqrt((2*n-1)**2 -4*(l-1)))/2) lvs = -2*n+2*n*lv-lv*lv+lv lrd = l - lvs - 1 res = [] i = lv j = lv+lrd//2 + 1 if l%2 == 0: res = [j] if j < n: j+=1 else: i+=1 j = i+1 for _ in range(l-1, r, 2): res += [i,j] if j < n: j += 1 else: i +=1 j = i+1 if r == n*(n-1)+1: res[r-l] = 1 print(' '.join(map(str, res[:r-l+1]))) q = int(input()) for _ in range(q): solve() if __name__ == "__main__": main()
10
PYTHON3
#include <bits/stdc++.h> using namespace std; int T; long long N; long long L, R; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> T; for (long long t = 0; t < T; t++) { cin >> N >> L >> R; if (L == N * (N - 1) + 1 && L == R) { cout << "1\n"; continue; } long long ini = 0; long long s = 1; long long len = N - 1; while (ini + 2 * len < L) { ini += 2 * len; len--; s++; } ini++; long long first = s; long long second = s + 1; while (ini < L) { if (ini % 2 == 0) second++; if (second > N) first++, second = first + 1; ini++; } while (ini <= R && ini < N * (N - 1) + 1) { if (ini % 2) cout << first << " "; else cout << second << " "; if (ini % 2 == 0) second++; if (second > N) first++, second = first + 1; ini++; } if (R == N * (N - 1) + 1) cout << "1 "; cout << "\n"; } return 0; }
10
CPP
from sys import stdin input = stdin.buffer.readline for _ in range(int(input())): n, l, r = map(int, input().split()) l -= 1 r -= 1 flag, tmp, s = 1, 0, 0 ans = [] x, y = n, n - 1 for i in range(1, n): s += 2 * (n - i) if l < s and flag: x, tmp = i, (i > 1) * (s - 2 * (n - i)) l -= tmp flag = 0 if r < s: y = i r -= tmp break if x > y: l, r = 0, 0 for i in range(x, y + 1): for j in range(i + 1, n + 1): ans.append(i) ans.append(j) #if r >= len(ans) and y != n - 1: # print(0/0) ans.append(1) print(*ans[l : r + 1])
10
PYTHON3
#include <bits/stdc++.h> using namespace std; long long power(long long x, long long y, long long md = 1000000007LL) { long long res = 1; x %= md; while (y > 0) { if (y & 1) res = (res * x) % md; x *= x; if (x >= md) x %= md; y >>= 1; } return res; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long t = 1; cin >> t; while (t--) { long long n, l, r; cin >> n >> l >> r; vector<long long> ans; long long prev = 1; for (long long i = 1; i <= n; i++) { long long st = prev; long long end = prev + 2 * (n - i) - 1; if (l > end) { prev = end + 1; continue; } if (r < st) break; long long st1 = max(l, st); long long en1 = min(r, end); for (long long j = st1; j <= en1; j++) { if ((j - st) % 2 == 0) ans.emplace_back(i); else { long long ex = (j - st + 1) / 2; ans.emplace_back(i + ex); } } prev = end + 1; } if (r == prev) ans.emplace_back(1); for (auto &x : ans) cout << x << " "; cout << '\n'; } }
10
CPP
#include <bits/stdc++.h> using namespace std; using namespace std; inline int qpow(int b, int e, int m = 998244353) { int a = 1; for (; e; e >>= 1, b = (long long)b * b % m) if (e & 1) a = (long long)a * b % m; return a; } int n, m, q, k; int a[300005], b[300005], c[300005]; const int pp[11] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; void solve() { long long n, l, r; cin >> n >> l >> r; vector<long long> v(n + 1); v[1] = 1; for (long long i = 2; i < n; i++) { v[i] = v[i - 1] + 2 * (n - (i - 1)); } v[n] = v[n - 1] + 2; auto itr1 = upper_bound(v.begin(), v.end(), l); auto itr2 = upper_bound(v.begin(), v.end(), r); if (itr1 == v.end()) { cout << 1 << endl; } else { int in1 = -1; int in2 = -1; auto k1 = itr1; auto k2 = itr2; int st1; st1 = (--k1) - v.begin(); int st2; st2 = k2 - v.begin(); long long i = v[st1]; while (i <= r && i < v[n]) { for (int j = 0; j < n - st1; j++) { if (i >= l && i <= r) cout << st1 << " "; i++; if (i >= l && i <= r) cout << st1 + j + 1 << " "; i++; } st1++; } if (r == v[n]) cout << 1 << endl; else cout << endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t; cin >> t; while (t--) solve(); return 0; }
10
CPP
t = int(input()) for y in range(t): n,l,r = list(map(int,input().split())) if(l == n*(n-1)+1): print(1) continue ind = 1 ct = 1 while(1): if(l < ct): break ct += (n-ind)*2 ind += 1 ind -= 1 if (ct-l)%2 == 0: st = ind print(st,end = " ") l += 1 st = n+1 for i in range(ct-1,l-1,-2): st -= 1 for i in range(l,r+1): if(i%2 == 1): print(ind,end = " ") else: print(st,end = " ") st += 1 if(st == n+1): ind += 1 if(ind == n): ind = 1 st = ind+1 print()
10
PYTHON3
from sys import stdin, gettrace def input(): return stdin.buffer.readline() t = int(input()) for _ in range(t): n, l, r = list(map(int, input().split())) count = 0 i = 1 while count < l: if i == n: i = 1 count += (n - i) * 2 i += 1 i -= 1 count -= (n - i) * 2 j = i + (l - count + 1) // 2 length = r - l + 1 if (l % 2) == 0: print(j, end=' ') j += 1 length -= 1 if j > n: i += 1 j = i + 1 # print("before while i: {}, j: {}, length is {}".format(i, j, length)) while length > 0: if i == n: i = 1 print(i, end=' ') length -= 1 if length <= 0: break print(j, end=' ') length -= 1 j += 1 if j > n: i += 1 j = i + 1 print() # 1 2 1 3 1 4 1 5 ... 1 n 2 3 2 4 2 5 ... 2 n 3 1 ... 3 n ... n-1 1 ... n-1 n 1 # 1 2 1 3 1 4 1 5. 2 3 2 4 2 5. 3 4 3 5. 4 5. 1
10
PYTHON3
import math import sys A = 1 B = 1 C = 1 K = 0 def LFromI(I): return I*I*A + I*B + C def IFromL(L): return math.floor((-B+math.sqrt(B*B - 4*A*(C-L)))/(2*A)) def Out(l, R): curint = IFromL(l) curpos = LFromI(curint) while curpos <= R: if curint == K: print(1, end=' ') break for i in range(2*(K-curint)): if curpos+i > R: break if curpos+i < l: continue if i % 2 == 0: print(curint, end=' ') else: print(curint + (i+1)//2, end=' ' ) curint += 1 curpos = LFromI(curint) return 1; t = int(input()) for _ in range(t): K, l, r = map(int, input().split()) A = -1 B = 2*K+1 C = -2*K Out(l-1, r-1) print()
10
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); int t = 1; cin >> t; while (t--) { long long n, l, r; cin >> n >> l >> r; bool f = 0; if (r == (n * (n - 1) + 1)) { r--; f = 1; } long long ans1 = (n - 1) * 2; long long sum = ans1, pos = 0; for (long long i = 1; i < n; i++) { if (l <= ans1) { pos = i; break; } l -= ans1; r -= ans1; ans1 -= 2; } long long pos2 = (l + 1) / 2 + pos; while (l <= r) { if (l % 2 == 0) { cout << pos2 << " "; pos2++; } else cout << pos << " "; if (pos2 == n + 1) { pos++; pos2 = pos + 1; } l++; } if (f == 1) cout << 1 << " "; cout << endl; } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; long long int max(long long int a, long long int b) { if (a >= b) return a; else return b; } long long int min(long long int a, long long int b) { if (a <= b) return a; else return b; } void solve() { long long int n, l, r; cin >> n >> l >> r; if (l == (1LL * n * (n - 1)) + 1) { cout << 1 << endl; return; } long long int sum = 0; long long int i; for (i = n - 1; i >= 1; i--) { sum += 1LL * 2 * i; if (sum >= l) { break; } } vector<long long int> ans; long long int total = r - l + 1; if (r == (1LL * n * (n - 1)) + 1) total--; long long int count = sum - l + 1; long long int a, b; if ((count % 2) == 0) { a = n - i; b = n - (count / 2) + 1; } else { ans.push_back(n - ((count) / 2)); a = n - i; b = n - (count / 2) + 1; total--; } if (count == 1) { a++; b = a + 1; } while (total > 0) { if (b == n + 1) { a++; b = a + 1; } ans.push_back(a); total--; if (total == 0) break; ans.push_back(b); total--; b++; } if (r == (1LL * n * (n - 1)) + 1) ans.push_back(1); for (auto it : ans) { cout << it << " "; } cout << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int t; cin >> t; while (t--) { solve(); } return 0; }
10
CPP
from bisect import bisect_left def get(x, n): if x==n: return [1] return [a for i in range(x+1,n+1) for a in [x,i]] def solve(): n,l,r = map(int,input().split()) cum = [0]*(n+1) for i in range(1,n): cum[i] = cum[i-1] + 2*(n-i) cum[n] = cum[n-1] + 1 xL = bisect_left(cum, l) xR = bisect_left(cum, r) hL = l - cum[xL-1] - 1 hR = r - cum[xR-1] - 1 if xL==xR: u = get(xL, n) print(*u[hL:hR+1]) return uL, uR = get(xL, n), get(xR, n) print(*uL[hL:], end=" ") for x in range(xL+1, xR): print(*get(x, n), end=" ") print(*uR[:hR+1]) return for _ in range(int(input())): solve()
10
PYTHON3
#include <bits/stdc++.h> using namespace std; long long t, n, l, r; void stampa(int x, int y) { if (r == 0) return; cout << x << " "; r--; if (x == n && y >= n) return; int succ; if (y > x) { succ = x + (y == n); } else { if (x == n) { succ = y + 1; } else { succ = x + 1; } } stampa(y, succ); } int main() { cin >> t; while (t--) { cin >> n >> l >> r; r = (r - l + 1); for (long long i = 1; i <= n; i++) { if (l > ((n - i) * 2ll)) { l -= ((n - i) * 2ll); } else { if (l % 2 == 0) stampa(i + l / 2, i + ((i + l / 2 == n))); else stampa(i, i + (l + 1) / 2); break; } } if (r > 0) cout << "1"; cout << "\n"; } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; long long n; void prnt(long long a, long long b, long long cnt) { if (cnt) printf("%d ", a); if (cnt < 2) return; printf("%d ", b); if (b != n) prnt(a, b + 1, cnt - 2); else if (a != n - 1) prnt(a + 1, a + 2, cnt - 2); else if (cnt > 2) printf("1"); } long long arth(long long st, long long en, long long n) { return n * (st + en) / 2; } long long clc(long long m) { return arth((n - 1) * 2, (n - m) * 2, m); } long long bs(long long lft) { long long l = 1, r = n - 1, m; while (l < r) { m = (l + r) / 2; long long c = clc(m); if (lft > c) l = m + 1; else r = m; } return l; } void fnd(long long lft, long long rit) { long long l1 = bs(lft); long long l2 = bs(lft + 1); if (l1 == l2) { long long nl = lft; if (l1 > 1) nl -= clc(l1 - 1); long long k = nl / 2; if (nl % 2 == 0) cout << k + l1 << " ", lft++; prnt(l1, k + l1 + 1, rit - lft + 1); } else { cout << n << " "; prnt(l2, l2 + 1, rit - lft); } } int main() { long long t; scanf("%lld", &t); while (t--) { scanf("%lld", &n); long long lft, rit; scanf("%lld%lld", &lft, &rit); long long mx = n * (n - 1) + 1; if (lft == mx) cout << 1 << endl; else if (lft + 1 == mx) { cout << n; if (rit == mx) cout << " " << 1 << endl; } else { fnd(lft, rit); cout << endl; } } }
10
CPP
#include <bits/stdc++.h> using namespace std; void file() {} signed main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); ; long long t; cin >> t; while (t--) { long long n, l, r; cin >> n >> l >> r; vector<long long> a; long long cnt = 0, flag = 0; for (long long i = 1; i <= n && cnt < r; i++) { if (cnt < l && l <= cnt + (n - i) * 2) { flag = 1; } if (flag) { for (long long j = i + 1; j <= n; j++) { a.push_back(i); cnt++; if (cnt == r) break; a.push_back(j); cnt++; if (cnt == r) break; } } else cnt += (n - i) * 2; } if (r == n * (n - 1) + 1) a.push_back(1); long long len = a.size(); for (long long i = len - (r - l + 1); i < len; i++) cout << a[i] << " "; cout << "\n"; } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int T, n; long long l, r; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> T; while (T > 0) { --T; cin >> n >> l >> r; long long s = 0; for (int i = 1; i <= n; ++i) { long long t = s + 1ll * 2 * (n - i); if (t < l) { s = t; continue; } if (s < l && t >= l) { if (l % 2 == 1) { int L = (l - s) / 2 + 1 + i; if (t >= r) { int R = (r - s) / 2 + i; for (int j = L; j <= R; ++j) { cout << i << " " << j << " "; } if ((r - s) % 2 == 1) cout << i << " "; s = t; break; } for (int j = L; j <= n; ++j) { cout << i << " " << j << " "; } } else { int L = (l - s) / 2 + i; cout << L << " "; if (t >= r) { int R = (r - s) / 2 + i; for (int j = L + 1; j <= R; ++j) { cout << i << " " << j << " "; } if ((r - s) % 2 == 1) cout << i << " "; s = t; break; } for (int j = L + 1; j <= n; ++j) { cout << i << " " << j << " "; } } s = t; continue; } if (t < r) { for (int j = i + 1; j <= n; ++j) { cout << i << " " << j << " "; } s = t; continue; } if (s < r && t >= r) { int R = (r - s) / 2 + i; for (int j = i + 1; j <= R; ++j) { cout << i << " " << j << " "; } if ((r - s) % 2 == 1) cout << i << " "; s = t; } } if (s < r) cout << 1; cout << '\n'; } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; long long arr[200005]; long long solve(long long x, long long n) { long long ind = lower_bound(arr + 1, arr + n, x) - arr; x -= arr[ind - 1]; if (x & 1) return ind; return x / 2 + ind; } int main() { long long t; cin >> t; while (t--) { long long n, l, r; cin >> n >> l >> r; arr[0] = 0; for (long long i = 1; i <= n; i++) { arr[i] = arr[i - 1] + (n - i) * 2; } for (long long i = l; i <= r; i++) { if (i == n * (n - 1) + 1) cout << 1 << " "; else cout << solve(i, n) << " "; } cout << endl; } }
10
CPP
#include <bits/stdc++.h> using namespace std; const long long N = 500005; const long long M = 1000000007; int main() { long long t; cin >> t; while (t--) { long long n, l, r; cin >> n >> l >> r; vector<long long> v; v.push_back(1); for (long long i = 1; i < n; i++) { v.push_back(2 * i); } reverse(v.begin(), v.end()); vector<long long> v1; v1.push_back(v[0]); long long sum = v[0]; for (long long i = 1; i < n; i++) { sum += v[i]; v1.push_back(sum); } vector<long long>::iterator it = lower_bound(v1.begin(), v1.end(), l); vector<long long>::iterator it1 = lower_bound(v1.begin(), v1.end(), r); long long z = it - v1.begin(); long long z1 = it1 - v1.begin(); long long num = v1[z]; long long num1 = v1[z1]; if (z == n - 1) { cout << 1; } else { long long chk = 0; long long a, b, c, d; long long w = num - l; b = n - w / 2; a = z + 1; if (z1 == n - 1) { chk = 1; } else { c = z1 + 1; long long w = num1 - r; d = n - w / 2; } if (chk == 1) { if (l % 2 == 0) { cout << b << " "; long long nu = b + 1; while (nu <= n) { cout << a << " " << nu << " "; nu++; } } else { long long nu = b; while (nu <= n) { cout << a << " " << nu << " "; nu++; } } for (long long j = a + 1; j < n; j++) { long long nu = j + 1; while (nu <= n) { cout << j << " " << nu << " "; nu++; } } cout << 1 << " "; } else if (a == c) { if (l % 2 == 0 && r % 2 == 0) { cout << b << " "; long long nu = b + 1; while (nu <= d) { cout << a << " " << nu << " "; nu++; } } else if (l % 2 == 0 && r % 2 != 0) { cout << b << " "; long long nu = b + 1; while (nu < d) { cout << a << " " << nu << " "; nu++; } cout << a << " "; } else if (l % 2 != 0 && r % 2 == 0) { long long nu = b; while (nu <= d) { cout << a << " " << nu << " "; nu++; } } else { long long nu = b; while (nu < d) { cout << a << " " << nu << " "; nu++; } cout << a << " "; } } else { if (l % 2 == 0) { cout << b << " "; long long nu = b + 1; while (nu <= n) { cout << a << " " << nu << " "; nu++; } } else { long long nu = b; while (nu <= n) { cout << a << " " << nu << " "; nu++; } } if (a + 1 < c) { for (long long j = a + 1; j < c; j++) { long long nu = j + 1; while (nu <= n) { cout << j << " " << nu << " "; nu++; } } } if (r % 2 == 0) { long long nu = c + 1; while (nu <= d) { cout << c << " " << nu << " "; nu++; } } else { long long nu = c + 1; while (nu < d) { cout << c << " " << nu << " "; nu++; } cout << c << " "; } } } cout << endl; } }
10
CPP
def solve(): n, l, r = [int(i) for i in input().split()] seq = [] i = 1 while l <= r: while l > 2 * n - 2: if l == 3: i = 1 break l -= 2 * n - 2 r -= 2 * n - 2 n -= 1 i += 1 if l%2 == 0: seq.append(l // 2 + i) else: seq.append(i) l += 1 return " ".join(str(i) for i in seq) T = int(input()) for _ in range(T): print(solve())
10
PYTHON3
t = int(input()) def query(i, n, x): if (x % 2 == 1): return i else: return (i + x // 2) for _ in range(t): n, l, r = map(int, input().split()) i = 1 s = 0 includeOne = False if r == n * (n - 1) + 1: includeOne = True r -= 1 if l == n * (n - 1) + 1: print(1) continue while s + 2 * (n - i) < l: s += 2 * (n - i) i += 1 newS = s allIs = [i] while newS + 2 * (n - i) < r: newS += 2 * (n - i) i += 1 allIs.append(i) allIin = 0 answer = [] i = allIs[0] for x in range(l, r + 1): r = query(i, n, x - s) if r == n: s += 2 * (n - i) i += 1 answer.append(r) if includeOne: answer.append(1) print(*answer)
10
PYTHON3
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") const int MAXN = 0; const int MAXK = 0; void solve() { long long n, l, r; cin >> n >> l >> r; long long sum = 0; for (int i = (1); i < (n); ++i) { sum += 2 * (n - i); if (sum >= l) { sum -= 2 * (n - i); long long var = i, cnt = 0; while (cnt < 2 * (n - i) && sum < r) { ++sum, ++cnt, var += !(cnt & 1); if (sum >= l) if (cnt & 1) cout << i << " "; else cout << var << " "; } if (sum != r) l = sum + 1; else break; } } if (r == n * (n - 1LL) + 1LL) cout << 1 << '\n'; else cout << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) solve(); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; void fun(long long start, long long end, long long l, long long r) { long long n = end - start + 1; for (long long i = max(1ll, l); i <= min(2 * (n - 1), r); i++) { if (i % 2 == 0) { cout << start + i / 2 << " "; } else { cout << start << " "; } } if (r > 2 * (n - 1) && start + 1 != end) { fun(start + 1, end, max(1ll, l - 2 * (n - 1)), r - 2 * (n - 1)); } } int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { long long n, l, r; cin >> n >> l >> r; bool flag = 0; if (r == n * (n - 1) + 1) { flag = 1; r--; } if (l <= r) { fun(1, n, l, r); } if (flag) { cout << 1 << " "; } cout << endl; } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; void _print(long long int t) { cerr << t; } void _print(int t) { cerr << t; } void _print(string t) { cerr << t; } void _print(char t) { cerr << t; } void _print(long double t) { cerr << t; } void _print(double t) { cerr << t; } void _print(long long unsigned t) { cerr << t; } template <class T, class V> void _print(pair<T, V> p); template <class T> void _print(vector<T> v); template <class T> void _print(set<T> v); template <class T, class V> void _print(map<T, V> v); template <class T> void _print(multiset<T> v); template <class T, class V> void _print(pair<T, V> p) { cerr << "{"; _print(p.first); cerr << ","; _print(p.second); cerr << "}"; } template <class T> void _print(vector<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T> void _print(set<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T> void _print(multiset<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T, class V> void _print(map<T, V> v) { cerr << "[ "; for (auto i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class a> void printarr(a arr[], int n) { for (int i = 0; i < n; i++) cout << arr[i] << ' '; cout << '\n'; } template <class c> void printarr(vector<c> v) { for (auto i : v) { cout << i << ' '; } cout << '\n'; } template <class c> void printarr(set<c> s) { for (auto i : s) { cout << i << ' '; } cout << '\n'; } template <class t> t extended_gcd(t a, t b, t &x, t &y) { t x1, y1; if (b == 0) { x = 1, y = 0; return a; } t g = extended_gcd(b, a % b, x1, y1); x = y1; y = x1 - (a / b) * y1; return g; } template <class a> a powmod(a t1, a t2, a mod) { a res = 1; t1 = t1 % mod; if (t1 == 0) return 0; while (t2) { if (t2 & 1) res = (res * t1) % mod; t1 = (t1 * t1) % mod; t2 = t2 >> 1; } return res; } template <class a> a powmod(a t1, a t2) { a res = 1; if (t1 == 0) return 0; while (t2) { if (t2 & 1) res = (res * t1); t1 = (t1 * t1); t2 = t2 >> 1; } return res; } const long long int MOD = 1e9 + 7; const int N = 1e2 + 5; void run() { long long int n, l, r; cin >> n >> l >> r; long long int p = n - 1, last = 0; while (p * 2 + last < l) { last += p * 2; p--; if (p == 0) { cout << 1 << '\n'; return; } } long long int t = n - p; ; long long int tt = t + ceil((l - last) / 2.0); tt = (tt == t) ? tt + 1 : tt; ; last = ((l - last) % 2) ? t : tt; while (l <= r) { cout << last << ' '; if (last == tt) { tt++; if (tt > n) { t++; if (t == n) t = 1; tt = t + 1; } last = t; } else { last = tt; } l++; } cout << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; cout << setprecision(15) << fixed; int test; cin >> test; while (test--) { run(); } cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << '\n'; return 0; ; }
10
CPP
for t in range(int(input())): n,l,r=map(int,input().split()) b=1 for i in range(1,n): a=b b+=2*(n-i) if l<b: break x,y=i,(l-a)//2+i+1 b=(l-a)%2 for _ in range(r-l): if b: print(y,end=" ") y+=1 if y==n+1: x+=1 y=x+1 else: print(x,end=" ") b^=1 if r==n*(n-1)+1: print(1) else: print(y if b else x) #JSR
10
PYTHON3
#include <bits/stdc++.h> using namespace std; void print_pattern(long long row, long long col, long long count, long long elements) { if (elements == 0) { cout << 1 << " "; return; } long long x = row; long long next_x = row + 1; long long j = 0; bool flag = 1; while (j < elements && count > 0) { if (j >= col) { cout << x << " "; count--; } if (flag) { x = next_x; next_x++; } else { x = row; } flag = !flag; j++; } if (count > 0) { print_pattern(row + 1, 0, count, elements - 2); } } void solve() { long long n, l, r, count; cin >> n >> l >> r; count = r - l + 1; bool zero_flag = 0; long long i, j, row; long long to_subtract = 2 * (n - 1); row = 1; while (l - to_subtract > 0) { l -= to_subtract; to_subtract -= 2; row++; if (to_subtract == 0) { zero_flag = 1; break; } } if (zero_flag) { cout << 1 << endl; return; } if (l == 0) { row++; } else { l--; } print_pattern(row, l, count, to_subtract); cout << endl; } int main() { int t; cin >> t; while (t--) { solve(); } return 0; }
10
CPP
#import math #from functools import lru_cache #import heapq #from collections import defaultdict #from collections import Counter #from sys import stdout #from sys import setrecursionlimit from sys import stdin input = stdin.readline for ti in range(int(input().strip())): n, l, r = [int(x) for x in input().strip().split()] cs, tc, cc = 1, 0, n - 1 ans = [] while(tc + 2*cc < l and cs<=n): tc += 2*cc cs += 1 cc -= 1 #print(tc) while(tc<=r and cs<=n): for cci in range(cs + 1, n+1): ne = cs tc += 1 if(l<=tc<=r): ans.append(ne) ne = cci tc += 1 if(l<=tc<=r): ans.append(ne) if(tc>r): break cs += 1 cc -= 1 if(tc<r): ans.append(1) print(*ans)
10
PYTHON3
#include <bits/stdc++.h> using namespace std; void debug_out() { cerr << endl; } void clock_out() { cerr << "\nTime Elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " s\n"; } void fileio() { freopen("/home/dwai/Desktop/cp/input.txt", "r", stdin); freopen("/home/dwai/Desktop/cp/output.txt", "w", stdout); freopen("/home/dwai/Desktop/cp/debug.txt", "w", stderr); } void ofileio() { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...); } int IT_MAX = 1 << 20; const long long MOD = 1000000007; const int INF = 0x3f3f3f3f; const long long LL_INF = 0x3f3f3f3f3f3f3f3f; const double PI = acos(-1); const double ERR = 1e-10; void solve() { long long n, l, r; cin >> n >> l >> r; long long lev = 1; long long cnt = 0; vector<long long> ans; while (cnt < r && lev < n) { long long x = (n - lev) * 2; if (cnt + x < l) { lev++; cnt += x; continue; } x = lev; while (cnt < r && x < n) { cnt++; if (cnt >= l && cnt <= r) { ans.emplace_back(lev); } cnt++; x++; if (cnt >= l && cnt <= r) { ans.emplace_back(x); } } lev++; } if (++cnt <= r) { ans.emplace_back(1); } for (long long x : ans) cout << x << " "; cout << "\n"; } int main() { 42; ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); srand(time(NULL)); long long t = 1; cin >> t; while (t--) { solve(); } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; long long z = 998244353; long long gcd(long long a, long long b) { if (a == 0) return b; if (b == 0) return a; return gcd(b, a % b); } long long power(long long a, long long b) { long long res = 1; while (b) { if (b & 1) { res = (res * a) % z; b--; } else { a = (a * a) % z; b = b >> 1; } } return res; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t; t = 1; cin >> t; while (t--) { long long n, l, r; cin >> n >> l >> r; long long s = 1, i = 1; long long len = r - l + 1; while (i < n) { if (l - 2 * (n - i) >= 0) { l -= 2 * (n - i); i++; s++; } else { break; } } vector<long long> v; len += l; long long p = 0; v.push_back(n); for (long long i = s; i <= n; i++) { for (long long j = i + 1; j <= n; j++) { v.push_back(i); p++; if (p == len) break; v.push_back(j); p++; if (p == len) break; } if (p == len) break; } v.push_back(1); for (long long i = l; i < len; i++) cout << v[i] << " "; cout << "\n"; } }
10
CPP
#include <bits/stdc++.h> using namespace std; const long long INF = (long long)2e9; const long long inf = (long long)2e18; const long double eps = (long double)1e-8; const long long mod = (long long)1e9 + 7; const long long MAXN = (long long)1e5 + 1; const long long MAXC = (long long)1e6 + 1; const long long MAXE = (long long)1000; const long long MAXLOG = 21; const long long maxlen = (long long)1e5; const long long asci = (long long)256; const long long block = 480; const long double PI = acos(-1); const long double e = 2.7182818284; template <class T> istream &operator>>(istream &in, vector<T> &arr) { for (T &cnt : arr) { in >> cnt; } return in; }; void solve() { long long n, l, r; cin >> n >> l >> r; l--, r--; vector<long long> a; long long i = 0; for (; i < n - 1; ++i) { if (l >= 2 * (n - i - 1)) l -= 2 * (n - i - 1), r -= 2 * (n - i - 1); else break; } while (a.size() <= r && i < n) { for (long long j = i + 1; j < n; ++j) { a.push_back(i); a.push_back(j); } ++i; } if (i == n) a.push_back(0); for (; l <= r; ++l) { cout << a[l] + 1 << " "; } cout << "\n"; } int main() { srand(time(0)); ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout.precision(30); long long t; cin >> t; while (t--) solve(); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; ifstream fin("input.txt"); ofstream fout("output.txt"); long long fast_exp(long long base, long long exp) { long long res = 1; while (exp > 0) { if (exp % 2 == 1) res = (res * base) % 1000000007; base = (base * base) % 1000000007; exp /= 2; } return res % 1000000007; } int palindromecheck(string s) { int n = s.size(); for (int i = 0; i < n / 2; ++i) { if (s[i] != s[n - i - 1]) { return 0; } } return 1; } int gcd(int a, int b) { while (a && b) a > b ? a %= b : b %= a; return a + b; } int val(char c) { if (c >= '0' && c <= '9') return (int)c - '0'; else return (int)c - 'A' + 10; } long long pows(int a, int b) { long long res = 1; for (int i = 0; i < b; ++i) { res *= a; } return res; } long long logx(long long base, long long num) { int cnt = 0; while (num != 1) { num /= base; ++cnt; } return cnt; } long long divisibles(long long a, long long b, long long m) { if (a % m == 0) return (b / m) - (a / m) + 1; else return (b / m) - (a / m); } string bitstring(int n, int size) { string s; while (n) { s += (n % 2) + '0'; n /= 2; } while (s.size() < size) { s += '0'; } reverse(s.begin(), s.end()); return s; } vector<int> root(200001, 0); vector<int> size(200001, 1); int find(int x) { while (x != root[x]) x = root[x]; return x; } bool same(int a, int b) { return find(a) == find(b); } void unite(int a, int b) { a = find(a); b = find(b); if (size[a] < size[b]) swap(a, b); size[a] += size[b]; root[b] = a; } vector<int> vis(200001, 0); vector<int> adj[200001]; int main() { std::ios::sync_with_stdio(false); int t; cin >> t; while (t--) { int n; long long l, r; cin >> n >> l >> r; vector<long long> vec(n + 1); long long sum = 0; int j = 1; for (int i = n; i >= 1; --i) { sum += (i - 1) * 2; vec[j] = sum; if (i == 1) ++vec[j]; ++j; } long long idxl = lower_bound(vec.begin(), vec.end(), l) - vec.begin(); long long idxr = lower_bound(vec.begin(), vec.end(), r) - vec.begin(); if (vec[idxl] > l && idxl > 1) --idxl; vector<pair<long long, long long>> ans; int temp = vec[idxl]; if (idxl == 1) temp = 1; long long start; if (idxl != 1) start = vec[idxl - 1] + 1; for (int i = idxl; i <= idxr; ++i) { for (int j = i + 1; j <= n; ++j) { ans.push_back(make_pair(i, start++)); ans.push_back(make_pair(j, start++)); } if (i + 1 > n) { ans.push_back(make_pair(1, start)); } } long long begin; if (idxl == 1) { begin = l - 1; } else { for (int i = 0; i < ans.size(); ++i) { if (ans[i].second == l) { begin = i; break; } } } for (int i = begin; i <= begin + (r - l); ++i) cout << ans[i].first << " "; cout << "\n"; } }
10
CPP
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long n; long long l, r; long long i; cin >> n >> l >> r; long long temp = l; long long b = 1; while (temp > 2 * (n - b) && b < n) { temp = temp - 2 * (n - b); b++; } for (i = l; i <= r && i < n * (n - 1) + 1; i++) { if (temp % 2 == 1) { printf("%I64d ", b); temp++; } else { printf("%I64d ", temp / 2 + b); if (temp / 2 + b == n) { temp = 1; b++; } else temp++; } } if (r == n * (n - 1) + 1) printf("1\n"); else printf("\n"); } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long t, i, n, cnt, od, l, r, ev, g; cin >> t; while (t--) { cin >> n >> l >> r; g = 0; if (r == (n * (n - 1)) + 1) { g = 500; } i = 0; while (i <= n - 1) { if (2 * n * (i) - (i) * (i + 1) < l) { i++; } else { i--; break; } } cnt = 2 * n * i - (i) * (i + 1) + 1; od = i + 1; ev = i + 2; if (g) { r--; } while (cnt <= r) { if (cnt >= l) { if (cnt % 2 == 0) { cout << ev << " "; ev++; } else { cout << od << " "; } cnt++; } else { if (cnt % 2 == 0) { ev++; } else { ; } cnt++; } if (ev > n) { od++; ev = od + 1; } } if (g == 500) { cout << 1 << " "; } cout << "\n"; } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int mods = 998244353; const int maxn = 1e5 + 10; const int N = 1e5 + 10; const int E = 2e5 + 10; long long n, l, r; long long k[maxn]; vector<int> ans; int main() { int T; cin >> T; while (T--) { cin >> n >> l >> r; k[1] = 1; for (int i = 2; i <= (n); ++i) { k[i] = k[i - 1] + 2 * (n - i + 1); } long long bo = n; for (int i = 1; i <= (n); ++i) { if (k[i] > l) { bo = i - 1; break; } } if (bo >= n) { printf("1\n"); continue; } long long pc = k[bo]; long long tot = 1; long long tmp; ans.clear(); while (pc <= r) { if (bo == n) { ans.push_back(1); break; } if (pc % 2 == 1) tmp = bo; else { tmp = bo + tot; tot++; } if (pc >= l) ans.push_back(tmp); pc++; if (tmp == n) { bo++; tot = 1; } } for (int i = 0; i <= (ans.size() - 1); ++i) { cout << ans[i] << " "; } cout << endl; } }
10
CPP
#include <bits/stdc++.h> using namespace std; using ll = long long; using pll = pair<ll, ll>; const int LM = 3e5 + 4; ll N; ll L, R; ll part[LM]; int main() { int T; scanf("%d", &T); while (T--) { scanf("%lld%lld%lld", &N, &L, &R); bool e = 0; if (R == N * (N - 1) + 1) { e = 1; R--; } for (int i = 1; i <= N - 1; i++) part[i] = part[i - 1] + (N - i) * 2; for (int i = 1; i <= N; i++) { if (L <= part[i] && R > part[i - 1]) { int v, last = i; for (int j = 1; j <= (N - i) * 2; j++) { if (j & 1) v = i; else v = ++last; if (L <= part[i - 1] + j && part[i - 1] + j <= R) printf("%d ", v); } } } if (e) printf("1 "); puts(""); } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7, MAX = 1e5 + 5; long long powN(long long a, long long p) { if (p == 0) return 1; long long z = powN(a, p / 2); z = (z * z) % MOD; if (p % 2) z = (z * a) % MOD; return z; } vector<bool> is_prime(MAX + 1, true); void Sieve() { is_prime[0] = is_prime[1] = false; int i, j; for (i = 2; i * i <= MAX; i++) { if (is_prime[i]) { for (j = i * i; j <= MAX; j += i) { is_prime[j] = false; } } } } int main() { int t; cin >> t; while (t--) { long long n; long long l, r; cin >> n >> l >> r; long long st = n - 1; for (long long k = 1; k <= n; k++) { if ((n * 2 - k - 1) * k >= l) { st = k - 1; break; } } long long ex = (2 * n - st - 1) * (st); l -= ex; r -= ex; st++; vector<int> V = {0}; long long some = st + 1; long long tot = 0; for (int i = 1; i <= r; i++) { if (st == n) { V.push_back(1); break; } if (i % 2) V.push_back(st); else { V.push_back(some); some++; } if (i == tot + 2 * (n - st)) { tot += 2 * (n - st); st++; some = st + 1; } } for (int i = l; i <= r; i++) { printf("%d ", V[i]); } cout << endl; } }
10
CPP
#include <bits/stdc++.h> using namespace std; struct Edge { long long to, dis, next, cost; } edge[24050]; long long num = -1; bool vis[10010]; long long mincost; long long pre[10010], head[10010], cost[10010], last[10010], flow[10010], n, k, a[110], b[110], s, t, maxflow; long long to[110]; void add(long long f, long long t, long long dis, long long cost) { edge[++num].to = t; edge[num].dis = dis; edge[num].next = head[f]; edge[num].cost = cost; head[f] = num; edge[++num].to = f; edge[num].dis = 0; edge[num].cost = -cost; edge[num].next = head[t]; head[t] = num; } queue<long long> q; bool spfa(long long s, long long t) { memset(cost, 0x3f3f3f3f, sizeof cost); memset(flow, 0x3f3f3f3f, sizeof flow); memset(vis, 0, sizeof vis); q.push(s); vis[s] = 1; cost[s] = 0; pre[t] = -1; while (!q.empty()) { long long nowp = q.front(); q.pop(); vis[nowp] = 0; for (long long i = head[nowp]; i != -1; i = edge[i].next) { if (edge[i].dis > 0 && cost[edge[i].to] > cost[nowp] + edge[i].cost) { cost[edge[i].to] = cost[nowp] + edge[i].cost; pre[edge[i].to] = nowp; last[edge[i].to] = i; flow[edge[i].to] = min(flow[nowp], edge[i].dis); if (!vis[edge[i].to]) { vis[edge[i].to] = 1; q.push(edge[i].to); } } } } return pre[t] != -1; } void MCMF(long long s, long long t) { while (spfa(s, t)) { long long now = t; maxflow += flow[t]; mincost += flow[t] * cost[t]; while (now != s) { edge[last[now]].dis -= flow[t]; edge[last[now] ^ 1].dis += flow[t]; now = pre[now]; } } } signed main() { long long T; cin >> T; while (T--) { num = -1; memset(to, -1, sizeof to); memset(head, -1, sizeof head); cin >> n >> k; for (long long i = 1; i <= n; i++) cin >> a[i] >> b[i]; maxflow = mincost = 0; s = 0; t = 2 * n + 1; for (long long i = 1; i <= n; i++) add(s, i, 1, 0), add(i + n, t, 1, 0); for (long long i = 1; i <= n; i++) { for (long long j = 1; j <= n; j++) { long long nc = 0; if (j <= k - 1) nc = a[i] + b[i] * (j - 1ll); else if (j != n) nc = b[i] * (k - 1ll); else nc = a[i] + b[i] * (k - 1ll); nc = 0x3f3f3f3f - nc; add(i, j + n, 1, nc); } } MCMF(s, t); long long nowi = -1; for (long long i = n * 4; i <= num; i += 2) { nowi++; if (edge[i].dis == 0) to[1 + nowi % n] = 1 + nowi / n; } cout << 2 * n - k << endl; for (long long i = 1; i <= k - 1; i++) cout << to[i] << " "; for (long long i = k; i < n; i++) cout << to[i] << " " << -to[i] << " "; cout << to[n]; cout << endl; } return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; void read(int &val) { int x = 0; int bz = 1; char c; for (c = getchar(); (c < '0' || c > '9') && c != '-'; c = getchar()) ; if (c == '-') { bz = -1; c = getchar(); } for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - 48; val = x * bz; } const int maxn = 1e5 + 101; const int mod = 1e9 + 7; struct Edge { int from, to, cap, flow; long long cost; }; int a[maxn], b[maxn]; bool cmp(int x, int y) { return b[x] < b[y]; } int n, k; int N; struct MCMF { int n, m; vector<Edge> edge; vector<int> G[maxn]; int inq[maxn], p[maxn], a[maxn], To[maxn]; long long d[maxn]; int tag[maxn]; void init(int _n) { n = _n; for (int i = 0; i <= n; i++) G[i].clear(); edge.clear(); } void add(int from, int to, int cap, long long cost, int flag) { edge.push_back(Edge{from, to, cap, 0, cost}); edge.push_back(Edge{to, from, 0, 0, -cost}); m = edge.size(); G[from].push_back(m - 2); G[to].push_back(m - 1); tag[m - 2] = tag[m - 1] = flag; To[m - 1] = To[m - 2] = to - N; } bool BellFord(int s, int t, int &flow, long long &cost) { for (int i = 0; i <= n; i++) d[i] = INF, inq[i] = 0; d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = INF; queue<int> q; q.push(s); while (!q.empty()) { int u = q.front(); q.pop(); inq[u] = 0; for (auto to : G[u]) { Edge &e = edge[to]; if (e.cap > e.flow && d[e.to] > d[u] + e.cost) { d[e.to] = d[u] + e.cost; p[e.to] = to; a[e.to] = min(a[u], e.cap - e.flow); if (!inq[e.to]) { q.push(e.to); inq[e.to] = 1; } } } } if (d[t] == INF) return false; flow += a[t]; cost += 1LL * d[t] * a[t]; for (int u = t; u != s; u = edge[p[u]].from) { edge[p[u]].flow += a[t]; edge[p[u] ^ 1].flow -= a[t]; } return true; } void MincostMaxflow(int s, int t, long long &cost) { int flow = 0; cost = 0; while (BellFord(s, t, flow, cost)) ; vector<int> ans1, ans2; n = (n - 4) / 3; for (int i = 1; i <= n; i++) { for (auto to : G[i]) { if (edge[to].flow == false) continue; if (tag[to] == 1) { ans1.push_back(To[to]); } else if (tag[to] == 2) { ans2.push_back(To[to]); } } } sort(ans1.begin(), ans1.end(), cmp); int sz = ans1.size(); for (int i = 0; i <= sz - 2; i++) { printf("%d ", ans1[i]); } for (auto to : ans2) { printf("%d %d ", to, -to); } printf("%d\n", ans1[sz - 1]); } } M; int main() { int t; read(t); while (t--) { read(n); read(k); N = n; M.init(3 * n + 4); int mx = 0, j = 0; for (int i = 1; i <= n; i++) { read(a[i]); read(b[i]); if (mx < a[i]) j = i; mx = max(mx, a[i]); } if (k == 1) { printf("%d\n%d\n", 1, j); continue; } printf("%d\n", k + 2 * (n - k)); int s = 0, t = 3 * n + 3; for (int i = 1; i <= n; i++) { M.add(s, i, 1, 0, -1); } for (int i = 1; i <= k - 1; i++) { for (int j = 1; j <= n; j++) { M.add(i, j + n, 1, -(1LL * (i - 1) * b[j] + a[j]), 1); } } for (int i = k; i <= n - 1; i++) { for (int j = 1; j <= n; j++) { M.add(i, j + n, 1, -(1LL * (k - 1) * b[j]), 2); } } for (int j = 1; j <= n; j++) { M.add(n, j + n, 1, -(1LL * (k - 1) * b[j] + a[j]), 1); } for (int i = n + 1; i <= n + n; i++) { M.add(i, t, 1, 0, -1); } long long cost = 0; M.MincostMaxflow(s, t, cost); } return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 2e9; int solve() { int n, k; cin >> n >> k; vector<pair<pair<int, int>, int>> arr(n); for (int i = 0; i < n; i++) cin >> arr[i].first.second >> arr[i].first.first, arr[i].second = i + 1; sort(arr.begin(), arr.end()); vector<int> a(n), b(n); for (int i = 0; i < n; i++) a[i] = arr[i].first.second, b[i] = arr[i].first.first; vector<vector<int>> dp(n + 1, vector<int>(k + 1, -INF)); vector<vector<bool>> op(n + 1, vector<bool>(k + 1, 0)); dp[0][0] = 0; for (int i = 0; i < n; i++) { dp[i + 1][0] = dp[i][0] + (k - 1) * b[i]; for (int j = 1; j < k + 1; j++) { int v1 = dp[i][j] + (k - 1) * b[i], v2 = dp[i][j - 1] + (j - 1) * b[i] + a[i]; if (v2 > v1) op[i + 1][j] = 1; dp[i + 1][j] = max(v1, v2); } } int curj = k; vector<int> maj, bon; for (int i = n; i; i--) { int oo = op[i][curj]; if (oo) { maj.push_back(i - 1); curj--; } else { bon.push_back(i - 1); } } cout << n + n - k << endl; for (int i = k - 1; i >= 1; i--) cout << arr[maj[i]].second << " "; for (auto i : bon) cout << arr[i].second << " " << -arr[i].second << " "; cout << arr[maj[0]].second << endl; return 0; } int32_t main() { ios_base::sync_with_stdio(false); int t; cin >> t; for (int i = 0; i < t; i++) solve(); return 0; }
12
CPP
#include <bits/stdc++.h> template <typename T> bool ckmax(T& a, T b) { return a < b ? a = b, 1 : 0; } template <typename T> bool ckmin(T& a, T b) { return b < a ? a = b, 1 : 0; } const int MN = 80; int N, K, T, dp[MN][MN], ans; bool u[MN], pr[MN][MN]; struct minion { public: int a, b, id; void in() { scanf("%d%d", &a, &b); } bool operator<(minion o) const { return b < o.b; } } a[MN]; std::vector<int> seq; int main(void) { scanf("%d", &T); for (; T--;) { scanf("%d%d", &N, &K); for (int i = 0; i < N; ++i) a[i].in(), a[i].id = i + 1; std::sort(a, a + N); memset(dp, -1, sizeof dp); dp[0][0] = 0; for (int i = 0; i < N; ++i) for (int j = 0; j <= K; ++j) if (~dp[i][j]) { if (ckmax(dp[i + 1][j], dp[i][j] + (K - 1) * a[i].b)) pr[i + 1][j] = 0; if (j + 1 <= K && ckmax(dp[i + 1][j + 1], dp[i][j] + a[i].a + j * a[i].b)) pr[i + 1][j + 1] = 1; } seq.clear(); ans = dp[N][K]; memset(u, 0, N * sizeof u[0]); int k = K; for (int i = N; i > 0; --i) { if (pr[i][k]) u[i - 1] = 1, seq.push_back(a[i - 1].id), --k; } assert(!k); std::reverse(seq.begin(), seq.end()); int v = seq.back(); seq.pop_back(); for (int i = 0; i < N; ++i) if (!u[i]) seq.push_back(a[i].id), seq.push_back(-a[i].id); seq.push_back(v); printf("%d\n", seq.size()); for (int i = 0; i < seq.size(); ++i) printf("%d%c", seq[i], " \n"[i + 1 == seq.size()]); } return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); mt19937 rnf(2106); const int N = 80; const int INF = 1000000009; struct ban { int i; int a, b; }; bool operator<(const ban& t1, const ban& t2) { return t1.b < t2.b; } int n, k; ban t[N]; int dp[N][N]; int p[N][N]; void solv() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) { t[i].i = i; scanf("%d%d", &t[i].a, &t[i].b); } for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) dp[i][j] = -INF; sort(t + 1, t + n + 1); dp[0][0] = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j <= k; ++j) { if (j < k) { if (dp[i][j] + t[i + 1].a + t[i + 1].b * j > dp[i + 1][j + 1]) { dp[i + 1][j + 1] = dp[i][j] + t[i + 1].a + t[i + 1].b * j; p[i + 1][j + 1] = 1; } } if (dp[i][j] + t[i + 1].b * (k - 1) > dp[i + 1][j]) { dp[i + 1][j] = dp[i][j] + t[i + 1].b * (k - 1); p[i + 1][j] = 0; } } } vector<int> ans1, ans0; int j = k; for (int i = n; i >= 1; --i) { if (p[i][j] == 1) { ans1.push_back(t[i].i); --j; } else { ans0.push_back(t[i].i); } } reverse((ans1).begin(), (ans1).end()); printf("%d\n", ((int)(ans1).size()) + ((int)(ans0).size()) * 2); for (int i = 0; i < ((int)(ans1).size()) - 1; ++i) printf("%d ", ans1[i]); for (int i = 0; i < ((int)(ans0).size()); ++i) printf("%d %d ", ans0[i], -ans0[i]); printf("%d\n", ans1.back()); } int main() { int tt; scanf("%d", &tt); while (tt--) solv(); return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; int dp[76][76]; bool taken[76][76]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t, n, m; cin >> t; while (t--) { cin >> n >> m; vector<pair<pair<int, int>, int>> data(n); for (int i = 0; i < n; ++i) { cin >> data[i].first.second >> data[i].first.first; data[i].second = i + 1; } sort(data.begin(), data.end()); for (int i = 1; i <= n; ++i) { int s = data[i - 1].first.first * (m - 1); int f = data[i - 1].first.second; f -= data[i - 1].first.first; dp[i][min(i, m)] = 0; taken[i][min(i, m)] = false; for (int p = 0; p <= m && p < i; ++p) { dp[i][p] = dp[i - 1][p] + s; taken[i][p] = false; } for (int p = 0; p < m && p < i; ++p) { f += data[i - 1].first.first; if (dp[i - 1][p] + f > dp[i][p + 1]) { dp[i][p + 1] = dp[i - 1][p] + f; taken[i][p + 1] = true; } } } int r = m; int pos = n; vector<int> order, others; while (pos > 0) { if (taken[pos][r]) { order.push_back(data[pos - 1].second); r--; } else others.push_back(data[pos - 1].second); pos--; } reverse(order.begin(), order.end()); int last = order.back(); order.pop_back(); cout << m + (n - m) * 2 << "\n"; for (auto &x : order) cout << x << " "; for (auto &x : others) cout << x << " " << -x << " "; cout << last << "\n"; } return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; template <typename T> void r1(T &x) { x = 0; char c(getchar()); int f(1); for (; c < '0' || c > '9'; c = getchar()) if (c == '-') f = -1; for (; '0' <= c && c <= '9'; c = getchar()) x = (x * 10) + (c ^ 48); x *= f; } template <typename T, typename... Args> inline void r1(T &t, Args &...args) { r1(t); r1(args...); } const int maxn = 1e2 + 5; const int maxm = maxn << 1; const int mod = 1e9 + 7; typedef int room[maxn]; template <int mod> struct typemod { int z; typemod(int a = 0) : z(a) {} inline int inc(int a, int b) const { return a += b - mod, a + ((a >> 31) & mod); } inline int dec(int a, int b) const { return a -= b, a + ((a >> 31) & mod); } inline int mul(int a, int b) const { return 1ll * a * b % mod; } typemod<mod> operator+(const typemod<mod> &x) const { return typemod(inc(z, x.z)); } typemod<mod> operator-(const typemod<mod> &x) const { return typemod(dec(z, x.z)); } typemod<mod> operator*(const typemod<mod> &x) const { return typemod(mul(z, x.z)); } typemod<mod> &operator+=(const typemod<mod> &x) { *this = *this + x; return *this; } typemod<mod> &operator-=(const typemod<mod> &x) { *this = *this - x; return *this; } typemod<mod> &operator*=(const typemod<mod> &x) { *this = *this * x; return *this; } int operator==(const typemod<mod> &x) const { return x.z == z; } int operator!=(const typemod<mod> &x) const { return x.z != z; } }; int n, k; int f[maxn][maxn]; struct Node { int a, b, id; int operator<(const Node &z) const { return b == z.b ? a > z.a : b < z.b; } } a[maxn], tmp[maxn]; int pre[maxn][maxn], visin[maxn]; int qwq[maxn], qaq[maxn]; int ans1[maxn], ans2[maxn]; signed main() { int i, j, t; r1(t); while (t--) { int ans(0), last(0), C(0); r1(n), r1(k); for (i = 1; i <= n; ++i) r1(a[i].a), r1(a[i].b), a[i].id = i; sort(a + 1, a + n + 1); for (i = 1; i <= n; ++i) ans1[i] = ans2[i] = 0; for (int st = 1; st <= n; ++st) { memset(f, -0x3f, sizeof(f)); for (i = 1; i <= n; ++i) visin[i] = 0; f[0][0] = 0; for (i = 1; i <= n; ++i) { for (j = 0; j <= k - 1; ++j) { if (!j || i == st) { pre[i][j] = 0, f[i][j] = f[i - 1][j]; continue; } int tmp = a[i].b * (j - k); if (f[i - 1][j] > f[i - 1][j - 1] + tmp + a[i].a) f[i][j] = f[i - 1][j], pre[i][j] = 0; else f[i][j] = f[i - 1][j - 1] + tmp + a[i].a, pre[i][j] = 1; } } i = n, j = k - 1; while (i && j) { if (!pre[i][j]) --i; else qaq[j] = i, visin[i] = 1, --i, --j; } int cnt(0); for (i = 1; i <= n; ++i) if (!visin[i] && i != st) qwq[++cnt] = i; int ct1(0); for (i = 1; i <= cnt; ++i) tmp[++ct1] = a[qwq[i]]; sort(tmp + 1, tmp + ct1 + 1); int res(0); for (i = 1; i <= ct1; ++i) res += (k - 1) * tmp[i].b; for (i = 1; i <= k - 1; ++i) res += a[qaq[i]].a + (i - 1) * a[qaq[i]].b; res += a[st].a + (k - 1) * a[st].b; if (res >= ans) { ans = res, last = st; C = cnt; for (i = 1; i <= k - 1; ++i) ans1[i] = qaq[i]; for (i = 1; i <= cnt; ++i) ans2[i] = qwq[i]; } } printf("%d\n", C * 2 + k); for (i = 1; i <= k - 1; ++i) printf("%d ", a[ans1[i]].id); for (i = 1; i <= C; ++i) printf("%d %d ", a[ans2[i]].id, -a[ans2[i]].id); printf("%d\n", a[last].id); } return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; class Debugger { public: template <typename T> Debugger &operator<<(const T &v) { cerr << v; return *this; } ~Debugger() { cerr << endl; } }; template <typename T1, typename T2> inline ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << "(" << p.first << ", " << p.second << ")"; } template <typename T, size_t N> inline ostream &operator<<(ostream &os, const array<T, N> &a) { bool first = true; os << "["; for (auto &v : a) { if (!first) os << ", "; os << v; first = false; } os << "]"; return os; } template <typename T> inline ostream &operator<<(ostream &os, const vector<T> &v) { bool first = true; os << "["; for (unsigned int i = 0; i < v.size(); i++) { if (!first) os << ", "; os << v[i]; first = false; } return os << "]"; } template <typename T> inline ostream &operator<<(ostream &os, const set<T> &v) { bool first = true; os << "{"; for (typename set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if (!first) os << ", "; os << *ii; first = false; } return os << "}"; } template <typename T1, typename T2> inline ostream &operator<<(ostream &os, const map<T1, T2> &v) { bool first = true; os << "["; for (typename map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if (!first) os << ", "; os << *ii; first = false; } return os << "]"; } const long long MOD = 1000000000 + 7; const int dx[] = {-1, 1, 0, 0}; const int dy[] = {0, 0, 1, -1}; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long power_mod(long long x, long long y, long long p) { long long ans = 1; x %= p; while (y > 0) { if (y & 1) ans = (ans * x) % p; y >>= 1; x = (x * x) % p; } return ans; } inline long long modInverse(long long n, long long p) { return power_mod(n, p - 2, p); } long long nCrModP(vector<long long> &fac, long long n, long long r, long long p) { if (r == 0) return 1; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } template <typename T> void print_vector(const vector<T> &v) { for (int i = (0); i < (((int)(v).size())); i++) { if (i > 0) cout << " "; cout << v[i]; } cout << endl; } int n, m; long long dp[76][76]; bool state[76][76]; pair<pair<int, int>, int> minions[76]; bool update(long long &a, long long b) { if (a == -1 || b > a) { a = b; return true; } return false; } long long calculate(int lastOne) { memset(dp, -1, sizeof(dp)); memset(state, 0, sizeof(state)); dp[0][0] = 0; long long a, b; for (int i = (1); i < (n + 1); i++) { if (i == lastOne) { for (int j = (0); j < (m); j++) dp[i][j] = dp[i - 1][j]; continue; } a = minions[i].first.second; b = minions[i].first.first; for (int j = (0); j < (m); j++) { if (dp[i - 1][j] != -1) { if (update(dp[i][j], dp[i - 1][j] + b * (m - 1))) state[i][j] = false; } if (j > 0 && dp[i - 1][j - 1] != -1) { if (update(dp[i][j], dp[i - 1][j - 1] + a + b * (j - 1))) state[i][j] = true; } } } a = minions[lastOne].first.second; b = minions[lastOne].first.first; return dp[n][m - 1] + a + b * (m - 1); } void solve() { cin >> n >> m; for (int i = (1); i < (n + 1); i++) { cin >> minions[i].first.second >> minions[i].first.first; minions[i].second = i; } sort(minions + 1, minions + n + 1); long long mx = 0; int lastOne = -1; for (int i = (1); i < (n + 1); i++) { long long t = calculate(i); if (t > mx) { mx = t; lastOne = i; } } assert(lastOne != -1); calculate(lastOne); vector<int> vis(n + 1, false); int x = n, y = m - 1; while (x > 0) { if (state[x][y]) { vis[x] = true; y--; } x--; } vector<int> ans; for (int i = (1); i < (n + 1); i++) { if (vis[i] && i != lastOne) ans.push_back(minions[i].second); } for (int i = (1); i < (n + 1); i++) { if (!vis[i] && i != lastOne) { ans.push_back(minions[i].second); ans.push_back(-minions[i].second); } } ans.push_back(minions[lastOne].second); cout << ((int)(ans).size()) << endl; print_vector(ans); } int main() { ios_base::sync_with_stdio(false); srand(time(NULL)); cin.tie(NULL); cout.tie(NULL); int cas; cin >> cas; for (int i = (0); i < (cas); i++) { solve(); } }
12
CPP
#include <bits/stdc++.h> #pragma GCC target("avx2,avx") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; using ll = long long; using ull = unsigned long long; using i128 = __int128_t; using pii = pair<int, int>; using pll = pair<long long, long long>; constexpr char ln = '\n'; istream& operator>>(istream& is, __int128_t& x) { x = 0; string s; is >> s; int n = int(s.size()), it = 0; if (s[0] == '-') it++; for (; it < n; it++) x = (x * 10 + s[it] - '0'); if (s[0] == '-') x = -x; return is; } ostream& operator<<(ostream& os, __int128_t x) { if (x == 0) return os << 0; if (x < 0) os << '-', x = -x; deque<int> deq; while (x) deq.emplace_front(x % 10), x /= 10; for (int e : deq) os << e; return os; } template <class T1, class T2> ostream& operator<<(ostream& os, const pair<T1, T2>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template <class T> ostream& operator<<(ostream& os, const vector<T>& v) { os << "{"; for (int i = 0; i < int(v.size()); i++) { if (i) os << ", "; os << v[i]; } return os << "}"; } template <class Container> inline int SZ(Container& v) { return int(v.size()); } template <class T> inline void UNIQUE(vector<T>& v) { v.erase(unique(v.begin(), v.end()), v.end()); } template <class T1, class T2> inline bool chmax(T1& a, T2 b) { if (a < b) { a = b; return true; } return false; } template <class T1, class T2> inline bool chmin(T1& a, T2 b) { if (a > b) { a = b; return true; } return false; } inline int topbit(ull x) { return x == 0 ? -1 : 63 - __builtin_clzll(x); } inline int botbit(ull x) { return x == 0 ? 64 : __builtin_ctzll(x); } inline int popcount(ull x) { return __builtin_popcountll(x); } inline int kthbit(ull x, int k) { return (x >> k) & 1; } inline constexpr long long TEN(int x) { return x == 0 ? 1 : TEN(x - 1) * 10; } inline void print() { cout << "\n"; } template <class T> inline void print(const vector<T>& v) { for (int i = 0; i < int(v.size()); i++) { if (i) cout << " "; cout << v[i]; } print(); } template <class T, class... Args> inline void print(const T& x, const Args&... args) { cout << x << " "; print(args...); } struct fast_ios { fast_ios() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; constexpr ll INF = 1e18; void solve() { int N, K; cin >> N >> K; vector<ll> A(N), B(N); for (int i = 0; i < (N); i++) cin >> A[i] >> B[i]; vector<int> idx(N); iota((idx).begin(), (idx).end(), 0); sort((idx).begin(), (idx).end(), [&](int i, int j) { return B[i] < B[j]; }); vector<vector<ll>> dp(N + 1, vector<ll>(K + 1, -INF)); vector<vector<int>> pre(N + 1, vector<int>(K + 1)); dp[0][0] = 0; for (int i = 0; i < (N); i++) for (int j = 0; j < (K + 1); j++) { int u = idx[i]; if (chmax(dp[i + 1][j], dp[i][j] + B[u] * (K - 1))) { pre[i + 1][j] = j; } if (j < K) { if (chmax(dp[i + 1][j + 1], dp[i][j] + A[u] + B[u] * j)) { pre[i + 1][j + 1] = j; } } } vector<int> C, D; { int k = K; for (int n = N; n > 0; n--) { int pk = pre[n][k]; if (pk == k) { D.emplace_back(idx[n - 1]); } else { C.emplace_back(idx[n - 1]); } k = pk; } } reverse((C).begin(), (C).end()); cout << SZ(C) + SZ(D) * 2 << ln; for (int i = 0; i < (SZ(C) - 1); i++) cout << C[i] + 1 << " "; for (auto e : D) cout << e + 1 << " " << -(e + 1) << " "; cout << C.back() + 1 << ln; } int main() { int Q; cin >> Q; while (Q--) { solve(); } }
12
CPP
#include <bits/stdc++.h> using namespace std; using li = long long; using ld = long double; using pii = pair<int, int>; using vi = vector<int>; using matr = vector<vi>; const int INF = 1e9 + 13; vector<int> hungary(vector<vector<int>> a) { int n = a.size() - 1; int m = n; vector<int> u(n + 1), v(m + 1), p(m + 1), way(m + 1); for (int i = 1; i <= n; ++i) { p[0] = i; int j0 = 0; vector<int> minv(m + 1, INF); vector<char> used(m + 1, false); do { used[j0] = true; int i0 = p[j0], delta = INF, j1; for (int j = 1; j <= m; ++j) if (!used[j]) { int cur = a[i0][j] - u[i0] - v[j]; if (cur < minv[j]) minv[j] = cur, way[j] = j0; if (minv[j] < delta) delta = minv[j], j1 = j; } for (int j = 0; j <= m; ++j) if (used[j]) u[p[j]] += delta, v[j] -= delta; else minv[j] -= delta; j0 = j1; } while (p[j0] != 0); do { int j1 = way[j0]; p[j0] = p[j1]; j0 = j1; } while (j0); } vector<int> ans(n + 1); for (int j = 1; j <= m; ++j) ans[j] = p[j]; return ans; } void solve() { int n, k; cin >> n >> k; vector<int> x(n), y(n); for (int i = 0; i < n; i++) { cin >> x[i] >> y[i]; } vector<vector<int>> a(n + 1, vector<int>(n + 1, 0)); for (int i = 0; i < n; i++) { for (int j = 0; j < k - 1; j++) { a[i + 1][j + 1] = x[i] + y[i] * j; } for (int j = k - 1; j < n - 1; j++) { a[i + 1][j + 1] = y[i] * (k - 1); } a[i + 1][n] = x[i] + y[i] * (k - 1); } for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) a[i][j] = -a[i][j]; } vector<int> res = hungary(a); vector<int> ans; for (int i = 0; i < k - 1; i++) { ans.push_back(res[i + 1]); } for (int i = k - 1; i < n - 1; i++) { ans.push_back(res[i + 1]); ans.push_back(-res[i + 1]); } ans.push_back(res[n]); cout << ans.size() << endl; for (auto x : ans) cout << x << ' '; cout << endl; } int main() { int t = 1; cin >> t; while (t--) solve(); }
12
CPP
#include <bits/stdc++.h> using namespace std; void read(long long &x) { char ch = getchar(); x = 0; while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar(); } const long long N = 77; long long n, k, f[N][N], g[N][N]; pair<long long, pair<long long, long long> > a[N]; vector<long long> res, ans; signed main() { long long T; read(T); while (T--) { read(n), read(k); k = min(k, n); for (long long i = 1; i <= n; ++i) read(a[i].second.first), read(a[i].first), a[i].second.second = i; if (k == 1) { puts("1"); long long w = 0; for (long long i = 1; i <= n; ++i) if (a[i].second.first > a[w].second.first) w = i; printf("%lld\n", w); continue; } sort(a + 1, a + n + 1); memset(f, 0xcf, sizeof(f)); f[0][0] = 0; for (long long i = 1; i <= n; ++i) for (long long j = 0; j <= i && j <= k; ++j) { if (j && f[i][j] < f[i - 1][j - 1] + a[i].second.first + a[i].first * (k - 1)) f[i][j] = f[i - 1][j - 1] + a[i].second.first + a[i].first * (j - 1), g[i][j] = 1; if (f[i][j] < f[i - 1][j] + a[i].first * (k - 1)) f[i][j] = f[i - 1][j] + a[i].first * (k - 1), g[i][j] = 0; } res.clear(); ans.clear(); for (long long i = n, j = k; i; --i) { if (g[i][j]) res.push_back(a[i].second.second), --j; else ans.push_back(a[i].second.second); } reverse(res.begin(), res.end()); printf("%lld\n", (long long)res.size() + ans.size() * 2); for (long long i = 0; i < k - 1; ++i) printf("%lld ", res[i]); for (long long i : ans) printf("%lld %lld ", i, -i); printf("%lld\n", res[k - 1]); } return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T &x) { x = 0; int f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } x *= f; } inline void write(int x) { if (x > 9) write(x / 10); putchar(x % 10 + '0'); } const int N = 160; const long long INF = 1ll << 62; long long a[N], b[N], id[N], n, m; inline bool upd(long long &x, long long v) { if (v <= x) return 0; x = v; return 1; } long long dp[N][N], lst[N][N]; int ans[N], lans; int a2[N << 1], l2; int tp[N]; inline void solve() { int i, j; read(n), read(m); for (i = 1; i <= n; ++i) tp[i] = 0, read(a[i]), read(b[i]), id[i] = i; for (i = 1; i <= n; ++i) for (j = i + 1; j <= n; ++j) if (b[i] < b[j]) swap(a[i], a[j]), swap(b[i], b[j]), swap(id[i], id[j]); for (i = 0; i <= n; ++i) for (j = 0; j <= n; ++j) dp[i][j] = -INF; dp[0][0] = 0; for (i = 1; i <= n; ++i) for (j = 0; j <= m; ++j) { if (dp[i - 1][j] >= 0 && upd(dp[i][j], dp[i - 1][j] + b[i] * (m - 1))) lst[i][j] = 0; if (j && dp[i - 1][j - 1] >= 0 && upd(dp[i][j], dp[i - 1][j - 1] + b[i] * (m - j) + a[i])) lst[i][j] = 1; } lans = 0; int nown = n, nowm = m; while (nown) { if (lst[nown][nowm]) ans[++lans] = nown, --nowm, tp[nown] = 1; --nown; } l2 = 0; for (i = 1; i < lans; ++i) a2[++l2] = id[ans[i]]; for (i = 1; i <= n; ++i) if (!tp[i]) { a2[++l2] = id[i]; a2[++l2] = -id[i]; } a2[++l2] = id[ans[lans]]; cout << l2 << '\n'; for (i = 1; i <= l2; ++i) cout << a2[i] << (i < l2 ? ' ' : '\n'); } int main() { int T; read(T); while (T--) solve(); return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; template <typename T, typename S> inline void chkmin(T &a, const S &b) { a = a < b ? a : b; } template <typename T, typename S> inline void chkmax(T &a, const S &b) { a = a > b ? a : b; } const int MAXN = 80; struct Data { int a, b, id; } dat[MAXN]; int f[MAXN][MAXN], g[MAXN][MAXN], ans[MAXN], T, n, K; bool cmp(const Data &a, const Data &b) { return a.b < b.b; } int main() { for (scanf("%d", &T); T--;) { scanf("%d%d", &n, &K); for (int i = 1; i <= n; i++) { scanf("%d%d", &dat[i].a, &dat[i].b); dat[i].id = i; } sort(dat + 1, dat + 1 + n, cmp); memset(f, 0xc0, sizeof(f)); f[0][0] = 0; memset(g, 0, sizeof(g)); memset(ans, 0, sizeof(ans)); for (int i = 1; i <= n; i++) { int a = dat[i].a, b = dat[i].b; for (int j = 1; j <= i && j <= K; j++) { int x = f[i - 1][j - 1] + a + b * (j - 1), y = f[i - 1][j] + b * (K - 1); f[i][j] = max(x, y); g[i][j] = x >= y ? 0 : 1; } f[i][0] = f[i - 1][0] + b * (K - 1); g[i][0] = 1; } for (int i = n, j = K; i > 0; i--) { if (g[i][j]) ans[i] = 1; else --j; } printf("%d\n", n + n - K); int cnt = 0; for (int i = 1; i <= n; i++) if (!ans[i] && ++cnt < K) printf("%d ", dat[i].id); assert(cnt == K); for (int i = 1; i <= n; i++) if (ans[i]) printf("%d %d ", dat[i].id, -dat[i].id); for (int i = n; i > 0; i--) if (!ans[i]) { printf("%d\n", dat[i].id); break; } } return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; template <class T> using vc = vector<T>; template <class T> using vvc = vc<vc<T>>; template <class T> void mkuni(vector<T>& v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } template <class T> void print(T x, int suc = 1) { cout << x; if (suc == 1) cout << '\n'; else cout << ' '; } template <class T> void print(const vector<T>& v, int suc = 1) { for (int i = 0; i < v.size(); i++) print(v[i], i == (int)(v.size()) - 1 ? suc : 2); } const int INF = 0x3f3f3f3f; const int maxn = 80; int n; int cost[maxn][maxn]; int lx[maxn], ly[maxn], match[maxn], slack[maxn]; int pprev[maxn]; bool vy[maxn]; void augment(int root) { memset(vy, 0, sizeof(vy)); memset(slack, 0x3f, sizeof(slack)); int py; match[py = 0] = root; do { vy[py] = true; int x = match[py], yy; int delta = INF; for (int y = 1; y <= n; y++) { if (!vy[y]) { if (lx[x] + ly[y] - cost[x][y] < slack[y]) slack[y] = lx[x] + ly[y] - cost[x][y], pprev[y] = py; if (slack[y] < delta) delta = slack[y], yy = y; } } for (int y = 0; y <= n; y++) { if (vy[y]) lx[match[y]] -= delta, ly[y] += delta; else slack[y] -= delta; } py = yy; } while (match[py] != -1); do { int pre = pprev[py]; match[py] = match[pre], py = pre; } while (py); } int KM() { for (int i = 1; i <= n; i++) { lx[i] = ly[i] = 0; match[i] = -1; for (int j = 1; j <= n; j++) lx[i] = max(lx[i], cost[i][j]); } int ans = 0; for (int root = 1; root <= n; root++) augment(root); for (int i = 1; i <= n; i++) ans += lx[i], ans += ly[i]; return ans; } int main() { int T; cin >> T; while (T--) { int k; cin >> n >> k; for (int i = 1; i <= n; i++) { int a, b; cin >> a >> b; for (int j = 1; j <= n; j++) { if (j <= k) cost[i][j] = (j - 1) * b + a; else cost[i][j] = (k - 1) * b; } } KM(); vector<int> ans; for (int i = 1; i < k; i++) ans.push_back(match[i]); for (int i = k + 1; i <= n; i++) { ans.push_back(match[i]); ans.push_back(-match[i]); } ans.push_back(match[k]); print(ans.size()); print(ans); } }
12
CPP
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + (c ^ 48); c = getchar(); } return f == 1 ? x : -x; } inline void print(int x) { if (x < 0) putchar('-'), x = ~x + 1; if (x > 10) print(x / 10); putchar((x % 10) | 48); } int dp[80][80][2]; int T; int n, k; struct node { pair<int, int> s; int id; bool operator<(const node &x) const { return s > x.s; } } a[100]; int m; vector<int> op1, op2, op3; void dfs(int x, int j, int op) { if (x == n + 1) return; if (op) { if (dp[x][j][op] == dp[x + 1][j][op] + a[x].s.first * (k - 1)) { op2.push_back(x); dfs(x + 1, j, op); } else if (dp[x][j][op] == dp[x + 1][j][0] + a[x].s.first * (k - 1) + a[x].s.second) { op3.push_back(x); dfs(x + 1, j, 0); } else if (j && dp[x][j][op] == dp[x + 1][j - 1][op] + a[x].s.first * (j - 1) + a[x].s.second) { dfs(x + 1, j - 1, op); op1.push_back(x); } } else { if (dp[x][j][0] == dp[x + 1][j][0] + a[x].s.first * (k - 1)) { op2.push_back(x); dfs(x + 1, j, 0); } else if (j && dp[x][j][0] == dp[x + 1][j - 1][0] + a[x].s.first * (j - 1) + a[x].s.second) { dfs(x + 1, j - 1, 0); op1.push_back(x); } } } vector<int> ans; int main() { T = read(); while (T--) { op1.clear(), op2.clear(), op3.clear(); n = read(), k = read(); for (int i = 1; i <= n; ++i) a[i].s.second = read(), a[i].s.first = read(), a[i].id = i; sort(a + 1, a + n + 1); memset(dp, -0x3f, sizeof(dp)); dp[n + 1][0][0] = 0; int ans = 0; for (int i = n; i >= 1; --i) { for (int j = 0; j <= (n - i + 1) && j < k; ++j) { dp[i][j][0] = dp[i + 1][j][0] + a[i].s.first * (k - 1); dp[i][j][1] = max(dp[i + 1][j][1] + a[i].s.first * (k - 1), dp[i + 1][j][0] + a[i].s.first * (k - 1) + a[i].s.second); if (j) { dp[i][j][0] = max(dp[i][j][0], dp[i + 1][j - 1][0] + a[i].s.first * (j - 1) + a[i].s.second); dp[i][j][1] = max(dp[i][j][1], dp[i + 1][j - 1][1] + a[i].s.first * (j - 1) + a[i].s.second); } } } dfs(1, k - 1, 1); printf("%d\n", op1.size() + op2.size() * 2 + 1); for (int i = 0; i < op1.size(); ++i) { printf("%d ", a[op1[i]].id); } for (int i = 0; i < op2.size(); ++i) printf("%d %d ", a[op2[i]].id, -a[op2[i]].id); printf("%d\n", a[op3[0]].id); } return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; pair<pair<int, int>, int> ba[102]; int g[102][102], dp[102][102]; int ans[102]; vector<int> ansvec; bool compba(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) { if (a.first.first != b.first.first) return a.first.first < b.first.first; return a.first.second < b.first.second; } int main() { int t; scanf("%d", &t); while (t--) { int n, k; scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { int ai, bi; scanf("%d%d", &ai, &bi); ba[i] = {{bi, ai}, i}; } sort(ba + 1, ba + n + 1, compba); for (int i = 0; i <= n; i++) for (int j = 0; j <= k; j++) dp[i][j] = -1; for (int i = 0; i <= n; i++) for (int j = 0; j <= k; j++) g[i][j] = -1; dp[0][0] = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j <= min(i - 1, k); j++) { int a0 = (k - 1) * ba[i].first.first; int a1 = min(k - 1, j) * ba[i].first.first + ba[i].first.second; if (dp[i][j] < dp[i - 1][j] + a0) { dp[i][j] = dp[i - 1][j] + a0; g[i][j] = 0; } if (j + 1 <= k) { if (dp[i][j + 1] < dp[i - 1][j] + a1) { dp[i][j + 1] = dp[i - 1][j] + a1; g[i][j + 1] = 1; } } } } int fi = n, fj = k; int lk = -1; while (fi) { int ai = ba[fi].second; if (g[fi][fj]) { ans[ai] = fj; if (fj == k) lk = ai; fj--; } else ans[ai] = 0; fi--; } ansvec.clear(); ansvec.assign(k - 1, 0); for (int i = 1; i <= n; i++) { if (ans[i]) { if (ans[i] < k) ansvec[ans[i] - 1] = i; } } for (int i = 1; i <= n; i++) { if (!ans[i]) { ansvec.push_back(i); ansvec.push_back(-i); } } ansvec.push_back(lk); printf("%d\n", ansvec.size()); for (auto it : ansvec) printf("%d ", it); printf("\n"); } return 0; }
12
CPP
#include <bits/stdc++.h> template <typename T> bool ckmax(T& a, T b) { return a < b ? a = b, 1 : 0; } template <typename T> bool ckmin(T& a, T b) { return b < a ? a = b, 1 : 0; } const int MN = 80; int N, RK, T, dp[MN][MN], ans; bool u[MN], pr[MN][MN]; struct minion { public: int a, b, id; void in() { scanf("%d%d", &a, &b); } bool operator<(minion o) const { return b < o.b; } } a[MN]; std::vector<int> seq; int main(void) { scanf("%d", &T); for (; T--;) { scanf("%d%d", &N, &RK); for (int i = 0; i < N; ++i) a[i].in(), a[i].id = i + 1; std::sort(a, a + N); memset(dp, -1, sizeof dp); dp[0][0] = 0; for (int i = 0; i < N; ++i) for (int j = 0; j <= RK; ++j) if (~dp[i][j]) { if (ckmax(dp[i + 1][j], dp[i][j] + (RK - 1) * a[i].b)) pr[i + 1][j] = 0; if (j + 1 <= RK && ckmax(dp[i + 1][j + 1], dp[i][j] + a[i].a + j * a[i].b)) pr[i + 1][j + 1] = 1; } { seq.clear(); ans = dp[N][RK]; memset(u, 0, N * sizeof u[0]); int k = RK; for (int i = N; i > 0; --i) { if (pr[i][k]) u[i - 1] = 1, seq.push_back(a[i - 1].id), --k; } assert(!k); std::reverse(seq.begin(), seq.end()); int v = seq.back(); seq.pop_back(); for (int i = 0; i < N; ++i) if (!u[i]) seq.push_back(a[i].id), seq.push_back(-a[i].id); seq.push_back(v); } for (int K = 1; K < RK; ++K) { memset(dp, -1, sizeof dp); dp[0][0] = 0; for (int i = 0; i < N; ++i) for (int j = 0; j <= K; ++j) if (~dp[i][j]) { if (ckmax(dp[i + 1][j], dp[i][j] + K * a[i].b)) pr[i + 1][j] = 0; if (j + 1 <= K && ckmax(dp[i + 1][j + 1], dp[i][j] + a[i].a + j * a[i].b)) pr[i + 1][j] = 1; } if (dp[N][K] <= ans) continue; seq.clear(); ans = dp[N][K]; memset(u, 0, N * sizeof u[0]); int k = K; for (int i = N; i >= 0; --i) { if (pr[i][k]) u[i - 1] = 1, seq.push_back(a[i - 1].id), --k; } assert(!k); std::reverse(seq.begin(), seq.end()); for (int i = 0; i < N; ++i) if (!u[i]) seq.push_back(a[i].id), seq.push_back(-a[i].id); } printf("%d\n", seq.size()); for (int i = 0; i < seq.size(); ++i) printf("%d%c", seq[i], " \n"[i + 1 == seq.size()]); } return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; inline int add(int _a, int _b) { if (_a < 0) { _a += 1000000007; } if (_b < 0) { _b += 1000000007; } if (_a + _b >= 1000000007) { return _a + _b - 1000000007; } return _a + _b; } inline int mul(int _a, int _b) { if (_a < 0) { _a += 1000000007; } if (_b < 0) { _b += 1000000007; } return ((long long int)((long long int)_a * (long long int)_b)) % 1000000007; } const int N = 75; namespace wm { bool vis[N + 5]; int U[N + 5], V[N + 5], P[N + 5], way[N + 5], minv[N + 5], match[N + 5], ar[N + 5][N + 5]; int hungarian(int n, int m, int mat[N + 5][N + 5], int flag) { memset(U, 0, sizeof(U)), memset(V, 0, sizeof(V)), memset(P, 0, sizeof(P)), memset(ar, 0, sizeof(ar)), memset(way, 0, sizeof(way)); int i, j; for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { ar[i][j] = mat[i][j]; if (flag == +1) ar[i][j] = -ar[i][j]; } } if (n > m) m = n; int a, b, c, d, r, w; for (i = 1; i <= n; i++) { P[0] = i, b = 0; for (j = 0; j < m + 1; j++) minv[j] = INT_MAX, vis[j] = 0; do { vis[b] = 1; a = P[b], d = 0, w = INT_MAX; for (j = 1; j <= m; j++) { if (!vis[j]) { r = ar[a][j] - U[a] - V[j]; if (r < minv[j]) minv[j] = r, way[j] = b; if (minv[j] < w) w = minv[j], d = j; } } for (j = 0; j < m + 1; j++) { if (vis[j]) U[P[j]] += w, V[j] -= w; else minv[j] -= w; } b = d; } while (P[b] != 0); do { d = way[b]; P[b] = P[d], b = d; } while (b != 0); } for (j = 1; j <= m; j++) match[j] = P[j]; return (flag == +1) ? V[0] : -V[0]; } } // namespace wm int n, A[N + 5], B[N + 5], choice, mat[N + 5][N + 5]; void solve() { int i, j; for (i = 0; i < n; i++) for (j = 0; j < n; j++) { if (j < choice - 1) mat[i + 1][j + 1] = A[i] + j * B[i]; else if (j >= choice - 1 && j < n - 1) mat[i + 1][j + 1] = (choice - 1) * B[i]; else mat[i + 1][j + 1] = A[i] + (choice - 1) * B[i]; } int sol = wm ::hungarian(n, n, mat, +1); printf("%d", choice - 1 + 2 * (n - choice) + 1), puts(""); vector<int> vec; for (i = 1; i <= choice - 1; i++) vec.push_back(wm ::match[i]); for (i = choice; i <= n - 1; ++i) vec.push_back(wm ::match[i]), vec.push_back(-wm ::match[i]); vec.push_back(wm ::match[n]); for (i = 0; i < vec.size(); i++) { if (i) printf(" "); printf("%d", vec[i]); } puts(""); } int main() { int cs, ts; scanf("%d", &ts); for (cs = 0; cs < ts; cs++) { int i, j; scanf("%d %d", &n, &choice); for (i = 0; i < n; i++) scanf("%d %d", &A[i], &B[i]); solve(); } }
12
CPP
#include <bits/stdc++.h> using namespace std; void solve() { int n, k; cin >> n >> k; vector<int> a(n), b(n); for (int i = 0; i < n; ++i) { cin >> a[i] >> b[i]; } vector<vector<long long>> dp(n + 1, vector<long long>(k + 2, 0)); vector<vector<int>> p(n + 1, vector<int>(k + 2)); vector<int> id(n); iota(id.begin(), id.end(), 0); sort(id.begin(), id.end(), [&](int i, int j) { return b[i] < b[j]; }); auto rlx = [&](int i, int j, long long val, int tk) { if (dp[i][j] < val) { dp[i][j] = val; p[i][j] = tk; } }; for (int i = 0; i < n; ++i) { int ii = id[i]; for (int j = 0; j <= min(i, k); ++j) { rlx(i + 1, j, dp[i][j] + (k - 1) * b[ii], 0); rlx(i + 1, j + 1, dp[i][j] + a[ii] + j * b[ii], 1); } } vector<int> u, v; for (int i = n, j = k; i >= 1; j -= p[i][j], --i) { if (p[i][j]) { u.push_back(id[i - 1]); } else { v.push_back(id[i - 1]); } } reverse(u.begin(), u.end()); cout << k + 2 * (n - k) << '\n'; for (int i = 0; i < k - 1; ++i) { cout << u[i] + 1 << ' '; } for (int i = 0; i < n - k; ++i) { cout << v[i] + 1 << ' ' << -(v[i] + 1) << ' '; } cout << u[k - 1] + 1; } int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int t; cin >> t; while (t--) { solve(); cout << '\n'; } return 0; }
12
CPP
#include <bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") using namespace std; template <typename T> T sqr(T a) { return a * a; } template <typename T> int sign(T a) { if (a == 0) return 0; return (a > 0 ? 1 : -1); } template <typename T> bool uax(T& a, const T b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool uin(T& a, const T b) { if (a > b) { a = b; return true; } return false; } template <typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& p) { in >> p.first >> p.second; return in; } template <typename T1, typename T2> ostream& operator<<(ostream& out, pair<T1, T2>& p) { out << p.first << ' ' << p.second; return out; } mt19937_64 rnd(0); const int LOG = 64; const int N = (int)1e5 + 7; const int MAXN = (int)4e5 + 7; const int MOD = (int)998244353; const int INF = (int)2e9 + 7; const int CONST = 450; const long long LINF = (int64_t)1e15; const long double PI = 3.1415926535897932384626433832795; const long double EPS = 1; struct Monster { int a, b, ind; Monster(){}; Monster(int a, int b, int ind) : a(a), b(b), ind(ind){}; }; bool operator<(const Monster& a, const Monster& b) { return a.b < b.b; } long long dp[100][100]; int par[100][100]; int n, k; Monster mon[N]; void solve() { cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> mon[i].a >> mon[i].b; mon[i].ind = i; } sort(mon + 1, mon + n + 1); for (int i = 0; i < 100; i++) { fill(dp[i], dp[i] + 100, -LINF); fill(par[i], par[i] + 100, -1); } dp[0][0] = 0; for (int i = 1; i <= n; i++) { par[i][0] = 0; dp[i][0] = dp[i - 1][0] + (k - 1) * mon[i].b; for (int j = 1; j <= k; j++) { if (dp[i - 1][j - 1] == -LINF && dp[i - 1][j] == -LINF) continue; if (dp[i - 1][j] == -LINF) { dp[i][j] = dp[i - 1][j - 1] + (j - 1) * mon[i].b + mon[i].a; par[i][j] = 1; continue; } if (dp[i - 1][j - 1] == -LINF) { dp[i][j] = dp[i - 1][j] + (k - 1) * mon[i].b; par[i][j] = 0; continue; } long long val1 = dp[i - 1][j - 1] + (j - 1) * mon[i].b + mon[i].a; long long val2 = dp[i - 1][j] + (k - 1) * mon[i].b; dp[i][j] = max(val1, val2); par[i][j] = (val1 >= val2 ? 1 : 0); } } int pos = n, cnt = k; vector<int> stay, rem; while (pos) { if (par[pos][cnt] == 0) { rem.push_back(mon[pos].ind); pos--; } else { stay.push_back(mon[pos].ind); pos--, cnt--; } } reverse(stay.begin(), stay.end()), reverse(rem.begin(), rem.end()); cout << (int)stay.size() + 2 * (int)rem.size() << "\n"; for (int i = 0; i < (int)stay.size() - 1; i++) cout << stay[i] << ' '; for (int x : rem) cout << x << ' ' << -x << ' '; cout << stay.back() << "\n"; } int main() { double start = clock(); cout << fixed << setprecision(20); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); rnd.seed(time(0)); int t = 1; cin >> t; for (int i = 0; i < t; i++) { solve(); } }
12
CPP
#include <bits/stdc++.h> using namespace std; template <typename A, typename B> ostream& operator<<(ostream& os, const pair<A, B>& x) { return os << "(" << x.first << "," << x.second << ")"; } int main() { int T; scanf("%d", &T); while (T--) { int n, k; scanf("%d%d", &n, &k); vector<pair<pair<int, int>, int>> M(n); long long offset = 0; for (int i = 0; i < n; ++i) { scanf("%d%d", &M[i].first.second, &M[i].first.first); offset += (k - 1) * M[i].first.second; M[i].second = i + 1; } sort(M.begin(), M.end()); const long long INF = 1LL << 60; vector<vector<vector<pair<long long, int>>>> dp( n + 1, vector<vector<pair<long long, int>>>( k + 1, vector<pair<long long, int>>(2, make_pair(-INF, -1)))); dp[0][0][0] = make_pair(0, 0); for (int i = 0; i < n; ++i) { for (int j = 0; j < k; ++j) { dp[i + 1][j][0] = max(dp[i + 1][j][0], make_pair(dp[i][j][0].first, 0)); dp[i + 1][j][1] = max(dp[i + 1][j][0], make_pair(dp[i][j][1].first, 0)); dp[i + 1][j][1] = max(dp[i + 1][j][1], make_pair(dp[i][j][0].first + M[i].first.second, 1)); if (j + 1 < k) { for (int v = 0; v < 2; ++v) { dp[i + 1][j + 1][v] = max(dp[i + 1][j + 1][v], make_pair(dp[i][j][v].first + M[i].first.second - (k - 1 - j) * M[i].first.first, 2)); } } } } if (false) cerr << "dp[n][k - 1][1]" << "=" << dp[n][k - 1][1] << endl; int j = k - 1, c = 1; int las = -1; vector<int> pick; for (int i = n; i >= 1; --i) { int act = dp[i][j][c].second; assert(act != -1); if (act == 0) { } else if (act == 1) { assert(c == 1); las = i - 1; c = 0; } else { pick.push_back(i - 1); --j; } } assert(j == 0); assert(c == 0); reverse(pick.begin(), pick.end()); assert(las != -1); vector<bool> used(n + 1); vector<int> ans; for (int x : pick) { ans.push_back(M[x].second); used[M[x].second] = true; } used[M[las].second] = true; for (int i = 1; i <= n; ++i) { if (used[i]) continue; ans.push_back(i); ans.push_back(-i); } ans.push_back(M[las].second); printf("%d\n", ans.size()); for (int x : ans) printf("%d ", x); printf("\n"); } }
12
CPP
#include <bits/stdc++.h> using namespace std; inline int read() { register int t = 0; register char v = getchar(); while (v < '0') v = getchar(); while (v >= '0') t = (t << 3) + (t << 1) + v - 48, v = getchar(); return t; } int f[76][76], n, m, t, v[76][76], ans[76], cnt, pos; struct node { int x, y, id; bool operator<(const node a) const { return y < a.y; }; } p[76]; inline void dfs(register int x, register int y) { if (!x) return; if (v[x][y]) ans[x] = 1, --y; dfs(x - 1, y); } int main() { t = read(); while (t--) { n = read(), m = read(); for (register int i = 1; i <= n; ++i) p[i].x = read(), p[i].y = read(), p[i].id = i; sort(p + 1, p + n + 1), memset(v, 0, sizeof(v)), memset(f, -0x3f, sizeof(f)); f[0][0] = 0; for (register int i = 1; i <= n; ++i) for (register int j = 0; j <= min(i, m); ++j) { f[i][j] = f[i - 1][j] + p[i].y * (m - 1); if (j && f[i - 1][j - 1] + p[i].x + p[i].y * (j - 1) > f[i][j]) f[i][j] = f[i - 1][j - 1] + p[i].x + p[i].y * (j - 1), v[i][j] = 1; } printf("%d\n", m + (n - m) * 2); cnt = 0; memset(ans, 0, sizeof(ans)), dfs(n, m); for (register int i = 1; i <= n; ++i) if (ans[i] && (++cnt != m)) printf("%d ", p[i].id); else if (ans[i]) pos = i; for (register int i = 1; i <= n; ++i) if (!ans[i]) printf("%d %d ", p[i].id, -p[i].id); printf("%d\n", p[pos].id); } }
12
CPP