Codeforces Global Round 1
1110D. Jongmah
大意: 给定$n$个数, (x,x,x)或者(x,x+1,x+2)可以组成三元组, 每个数最多用一次, 求最多组成多少三元组.
核心观察是以$x$开头的$(x,x+1,x+2)$最多出现两次, 然后暴力dp即可.
#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include <cmath> #include <set> #include <map> #include <queue> #include <string> #include <cstring> #include <bitset> #include <functional> #include <random> #define REP(i,a,n) for(int i=a;i<=n;++i) #define PER(i,a,n) for(int i=n;i>=a;--i) #define hr putchar(10) #define pb push_back #define lc (o<<1) #define rc (lc|1) #define mid ((l+r)>>1) #define ls lc,l,mid #define rs rc,mid+1,r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl '\n' #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;}) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int P = 1e9+7, INF = 0x3f3f3f3f; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;} //head #ifdef ONLINE_JUDGE const int N = 1e6+50; #else const int N = 1e2+10; #endif int n,m,a[N],dp[N][3][3]; void chkmax(int &a, int b) {a<b?a=b:0;} int main() { scanf("%d%d", &n, &m); REP(i,1,n) { int t; scanf("%d", &t); ++a[t]; } memset(dp,0xef,sizeof dp); dp[0][0][0] = 0; //f[i][x][y] = (i,i+1,i+2)使用y次, (i-1,i,i+1)使用x次的答案 REP(i,1,m) REP(x,0,2) REP(y,0,2) REP(z,0,2) { int &r = dp[i-1][x][y]; if (r<0||x+y+z>a[i]) continue; //x个(i-2,i-1,i),y个(i-1,i,i+1),z个(i,i+1,i+2) chkmax(dp[i][y][z],r+(a[i]-x-y-z)/3+z); } printf("%d\n",dp[m][0][0]); }
1110E. Magic Stones
大意: 给定序列$c,t$, 每次操作选择一个$i$, 将$c_i$变为$c_{i+1}+c_{i-1}-c_{i}$, 求判断$c$是否能变为$t$.
这种题一看就没什么思路, 然后开始打表找规律, 跑了一下发现长$n$的序列能得到$n!$种不同的序列, 似乎没什么用处. 搞了半个小时, 觉得似乎可以只判断第二个数的情况, 这样肯定很难卡掉. 交了一发就A了..... 具体证明的话, 考虑每次操作对差分序列的影响, 可以发现相当于是交换了差分序列的两个数, 而第一个数是不会变的, 所以只要差分序列相同即可, 也就等价于第二个数所有可能值相同.
#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include <cmath> #include <set> #include <map> #include <queue> #include <string> #include <cstring> #include <bitset> #include <functional> #include <random> #define REP(i,a,n) for(int i=a;i<=n;++i) #define PER(i,a,n) for(int i=n;i>=a;--i) #define hr putchar(10) #define pb push_back #define lc (o<<1) #define rc (lc|1) #define mid ((l+r)>>1) #define ls lc,l,mid #define rs rc,mid+1,r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl '\n' #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;}) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int P = 1e9+7, INF = 0x3f3f3f3f; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;} //head const int N = 200; typedef vector<int> node[N]; int n; node f; vector<int> add(vector<int> a, vector<int> b) { REP(i,1,n) a[i]+=b[i]; return a; } vector<int> sub(vector<int> a, vector<int> b) { REP(i,1,n) a[i]-=b[i]; return a; } typedef vector<vector<int> > vv; set<vv> s; void dfs(node x) { vv t(n+1); REP(i,1,n) t[i] = x[i]; if (s.count(t)) return; s.insert(t); REP(i,2,n-1) { auto t = x[i]; x[i] = sub(add(x[i-1],x[i+1]),x[i]); dfs(x); x[i] = t; } } int main() { scanf("%d",&n); REP(i,1,n) f[i].resize(n+1); REP(i,1,n) f[i][i] = 1; dfs(f); printf("tot=%d\n",(int)s.size()); int p=0; set<vector<int> > qq; for(auto &e:s) { qq.insert(e[2]); // REP(i,1,n) { // REP(j,1,n) cout<<e[i][j]<<' ';hr; // }hr; } for(auto &e:qq) { REP(i,1,n) cout<<e[i]<<' ';hr; hr; } }
#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include <cmath> #include <set> #include <map> #include <queue> #include <string> #include <cstring> #include <bitset> #include <functional> #include <random> #define REP(i,a,n) for(int i=a;i<=n;++i) #define PER(i,a,n) for(int i=n;i>=a;--i) #define hr putchar(10) #define pb push_back #define lc (o<<1) #define rc (lc|1) #define mid ((l+r)>>1) #define ls lc,l,mid #define rs rc,mid+1,r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl '\n' #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;}) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int P = 1e9+7, INF = 0x3f3f3f3f; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;} //head const int N = 1e6+50; int n; ll c[N],t[N]; int main() { scanf("%d", &n); REP(i,1,n) scanf("%lld",c+i); REP(i,1,n) scanf("%lld",t+i); if (c[1]!=t[1]||c[n]!=t[n]) return puts("No"),0; //求出c[2]的所有可能值 multiset<ll> L; L.insert(c[2]); REP(i,2,n-1) L.insert(c[1]-c[i]+c[i+1]); multiset<ll> R; R.insert(t[2]); REP(i,2,n-1) R.insert(t[1]-t[i]+t[i+1]); puts(L==R?"Yes":"No"); }
1110F. Nearest Leaf
大意: 给定一棵树, 节点编号与$dfs$序一致, $q$个询问, 每次询问给出$(v,l,r)$, 求区间$[l,r]$内的所有叶子到$v$的最短距离.
询问离线, 然后$dfs$一遍, 线段树节点$x$维护编号为$x$的叶子到当前遍历到的点的距离, 每次移动相当于是子树加减.