C - Multiple Gift

题解

首项是X,每次乘个2,暴力统计

代码

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define pdi pair<db,int>
#define mp make_pair
#define pb push_back
#define enter putchar('\n')
#define space putchar(' ')
#define eps 1e-8
#define mo 974711
#define MAXN 2005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
res = 0;char c = getchar();T f = 1;
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int64 X,Y; int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
read(X);read(Y);
int l = 1;
while(X <= Y / 2) {++l;X *= 2;}
out(l);enter;
}

D - Wide Flip

题解

从大到小枚举K(也可以二分)

如果\(2K <= N\),此时\(K\)一定合法

否则看前\(K\)个和后\(K\)个交出来的中间一段,这段的前面和后面1和0是可以单个位置随便改的(用一次K + 1的区间再用一次K的区间)

只要看中间一段是不是全1或者全0即可

代码

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define pdi pair<db,int>
#define mp make_pair
#define pb push_back
#define enter putchar('\n')
#define space putchar(' ')
#define eps 1e-8
#define mo 974711
#define MAXN 2005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
res = 0;char c = getchar();T f = 1;
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
char s[1000005];
int sum[100005],N;
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
scanf("%s",s + 1);
N = strlen(s + 1);
for(int i = 1 ; i <= N ; ++i) {
sum[i] = sum[i - 1];
if(s[i] == '1') sum[i]++;
}
for(int K = N ; K >= 1 ; --K) {
int r = K;
int l = N - K + 1;
if(l > r) {
out(K);enter;return 0;
}
if(sum[r] - sum[l - 1] == 0 || sum[r] - sum[l - 1] == r - l + 1) {
out(K);enter;return 0;
}
}
}

E - Papple Sort

题解

只要每次把靠边的一个字符的匹配点找出来同样后移然后一起删除即可

就是讨论一下

...A...A...B...B..

A要和这个一对B交换两次

B如果要过去也要换两次,所以都一样

A..B..A..B

A要和B换一次,如果B到外面也是和A换一次,所以都一样

树状数组维护一下前缀有多少数即可

代码

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define pdi pair<db,int>
#define mp make_pair
#define pb push_back
#define enter putchar('\n')
#define space putchar(' ')
#define eps 1e-8
#define mo 974711
#define MAXN 200005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
res = 0;char c = getchar();T f = 1;
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
char s[MAXN];
int N;
vector<int> c[30];
bool vis[MAXN];
int tr[MAXN];
void Init() {
scanf("%s",s + 1);
N = strlen(s + 1);
}
int lowbit(int x) {return x & -x;}
void Insert(int x,int v) {
while(x <= N) {
tr[x] += v;
x += lowbit(x);
}
}
int Query(int x) {
int res = 0;
while(x > 0) {
res += tr[x];
x -= lowbit(x);
}
return res;
}
void Solve() {
int cnt = 0;
for(int i = N ; i >= 1 ; --i) {
c[s[i] - 'a'].pb(i);
}
for(int i = 0 ; i < 26 ; ++i) {
if(c[i].size() & 1) ++cnt;
}
if(cnt >= 2) {puts("-1");return;}
for(int i = 1 ; i <= N ; ++i) Insert(i,1);
int64 ans = 0;
int k = 0;
for(int i = N ; i >= 1 ; --i) {
if(vis[i]) continue;
int t = c[s[i] - 'a'].back();c[s[i] - 'a'].pop_back();
if(t == i){ ++k;continue;}
ans += Query(t - 1) + k;
vis[t] = 1;
Insert(t,-1);
}
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Init();
Solve();
}

F - Christmas Tree

题解

啊这个只要dp一下第一个值再二分。。。二分判的方法是……

woc这不NOIPD1T3啊怎么一模一样啊

