codeforces round 881 div2 题解
马上要秋招了,自己本事全丢了,感觉如果这样的话今年就估计要饿死了。先打div3,7月份得开始收心了
A. Sasha and Array Coloring
题意,可以分任意组,每组的贡献是max - min,问最大贡献
显然是贪心,从大到小配对一下就行,不想放代码了’
B. Long Long
给出一个数列a,我们一次能反转一个区间的正负,问我们最少需要多少次操作才能使得a中所有的数都是正数
思路:
记录下标,贪心,然后expand一下,记得特判区间全是0的情况
AC代码
#include <bits/stdc++.h>
using namespace std;
constexpr int limit = (4e5 + 5);//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-9
#define FASTIO ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
#define pi(a, b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout)
typedef long long ll;
typedef unsigned long long ull;
char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;
inline ll read() {
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
ll sign = 1, x = 0;
char s = getchar();
while (s > '9' || s < '0') {
if (s == '-')sign = -1;
s = getchar();
}
while (s >= '0' && s <= '9') {
x = (x << 3) + (x << 1) + s - '0';
s = getchar();
}
return x * sign;
#undef getchar
}//快读
void print(ll x) {
if (x / 10) print(x / 10);
*O++ = x % 10 + '0';
}
void write(ll x, char c = 't') {
if (x < 0)putchar('-'), x = -x;
print(x);
if (!isalpha(c))*O++ = c;
fwrite(obuf, O - obuf, 1, stdout);
O = obuf;
}
int n;
int a[limit];
void solve(){
cin>>n;
ll ans = 0;
deque<int>v;
rep(i, 1, n){
cin>>a[i];
ans += abs(a[i]);
if(a[i] <= 0)v.push_back(i);
}
int res = 0;
while(v.size()){
auto cur = v.front();
v.pop_front();
bool is_negative = a[cur] < 0;
while(v.size() and v.front() == cur + 1) {
cur = v.front();
is_negative |= a[cur] < 0;
v.pop_front();
}
if(is_negative)res++;
}
cout<<ans<<" "<<res<<endl;
};
int32_t main() {
#ifdef LOCAL
FOPEN;
// FOUT;
#endif
FASTIO
int kase;
cin>>kase;
while (kase--)
invoke(solve);
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s";
return 0;
}
C.Sum in Binary Tree
给一个完全二叉树,bfs序就是weight,问给出一个node走到1的路径总和
思路:
稍加观察我们发现这完全就是 /2的过程,所以模拟就行
AC代码
#include <bits/stdc++.h>
using namespace std;
constexpr int limit = (4e5 + 5);//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-9
#define FASTIO ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
#define pi(a, b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout)
typedef long long ll;
typedef unsigned long long ull;
char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;
inline ll read() {
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
ll sign = 1, x = 0;
char s = getchar();
while (s > '9' || s < '0') {
if (s == '-')sign = -1;
s = getchar();
}
while (s >= '0' && s <= '9') {
x = (x << 3) + (x << 1) + s - '0';
s = getchar();
}
return x * sign;
#undef getchar
}//快读
void print(ll x) {
if (x / 10) print(x / 10);
*O++ = x % 10 + '0';
}
void write(ll x, char c = 't') {
if (x < 0)putchar('-'), x = -x;
print(x);
if (!isalpha(c))*O++ = c;
fwrite(obuf, O - obuf, 1, stdout);
O = obuf;
}
int n;
int a[limit];
void solve(){
ll x;
cin>>x;
ll ans = 0;
while (x != 1){
ans += x;
x >>= 1;
}
cout<<ans+1ll<<endl;
};
int32_t main() {
#ifdef LOCAL
FOPEN;
// FOUT;
#endif
FASTIO
int kase;
cin>>kase;
while (kase--)
invoke(solve);
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s";
return 0;
}
D. Apple Tree
题意是给你一个树,然后你有两个苹果,每个苹果可能从子树的任何一个叶子节点掉下去,给出出生点,问你最后两个苹果会掉的位置的对数总和
这题很简单,就是题面真恶心,我看了半天才看懂题意,然后就是算一下每个点以下的叶子节点size就行
AC代码
#include <bits/stdc++.h>
using namespace std;
constexpr int limit = (4e5 + 5);//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-9
#define FASTIO ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
#define pi(a, b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout)
typedef long long ll;
typedef unsigned long long ull;
char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;
inline ll read() {
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
ll sign = 1, x = 0;
char s = getchar();
while (s > '9' || s < '0') {
if (s == '-')sign = -1;
s = getchar();
}
while (s >= '0' && s <= '9') {
x = (x << 3) + (x << 1) + s - '0';
s = getchar();
}
return x * sign;
#undef getchar
}//快读
void print(ll x) {
if (x / 10) print(x / 10);
*O++ = x % 10 + '0';
}
void write(ll x, char c = 't') {
if (x < 0)putchar('-'), x = -x;
print(x);
if (!isalpha(c))*O++ = c;
fwrite(obuf, O - obuf, 1, stdout);
O = obuf;
}
int n;
int a[limit];
vector<int>g[limit];
int leaf[limit];
void dfs(int u,int pre){
if(u != 1 and g[u].size() == 1){
leaf[u] = 1;
return;
}
for(auto v : g[u]) {
if (v == pre)continue;
dfs(v, u);
leaf[u] += leaf[v];
}
}
void solve(){
cin>>n;
rep(i,1,n)g[i].clear(), leaf[i] = 0;
rep(i, 1, n - 1){
int x, y;
cin>>x>>y;
g[x].push_back(y);
g[y].push_back(x);
}
int q;
cin>>q;
dfs(1, -1);
while(q--){
int x, y;
cin>>x>>y;
ll ans = 1ll*leaf[x] * leaf[y];
cout<<ans<<endl;
}
};
int32_t main() {
#ifdef LOCAL
FOPEN;
// FOUT;
#endif
FASTIO
int kase;
cin>>kase;
while (kase--)
invoke(solve);
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s";
return 0;
}
E. Tracking Segments
这个题问给一个全0的序列和一些区间,然后给一个操作序列每次把一个位置set on,如果一个区间里面1的个数占绝对多数,就是beautiful区间,问你第一个beautiful区间出现最早时间
妈的,因为很久不写题了,直接想上主席树(事实上也完全没有错),但是主席树我没板子,抄别人的板子锅了,然后突然发现这不就一傻逼二分么,哎
AC代码
#include <bits/stdc++.h>
using namespace std;
constexpr int limit = (4e5 + 5);//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-9
#define FASTIO ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
#define pi(a, b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout)
typedef long long ll;
typedef unsigned long long ull;
char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;
inline ll read() {
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
ll sign = 1, x = 0;
char s = getchar();
while (s > '9' || s < '0') {
if (s == '-')sign = -1;
s = getchar();
}
while (s >= '0' && s <= '9') {
x = (x << 3) + (x << 1) + s - '0';
s = getchar();
}
return x * sign;
#undef getchar
}//快读
void print(ll x) {
if (x / 10) print(x / 10);
*O++ = x % 10 + '0';
}
void write(ll x, char c = 't') {
if (x < 0)putchar('-'), x = -x;
print(x);
if (!isalpha(c))*O++ = c;
fwrite(obuf, O - obuf, 1, stdout);
O = obuf;
}
int n;
int a[limit];
vector<int>g[limit];
int pref[limit];
const int MAXN=2e5+50,SIZE=MAXN*20;
int N,M,K;
int A[MAXN],rt[MAXN];
int sz,lson[SIZE],rson[SIZE],sum[SIZE];
int init(int l,int r){
int cur=++sz;
if(l<r){
int mid=(l+r)>>1;
lson[cur]=init(l,mid);
rson[cur]=init(mid+1,r);
}
return cur;
}
int modify(int pre,int l,int r,int x){
int cur=++sz;
lson[cur]=lson[pre];
rson[cur]=rson[pre];
sum[cur]=sum[pre]+1;
int mid=(l+r)>>1;
if(l<r){
if(x<=mid)
lson[cur]=modify(lson[pre],l,mid,x);
else
rson[cur]=modify(rson[pre],mid+1,r,x);
}
return cur;
}
int query(int x,int y,int l,int r,int k){
if(l==r)
return l;
int ii=sum[lson[y]]-sum[lson[x]],mid=(l+r)>>1;
if(ii>=k){
return query(lson[x],lson[y],l,mid,k);
}else{
return query(rson[x],rson[y],mid+1,r,k-ii);
}
}
void solve(){
vector<pi(int, int)>p;
int m;
cin>>n>>m;
sz = 0;
rep(i,0,n){
lson[i] = 0;
rson[i] = 0;
sum[i] = rt[i] = A[i] = 0;
pref[i] = 0;
a[i] = 0;
A[i] = 0;
}
rep(i,1,m){
int l,r;
cin>>l>>r;
p.push_back({l, r});
}
int q;
cin>>q;
rep(i, 1, q){
int x;
cin>>x;
a[x] = i;
A[i] = a[i];
}
auto check = [&](int x)->bool {
rep(i,1,n){
pref[i] = pref[i - 1] + (a[i] <= x and a[i]);
}
for(const auto &[l, r] : p){
int one = pref[r] - pref[l - 1];
int zero = r - l + 1 - one;
if(one > zero){
return true;
}
}
return false;
};
int ans = -1;
for(int l = 1, r = n; l <= r; ){
int mid = l + (r - l) / 2;
if(check(mid)){
ans = mid;
r = mid - 1;
}else {
l = mid + 1;
}
}
cout<<ans<<endl;
};
int32_t main() {
#ifdef LOCAL
FOPEN;
// FOUT;
#endif
FASTIO
int kase;
cin>>kase;
while (kase--)
invoke(solve);
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s";
return 0;
}
F1.Omsk Metro (simple version)
这题题面和D一样屎,我读了半天才发现是个树,然后给一对节点,问这两个节点之间的路径上能否凑出来一个k的路径,possibly empty。节点是动态添加的。easy和hard区别是easy里面的两个节点在query里面有一个固定为1
然后这个题输入也很烦,不过很简单。就是拿DP判一下k是否在某个范围内就好了,既然是从1开始,那么久记录一下当前路径(i个节点被添加之后)最大最小值,然后判断一下范围,因为如果有k \(\in [l,r]\) 我们不需要知道具体方案,我们只需要知道max区间可以拓展得到k,而min区间同理
AC code
#include <bits/stdc++.h>
using namespace std;
constexpr int limit = (4e5 + 5);//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-9
#define FASTIO ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
#define pi(a, b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout)
typedef long long ll;
typedef unsigned long long ull;
char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;
inline ll read() {
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
ll sign = 1, x = 0;
char s = getchar();
while (s > '9' || s < '0') {
if (s == '-')sign = -1;
s = getchar();
}
while (s >= '0' && s <= '9') {
x = (x << 3) + (x << 1) + s - '0';
s = getchar();
}
return x * sign;
#undef getchar
}//快读
void print(ll x) {
if (x / 10) print(x / 10);
*O++ = x % 10 + '0';
}
void write(ll x, char c = 't') {
if (x < 0)putchar('-'), x = -x;
print(x);
if (!isalpha(c))*O++ = c;
fwrite(obuf, O - obuf, 1, stdout);
O = obuf;
}
int n;
int a[limit];
vector<int>g[limit];
int dp[limit][2];
int minn[limit], maxx[limit];
void solve(){
cin>>n;
int tot = 1;
dp[1][1] = dp[1][0] = 1;
maxx[1] = 1;
minn[1] = 0; //no opt
rep(i,1,n){
string op;
cin>>op;
if(op == "+"){
int fa, x;
cin>>fa>>x;
++tot; //增加新儿子
auto fa_max = dp[fa][0];
auto fa_min = dp[fa][1];
auto&& self_max = dp[tot][0];
auto&& self_min = dp[tot][1];
self_max = max({x, fa_max + x, fa_min + x});
self_min = min({x, fa_min + x, fa_max + x});
// self_max = max(self_max, x);
// self_min = min(self_min, x);
minn[tot] = min({self_min, minn[fa], 0});
maxx[tot] = max({self_max, maxx[fa], 0});
}else {
int u, v, x;
cin>>u>>v>>x;
// cout<<x<<" "<<maxx[v]<<" "<<minn[v]<<endl;
if(x <= maxx[v] and x >= minn[v]){
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;
}
}
}
};
int32_t main() {
#ifdef LOCAL
FOPEN;
// FOUT;
#endif
FASTIO
int kase;
cin>>kase;
while (kase--)
invoke(solve);
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s";
return 0;
}
F2我没看,因为我记得我写过一样的,很简单,就是树剖然后用线段树维护一下最大最小值,拼起来,但是好像找不到了,等下看看再。
标签:F1,881,int,题解,ll,cin,long,rep,define From: https://www.cnblogs.com/tiany7/p/17521038.html