CF 800D Fake Plastic Tree
CF 800D Fake Plastic Tree
首先这道题给了一棵有根树,然后对于每个节点给了区间[l, r],要求每次选取一个节点v,那么从1-v的路径上的点需要加一个c,这个c按照depth应该是非严格升序,问最少要填多少次?
思路
这个题很明显有点树形dp的味道在里面,那么我们先整理出来一些简单的点。我刚开始以为是按照l[i]填,然后判断是否小于当前的l[u],后来发现似乎不用考虑没有解的情况(因为样例没给),那就应该贪心从叶子节点填r[u],然后因为我们按照dfs序去填,所以我们只用判断下面填了多少,如果没有到l[u], 那么我们需要为这个节点单独填一次,次数 = $ \Sigma$ dp[v] + 1, 然后我们一旦在这个节点 + 1, 我们肯定要贪心填r,然后继续更新,向上传递
代码
AC Code
#include <bits/stdc++.h>
using namespace std;
constexpr int limit = (2e6 + 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,m;
int a[limit];
int l[limit],r[limit];
vector<int>g[limit];
int dp[limit];
int sizes[limit];
ll dfs(int u, int fa){
// cout<<u<<endl;
// sizes[u] = 1;
if(g[u].empty()){
dp[u] = 1;
return r[u];
}
for(auto v : g[u]){
if(v == fa)continue;
auto res = dfs(v,u);
dp[u] += dp[v];
sizes[u] += res;
}
if(sizes[u] < l[u]){
dp[u]++;
return r[u];
}
return min(sizes[u], r[u]);
}
void solve(){
cin>>n;
rep(i,1,n) g[i].clear();
rep(i,2,n){int x;cin>>x,g[x].push_back(i);}
rep(i,1,n){
cin>>l[i]>>r[i];
dp[i] = 0;
sizes[i] = 0;
}
// cout<<"enter"<<endl;
dfs(1,-1);
cout<<dp[1]<<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\n";
return 0;
}
然后还有几道今天的题,就放E和F吧
Multiples and Power Differences
这道题给一个2D矩阵a,要求矩阵 \(b_{i, j}\) 是 \(a_{i, j}\) 的倍数,然后 \(b_{i, j}\) 和相邻的格子差diff至少为1,并且 \(diff = k^{4}\) ,k为任意正整数
然后这道题,首先我们发现元素范围1-16,这个肯定是一个hint。之后我们考虑下怎么fix。当然,直接暴力是不可能的,因为1-16两个数不一定能写成4次方的形式,我们更应该考虑位置而不是怎么去把这些数字枚举出来。然后我们看看能不能给这些数找一个基准点。那就是所有数的倍数,或者说所有数字的最小公倍数lcm。然后对于每个格子,我们可以用二部图那样去处理,首先如果一个格子被染成灰色,那么我们就把lcm赋值给这个格子,如果这个格子属于白色,那么我们应该就加一个 \(a_{i,j}^{4}\) 给这个格子,然后我们每两个相邻格子之间自然就是4次方数了。
代码
AC Code
#include <bits/stdc++.h>
using namespace std;
constexpr int limit = (2e6 + 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;
}
#define int ll
int n,m;
int a[limit];
int mp[505][505];
int ans[505][505];
void solve(){
cin>>n>>m;
rep(i,1,n){
rep(j,1,m){
cin>>mp[i][j];
}
}
int flag = 0;
auto isp = [&](ll x)->bool{
//判断是否是平方
ll l = 1,r = 1e6;
while(l <= r){
ll mid = l + (r - l) / 2;
if(mid * mid == x)return true;
else if(mid * mid < x)l = mid + 1;
else r = mid - 1;
}
return false;
};
auto check = [&](ll x)->bool{
return isp(x) and isp(sqrtl(x));
};
auto legal = [&](ll x,ll y)->bool{
return x >= 1 and x <= n and y >= 1 and y <= m;
};
int lc = mp[1][1];
rep(i, 1, n){
rep(j, 1, m){
lc = lcm(lc, mp[i][j]);
}
}
// ans[1][1] = lc;
rep(i,1,n){
rep(j,1,m){
if((i + j) bitand 1){
ll p = 1;
rep(k, 1, 4){
p = p * mp[i][j];
}
ans[i][j] = lc + p;
}
else{
ans[i][j] = lc;
}
}
}
rep(i,1,n){
rep(j,1,m){
cout<<ans[i][j]<<" ";
}
cout<<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\n";
return 0;
}
Two Merged Sequences
这道题是给一个序列,那么我们能不能产生一种划分,使得序列的子序列分别组成一个严格递增和严格递减的子序列。
然后这个题,首先lis肯定是行不通的,因为你如果要lis,那么就没办法确定从哪个位置开始,pass。
然后考虑网络流,网络流可以记录路径,但是这图看上去不像是二分图。
这道题好像在给王大佬上分的那场见过类似的,有点像这道题.
然后整体的思路是这样,我们从分类讨论,维护严格递增序列p和严格递减序列q,当当前a[i]可以填入任意一个序列的时候,我们贪心地判断a[i] 和a[i + 1]哪个大,如果是当前大,那么我们显然需要将这一位填入p,反之亦然。
然后对于n我们检查一下是否可以填入任意一个队列,如果不行的话,就no,过程中如果遇到不合法的情况直接no,其他是yes
AC Code
#include <bits/stdc++.h>
using namespace std;
constexpr int limit = (2e6 + 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,m;
int a[limit];
void solve(){
cin>>n;
rep(i,1,n){
cin>>a[i];
}
deque<int>p, q;
p.push_back(0);
q.push_back(n + 1);
a[0] = numeric_limits<int>::min();
a[n + 1] = numeric_limits<int>::max();
rep(i, 1, n - 1){
if(a[i] > a[p.back()] and a[i] < a[q.back()]){
(a[i] < a[i + 1] ? p : q).push_back(i);
}else if(a[i] > a[p.back()] ){
p.push_back(i);
}else if(a[i] < a[q.back()]){
q.push_back(i);
}else{
cout<<"NO"<<endl;
return;
}
}
deque<int>null;
(p.empty() ? p : q.empty() ? q : a[n] > a[p.back()] ? p : a[n] < a[q.back()] ? q : null).push_back(n);
if(null.size()){
cout<<"NO"<<endl;
return;
}
cout<<"YES"<<endl;
vector<int>ans(n + 1, 0);
for(auto it : q | views::drop(1)){
ans[it] = 1;
}
for(auto && x : ans | views::drop(1))cout<<x<<" ";
cout<<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\n";
return 0;
}
晚上整一下OS作业,看看那个random file access