代码

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define pdi pair<db,int>
#define mp make_pair
#define pb push_back
#define enter putchar('\n')
#define space putchar(' ')
#define eps 1e-8
#define mo 974711
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
res = 0;char c = getchar();T f = 1;
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N;
struct node {
int to,next;
}E[MAXN * 2];
int head[MAXN],sumE;
int dp[MAXN],A,lim,res;
int val[MAXN],tot;
bool vis[MAXN];
void add(int u,int v) {
E[++sumE].to = v;
E[sumE].next = head[u];
head[u] = sumE;
}
void Init() {
read(N);
int a,b;
for(int i = 1 ; i < N ; ++i) {read(a);read(b);add(a,b);add(b,a);} }
void dfs(int u,int fa) {
int son = 0;
for(int i = head[u] ; i ; i = E[i].next) {
int v = E[i].to;
if(v != fa) {
dfs(v,u);
++son;
dp[u] += dp[v];
}
}
dp[u] -= son / 2;
if(son % 2 == 0 && fa) dp[u]++;
}
struct BIT {
int tr[MAXN],s;
void clear() {
for(int i = 1 ; i <= s ; ++i) tr[i] = 0;
}
int lowbit(int x) {return x & -x;}
int query(int x) {
int res = 0;
while(x > 0) {
res += tr[x];
x -= lowbit(x);
}
return res;
}
void insert(int x,int v) {
while(x <= s) {
tr[x] += v;
x += lowbit(x);
}
}
int find_max() {
int L = 1,R = s;
int x = query(s);
if(x == 0) return 0;
while(L < R) {
int m = (L + R + 1) >> 1;
if(query(m - 1) != x) L = m;
else R = m - 1;
}
return L;
}
}BST;
void calc(int u,int fa) {
for(int i = head[u] ; i ; i = E[i].next) {
int v = E[i].to;
if(v != fa) {
calc(v,u);
}
}
tot = 0;
for(int i = head[u] ; i ; i = E[i].next) {
int v = E[i].to;
if(v != fa) {
val[++tot] = dp[v];
}
}
if(!tot) {dp[u] = 1;++res;return;}
BST.clear();
sort(val + 1,val + tot + 1);
for(int i = 1 ; i <= tot ; ++i) vis[i] = 1;
BST.s = tot;int p = 0;
for(int i = tot ; i >= 1 ; --i) {
if(!vis[i]) continue;
while(p < i && val[p + 1] + val[i] <= lim) {
++p;BST.insert(p,1);
}
if(p >= i) BST.insert(i,-1);
int t = BST.find_max();
if(t) {vis[t] = 0;vis[i] = 0;BST.insert(t,-1);--res;}
}
if(fa) {
dp[u] = 1;++res;
for(int i = 1 ; i <= tot ; ++i) {
if(vis[i]) {
if(val[i] + 1 > lim) break;
dp[u] += val[i];--res;
break;
}
}
}
}
bool check(int m) {
lim = m;res = 0;
calc(1,0);
return res <= A;
}
void Solve() {
dfs(1,0);
out(dp[1]);space;
A = dp[1];
int L = 1,R = N - 1;
while(L < R) {
int mid = (L + R) >> 1;
if(check(mid)) R = mid;
else L = mid + 1;
}
out(R);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Init();
Solve();
}

