NC21296. 数组构造
描述
输入描述
输入四个整数
其中
输出描述
输出"Possible"如果能构造出来
否则输出"Impossible"
示例1
输入:
2 3 7 18
输出:
Possible
示例2
输入:
1 1 3 5
输出:
Impossible
示例3
输入:
3 5 300 500
输出:
Possible
示例4
输入:
100 1 0 2
输出:
Impossible
示例5
输入:
1000000000 1000000000 1000000000 1000000000
输出:
Possible
C++14(g++5.4) 解法, 执行用时: 4ms, 内存消耗: 404K, 提交时间: 2020-08-05 23:38:45
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll d1, d2, w1, w2; ll l[5], r[5]; ll get(ll n, ll num){ return (2 * n - num + 1) * num; } void solve(ll n, ll m, int id){ r[id] = 2 * m / n + 1 - n; r[id] /= 2; ll L, R, mid; if((n + 1) * n > 2 * m){ L = 0; R = n; while(R - L > 1){ mid = (L + R) / 2; if(get(mid, mid) <= 2 * m) L = mid; else R = mid; } }///0-n else{ L = n, R = 1e10; while(R - L > 1){ mid = (L + R) / 2; if(get(mid, n) <= 2 * m) L = mid; else R = mid; } }///n-无穷 l[id] = L; while((2 * r[id] + n - 1) * n >= 2 * m) r[id]--; while((2 * r[id] + n - 1) * n < 2 * m) r[id]++; swap(l[id], r[id]); if(l[id] < 0) l[id] = 0; } void print(int id){ printf("l = %lld r = %lld\n", l[id], r[id]); } int main(){ scanf("%lld%lld%lld%lld", &d1, &d2, &w1, &w2); solve(d1, w1, 1); solve(d2, w2, 2); //print(1); print(2); if(l[1] > r[1] || l[2] > r[2] || l[1] < 0 || r[1] < 0 || l[2] < 0 || r[2] < 0){ puts("Impossible"); } else{ l[1]--; r[1]++; if(l[1] < 0) l[1] = 0; if(r[1] < 0) r[1] = 0; if(l[2] > r[1] || r[2] < l[1]){ puts("Impossible"); } else{ puts("Possible"); } } return 0; }
C++11(clang++ 3.9) 解法, 执行用时: 4ms, 内存消耗: 616K, 提交时间: 2020-03-24 22:44:58
#include<bits/stdc++.h> using namespace std; int d1, d2, w1, w2; int main(){ scanf("%d%d%d%d", &d1, &d2, &w1, &w2); long long t1 = 1ll*(d1-1)*d1/2, t2 = 1ll*(d2-1)*d2/2; long long a = max(0ll, (w1-t1+d1-1)/d1); long long b = max(0ll, (w2-t2+d2-1)/d2); long long c, d; if(w1 < t1) c = static_cast<int>(sqrt(2.0*w1+0.25)-0.5); else c = 1ll*(w1+t1)/d1; if(w2 < t2) d = static_cast<int>(sqrt(2.0*w2+0.25)-0.5); else d = 1ll*(w2+t2)/d2; if(max(a,b)-min(c,d) <= 1) puts("Possible"); else puts("Impossible"); return 0; }