Dashboard - Codeforces Round 592 (Div. 2) - Codeforces
时间复杂度 O ( 1 ) 时间复杂度O(1) 时间复杂度O(1)
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
int a , b , c , d , k , t;
signed main(){
cin >> t;
while(t --){
cin >> a >> b >> c >> d >> k;
int cnt1 = (a - 1) / c + 1;
int cnt2 = (b - 1) / d + 1;
if(cnt1 + cnt2 <= k){
cout << cnt1 << " " << k - cnt1 << "\n";
} else {
cout << "-1\n";
}
}
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
时间复杂度 O ( n ) 时间复杂度O(n) 时间复杂度O(n)
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
int t , n;
string s;
signed main(){
IOS
cin >> t ;
while(t --){
cin >> n >> s;
int maxx = n;
for(int i = 0 ; i < n ; i ++){
if(s[i] == '1') maxx = max(maxx , (i + 1) * 2);
}
for(int i = n - 1 ; i >= 0 ; i --){
if(s[i] == '1') maxx = max(maxx , (n - i) * 2);
}
cout << maxx << "\n";
}
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
x ∗ w + y ∗ d = p ( 1 ) x*w+y*d=p~~(1) x∗w+y∗d=p (1)
x + y + z = n ( 2 ) x+y+z=n~~(2) x+y+z=n (2)
x = x 0 ∗ p g c d ( w , d ) + k ∗ d g c d ( w , d ) x=x_0*{p\over gcd(w,d)}+{k*d\over gcd(w,d)} x=x0∗gcd(w,d)p+gcd(w,d)k∗d
y = y 0 ∗ p g c d ( w , d ) − k ∗ w g c d ( w , d ) y=y_0*{p\over gcd(w,d)}-{k*w\over gcd(w,d)} y=y0∗gcd(w,d)p−gcd(w,d)k∗w
时间复杂度 O ( l o g n ) 时间复杂度O(logn) 时间复杂度O(logn)
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int __int128
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
inline void read(int &n){
int x = 0 , f = 1;char ch = getchar();
while(ch < '0' || ch > '9'){ if(ch == '-') f = -1; ch = getchar(); }
while(ch >= '0' && ch <= '9'){ x = (x << 1) + (x << 3) + (ch ^ 48); ch=getchar();}
n = x * f;
}
inline void write(int n){
if(n < 0){ putchar('-'); n *= -1; }
if(n > 9) write(n / 10);
putchar(n % 10 + '0');
}
int exgcd(int a , int b , int &x , int &y){
if(b == 0){ x = 1; y = 0; return a;}
int g = exgcd(b , a % b , y , x);
y -= a / b * x;
return g;
}
int n , p , w , d , g , x , y;
signed main(){
read(n);read(p);read(w);read(d);
g = exgcd(w , d , x , y);
if(p % g){
write(-1);
}else{
w /= g;d /= g;p /= g;
int k = floor(1.0L * (y * p) / (1.0L * w));
int xx = x * p + k * d;
int yy = y * p - k * w;
if(xx + yy > n || xx < 0){
write(-1);
}else{
write(xx);
putchar(' ');
write(yy);
putchar(' ');
write(n - (xx + yy));
}
}
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
时间复杂度 O ( n l o g n ) 时间复杂度O(nlogn) 时间复杂度O(nlogn)
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
int val[N][4] , n , cnt , in[N] , lis[4];
vector<int>ve[N];
bool vis[N];
struct node{
int x , ans;
}a[N];
bool bfs(){
queue<int>q;
for(int i = 1 ; i <= n ; i ++){
if(in[i] == 1) {
q.push(i);
a[++cnt].x = i;
vis[i] = 1;
break;
}
}
while(q.size()){
int now = q.front();q.pop();
for(auto k : ve[now]){
if(vis[k]) continue;
vis[k] = 1;
q.push(k);
a[++cnt].x = k;
}
if(q.size() > 1) return 0;
}
return 1;
}
signed main(){
cin >> n;
for(int i = 1 ; i <= 3 ; i ++){
for(int j = 1 ; j <= n ; j ++){
cin >> val[j][i];
}
}
for(int i = 1 ; i <= n - 1 ; i ++){
int u , v;
cin >> u >> v;
in[u] += 1;
in[v] += 1;
ve[u].push_back(v);
ve[v].push_back(u);
}
if(bfs()){
int all = 6;
for(int i = 0 ; i < 3 ; i ++) lis[i] = i + 1;
int minn = 9e18;
while(all --) {
int now = 0;
for(int i = 1 ; i <= n ; i ++){
now += val[a[i].x][lis[i % 3]];
}
if(now < minn) {
minn = now;
for(int i = 1 ; i <= n ; i ++){
a[i].ans = lis[i % 3];
}
}
next_permutation(lis , lis + 3);
}
sort(a + 1 , a + 1 + n , [&](node x , node y){
return x.x < y.x;
}
);
cout << minn << "\n";
for(int i = 1 ; i <= n ; i ++){
cout << a[i].ans << " ";
}
cout << "\n";
}else{
cout << "-1\n";
}
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
时间复杂度 O ( n ) 时间复杂度O(n) 时间复杂度O(n)
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
int n , k;
int a[N];
signed main(){
cin >> n >> k;
for(int i = 1 ; i <= n ; i ++) cin >> a[i];
sort(a + 1 , a + 1 + n);
int l = 1 , r = n;
int cntl = 1 , cntr = 1;
int now = a[r] - a[l] , res = 0;
while(l < r) {
while(a[l] == a[l + 1]) l += 1 , cntl += 1;
while(a[r] == a[r - 1]) r -= 1 , cntr += 1;
if(cntl < cntr) {
res = (a[l + 1] - a[l]) * cntl;
if(k >= res) {
k -= res;
now -= (a[l + 1] - a[l]);
l += 1;cntl += 1;
} else {
int cntk = k / cntl;
now -= cntk;
break;
}
} else {
res = (a[r] - a[r - 1]) * cntr;
if(k >= res) {
k -= res;
now -= (a[r] - a[r - 1]);
r -= 1;cntr += 1;
} else {
int cntk = k / cntr;
now -= cntk;
break;
}
}
}
cout << max(0ll , now) << "\n";
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
时间复杂度 O ( n l o g n ) 时间复杂度O(nlogn) 时间复杂度O(nlogn)
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
int n , m ;
string s;
int vis[N] , now[N];
int nex(int x) {
return (x + 1) % n;
}
int pre(int x){
return (x - 1 + n) % n;
}
signed main(){
cin >> n >> m >> s;
for(int i = 0 ; i < n ; i ++) {
if(s[i] == s[nex(i)]) {
if(s[i] == 'B') vis[i] = vis[nex(i)] = 1;
else vis[i] = vis[nex(i)] = -1;
}
}
int cnt = 0;
for(int i = 0 ; i < n ; i ++) {
if(vis[i]) cnt += 1;
}
if(cnt == n) {
cout << s << "\n";
} else if (cnt == 0) {
if(m & 1) {
for(int i = 0 ; i < n ; i ++) if(s[i] == 'B') s[i] = 'W'; else s[i] = 'B';
}
cout << s << "\n";
} else {
set<tuple<int , int , int>>info;
for(int i = 0 ; i < 2 * n ; i ++) {
if(i < n) now[i] = vis[i];
else now[i] = vis[i - n];
// cout << now[i] << " ";
}
// cout << "\n";
int l = 0 , r = 2 * n - 1;
while(!now[l]) l += 1;
while(!now[r]) r -= 1;
// cout << l << " " << r << '\n';
bool tag = 0;
int ans_l = 0 , ans_r = 0 , len = 0;
for(int i = l ; i <= r ; i ++) {
if(now[i]) {
if(!tag) continue;
ans_r = (i - 1) % n;
info.insert({ans_l , ans_r , len});
len = 0;tag = 0;
continue;
}
if(!tag) {
ans_l = i % n;
tag = 1;
len += 1;
} else {
len += 1;
}
}
// for(int i = 0 ; i < n ; i ++) cout << vis[i] << " " ; cout << "\n";
for(auto [l , r , len] : info) {
// cout << l << " " << r << " " << len << "\n";
int typel = vis[pre(l)];
int typer = vis[nex(r)];
if(m >= (len + 1) / 2) {
for(int i = 1 , j = l , k = r; i <= (len + 1) / 2 ; i ++ , j = nex(j) , k = pre(k)) {
vis[j] = typel;
vis[k] = typer;
}
} else {
int j = l , k = r;
for(int i = 1 ; i <= m ; i ++ , j = nex(j) , k = pre(k)) {
vis[j] = typel;
vis[k] = typer;
}
if(m & 1) for(int i = j ; i != nex(k) ; i = nex(i)) if(s[i] == 'W') vis[i] = 1 ; else vis[i] = -1;
else for(int i = j ; i != nex(k) ; i = nex(i)) if(s[i] == 'W') vis[i] = -1 ; else vis[i] = 1;
}
}
for(int i = 0 ; i < n ; i ++ ){
if(vis[i] == 1) cout << 'B';
else if(vis[i] == -1) cout << 'W';
}
}
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
/*
5 1
WBBWB
*/
[ n , n − 1 , . . . , 2 , 1 ] [n ~,~ n-1~,~... ~~,~2~,~1] [n , n−1 , ... , 2 , 1]
( n + 1 ) ∗ n 2 \frac{(n+1)*n}{2} 2(n+1)∗n
[ n , n , n − 1 , n − 1 , . . . , ( n + 1 ) / 2 ] [n ~,~ n~, n-1~,n-1,~... ~~,~(n+1)/2~] [n , n ,n−1 ,n−1, ... , (n+1)/2 ]
( n / 2 ) ∗ ( n + 1 + ( n + 1 ) / 2 ) + ( n + 1 ) / 2 ∗ ( n & 1 ) (n / 2) * (n + 1 + (n + 1) / 2) + (n + 1) / 2 * (n~\&~1) (n/2)∗(n+1+(n+1)/2)+(n+1)/2∗(n & 1)
| 15 | [5 , 4 , 3 , 2 , 1] |
|---|---|
| 16 | [5 , 5 , 3 , 2 , 1] |
| 17 | [5 , 5 , 4 , 2 , 1] |
| 18 | [5 , 5 , 4 , 3 , 1] |
| 19 | [5 , 5 , 4 , 4 , 1] |
| 20 | [5 , 5 , 4 , 4 , 2] |
| 21 | [5 , 5 , 4 , 4 , 3] |
时间复杂度 O ( n ) 时间复杂度O(n) 时间复杂度O(n)
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
int n , k;
int pre[N] , nex[N] , cnt[N] , ans[N];
bool vis[N];
signed main(){
cin >> n >> k;
int l = (n + 1) * n / 2;
int r = (n / 2) * (n + 1 + (n + 1) / 2) + (n + 1) / 2 * (n & 1);
// cout << l << " " << r << "\n";
if(k < l) {
cout << "-1\n";
} else if(k > r) {
cout << r << "\n";
for(int i = 1 ; i <= n ; i ++) cout << i << " ";cout << "\n";
for(int i = n ; i >= 1 ; i --) cout << i << " ";cout << "\n";
} else {
cout << k << "\n";
for(int i = 1 ; i <= n ; i ++) pre[i] = (n - i + 1);
for(int i = 1 , j = n ; i <= n ; i ++) {
nex[i] = j;
if(!(i & 1)) j -= 1;
}
int res = k - l;
for(int i = 1 ; i <= n ; i ++) {
int now = nex[i] - pre[i];
if(now <= res) {
res -= now;
pre[i] += now;
} else {
pre[i] += res;
break;
}
}
for(int i = 1 ; i <= n ; i ++) {
if(vis[pre[i]]) {
ans[i] = pre[i];
pre[i] = 0;
} else {
vis[pre[i]] = 1;
}
}
for(int i = 1 , j = n ; i <= n ; i ++) {
while(vis[j]) j -= 1;
if(pre[i]) continue;
vis[j] = 1;
pre[i] = j;
}
for(int i = 1 ; i <= n ; i ++) vis[i] = 0;
for(int i = 1 ; i <= n ; i ++) vis[ans[i]] = 1;
for(int i = 1 , j = n ; i <= n ; i ++) {
while(vis[j]) j -= 1;
if(ans[i]) continue;
vis[j] = 1;
ans[i] = j;
}
for(int i = 1 ; i <= n ; i ++) cout << ans[i] << " ";cout << "\n";
for(int i = 1 ; i <= n ; i ++) cout << pre[i] << " ";cout << "\n";
}
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
/*
5 5 4 4 3
5 4 3 2 1
加不会溢出 , 减会溢出
*/