【AtCoder】ARC088的更多相关文章

  1. 【Atcoder】ARC088 D - Wide Flip

    [题目]D - Wide Flip [题意]给定n个数字的01序列,要求每次翻转>=k个数字使得全0,求最大的k.n<=10^5 [算法]数学 [题解]有两个角度可以得到等价的结论: 1. ...

  2. 【AtCoder】ARC092 D - Two Sequences

    [题目]AtCoder Regular Contest 092 D - Two Sequences [题意]给定n个数的数组A和数组B,求所有A[i]+B[j]的异或和(1<=i,j<=n ...

  3. 【Atcoder】CODE FESTIVAL 2017 qual A D - Four Coloring

    [题意]给定h,w,d,要求构造矩阵h*w满足任意两个曼哈顿距离为d的点都不同色,染四色. [算法]结论+矩阵变换 [题解] 曼哈顿距离是一个立着的正方形,不方便处理.d=|xi-xj|+|yi-yj ...

  4. 【AtCoder】ARC 081 E - Don't Be a Subsequence

    [题意]给定长度为n(<=2*10^5)的字符串,求最短的字典序最小的非子序列字符串. http://arc081.contest.atcoder.jp/tasks/arc081_c [算法]字 ...

  5. 【AtCoder】AGC022 F - Leftmost Ball 计数DP

    [题目]F - Leftmost Ball [题意]给定n种颜色的球各k个,每次以任意顺序排列所有球并将每种颜色最左端的球染成颜色0,求有多少种不同的颜色排列.n,k<=2000. [算法]计数 ...

  6. 【AtCoder】AGC005 F - Many Easy Problems 排列组合+NTT

    [题目]F - Many Easy Problems [题意]给定n个点的树,定义S为大小为k的点集,则f(S)为最小的包含点集S的连通块大小,求k=1~n时的所有点集f(S)的和取模92484403 ...

  7. 【AtCoder】ARC067 F - Yakiniku Restaurants 单调栈+矩阵差分

    [题目]F - Yakiniku Restaurants [题意]给定n和m,有n个饭店和m张票,给出Ai表示从饭店i到i+1的距离,给出矩阵B(i,j)表示在第i家饭店使用票j的收益,求任选起点和终 ...

  8. 【AtCoder】ARC095 E - Symmetric Grid 模拟

    [题目]E - Symmetric Grid [题意]给定n*m的小写字母矩阵,求是否能通过若干行互换和列互换使得矩阵中心对称.n,m<=12. [算法]模拟 [题解]首先行列操作独立,如果已确 ...

  9. 【Atcoder】AGC022 C - Remainder Game 搜索

    [题目]C - Remainder Game [题意]给定n个数字的序列A,每次可以选择一个数字k并选择一些数字对k取模,花费2^k的代价.要求最终变成序列B,求最小代价或无解.n<=50,0& ...

随机推荐

  1. 【刷题】LOJ 6011 「网络流 24 题」运输问题

    题目描述 W 公司有 \(m\) 个仓库和 \(n\) 个零售商店.第 \(i\) 个仓库有 \(a_i\) 个单位的货物:第 \(j\) 个零售商店需要 \(b_j\) 个单位的货物.货物供需平衡, ...

  2. Graham's Scan法求解凸包问题

    概念 凸包(Convex Hull)是一个计算几何(图形学)中的概念.用不严谨的话来讲,给定二维平面上的点集,凸包就是将最外层的点连接起来构成的凸多边型,它能包含点集中所有点的.严谨的定义和相关概念参 ...

  3. WEB入门之十二 jquery简介

    学习内容 jQuery简介 搭建jQuery开发环境 jQuery基本选择器 能力目标 熟悉jQuery开发环境 能编写简单的jQuery代码 本章简介 在前面两章,我们学习了JavaScript面向 ...

  4. 伸展树(Splay)复杂度证明

    本文用势能法证明\(Splay\)的均摊复杂度,对\(Splay\)的具体操作不进行讲述. 为了方便本文的描述,定义如下内容: 在文中我们用\(T\)表示一棵完整的\(Splay\),并(不严谨地)用 ...

  5. cookie的安全性问题

    HTTP协议: (1)请求组成部分: 请求行:(get或者post请求:请求路径(不包括主机) :http1.1) 请求头:请求头是浏览器交给服务器的一些信息(比较cookie啥的) 请求体:只有po ...

  6. python教程1:Python基础之数据类型和变量、字符串和编码

    视频链接:http://www.bilibili.com/video/av10730372/ 我是在Linux下玩python的,Linux下默认安装python,直接打个pyhon3就好了,pyth ...

  7. SpringBoot入门Demo(Hello Word Boot)

    Spring Boot 是由Pivotal团队提供的全新框架,其设计目的是用来简化新的Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置. ...

  8. shell 示例1 从1叠加到100

    从1叠加到100 echo $[$(echo +{..})] echo $[(+)*(/)] seq -s |bc

  9. 开放通用Api,总有你喜欢的

    接口文档 目录 通用 更新记录 接口列表 一.福彩-双色球接口 指定期号中奖号码 最新中奖号码信息 获取双色球中奖信息列表 二.节假日及万年历 指定日期的节假日及万年历信息 指定多个日期的节假日及万年 ...

  10. CF359B Permutation (构造)

    CF359B Permutation \(solution:\) 作为一道构造题,这题也十分符合构造的一些通性----(找到一些规律,然后无脑循环). 构造一个长度为 \(2n\) 的排列 \(a\) ...