[ARC 066] Tutorial
Link:
C:
如果存在可行方案则答案为$2^{n/2}$
#include <bits/stdc++.h> using namespace std;
#define X first
#define Y second
typedef long long ll;
typedef pair<int,int> P;
const int MAXN=1e5+,MOD=1e9+;
int n,x,res[MAXN]; int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&x);
int rk=(n-+x)/;
if(!res[rk+]) res[rk+]=i;
else if(!res[n-rk]) res[n-rk]=i;
else return puts(""),;
}
int res=;
for(int i=;i<=n/;i++) (res*=)%=MOD;
printf("%d",res);
return ;
}
Problem C
D:
挺不错的一道数位$dp$
由于无法直接计算$sum$和$xor$的对数,因此考虑枚举$a,b$,而将$sum,xor\le n$作为限制条件
又因为公式:$a+b=aXORb+2*(a\&b)$,所以$a+b\le aXORb$,只考虑$a+b$的限制即可
此时问题转化为对于每个$sum\le n$求$xor$的取值个数
这样就可以用$dp[i][s]$表示前$i$位确定,$a+b$的和为$s$的个数,每次分$a,b$在该位上总共有几个1转移
(按每位1的个数转移才不会考虑异或与和同时相同的情况!)
但这样复杂度是不对的,在枚举$s$上明显花费了不必要的时间
根据一般数位$dp$记录上限的思想,如果前$i$位的$n-s\ge 2$,这些数以后都保证合法,就能统一计算了
这样就从$dp[i][s]$变成了$dp[i][0/1/2]$
#include <bits/stdc++.h> using namespace std;
#define X first
#define Y second
typedef long long ll;
typedef pair<int,int> P;
#define MAX_D 64
#define MOD ((ll)1e9 + 7)
ll N,dp[MAX_D][],res;int nxt; int main()
{
scanf("%lld",&N);
dp[MAX_D-][]=;
for(int i=MAX_D-;i>;i--)
for(int j=; j <= ; j++)
for(int k=;k<=;k++)
{
nxt=j*+((N>>(i-))&)-k;
if (nxt<) continue;
nxt=nxt>?:nxt;
(dp[i-][nxt]+=dp[i][j])%=MOD;
}
res=;
for (int i=;i<=;i++) (res+=dp[][i])%=MOD;
printf("%lld\n", res);
return ;
}
Solution A
从后往前用记忆化搜索的形式写起来更加方便
一开始将上限值就设为$n$,每次确定最后一位取几个以后去掉最后一位,不用考虑和的合法性了
#include <bits/stdc++.h> using namespace std;
#define X first
#define Y second
typedef long long ll;
typedef pair<int,int> P;
const int MOD=1e9+;
map<ll,ll> dp;ll n; ll dfs(ll x)
{
if(dp.count(x)) return dp[x];
return dp[x]=(dfs(x>>)+dfs((x-)>>)+dfs((x-)>>))%MOD;
} int main()
{
scanf("%lld",&n);
dp[]=;dp[]=;
printf("%lld",dfs(n));
return ;
}
Solution B
E:
首先要观察出几点性质:
1、只有在减号后可能加括号
2、括号不可能嵌套超过两层,否则可以转化为只有两层的简化情况
这样就可以记录$dp[0/1/2]$分别表示当前还有几个左括号未匹配的最大值来$dp$了
#include <bits/stdc++.h> using namespace std;
typedef long long ll;
int n,x;char op;ll dp[],nxt[]; int main()
{
scanf("%d%d",&n,&dp[]);
dp[]=dp[]=-1ll<<;
for(int i=;i<n;i++)
{
scanf(" %c%d",&op,&x);
if(op=='-') x=-x;
nxt[]=dp[]+x,nxt[]=dp[]-x,nxt[]=dp[]+x;
dp[]=max(nxt[],max(nxt[],nxt[]));
if(op=='+') dp[]=max(nxt[],nxt[]),dp[]=nxt[];
else dp[]=dp[],dp[]=max(nxt[],nxt[]);
}
printf("%lld",dp[]);
return ;
}
Solution A
其实也可以不用$dp$,考虑如果在某个减号后加了第一个括号的最优解
发现此时能保证将下一个减号后的值都变为正贡献,但对当前位到下一个减号间的值是无能为力的
这样枚举第一个括号的位置对答案更新即可
#include <bits/stdc++.h> using namespace std;
#define X first
#define Y second
typedef long long ll;
typedef pair<int,int> P;
const int MAXN=1e5+;
char op[MAXN];
int n,dat[MAXN],nxt[MAXN],fst;
ll suf[MAXN],cur,res=-1ll<<; int main()
{
scanf("%d%d",&n,&fst);
for(int i=;i<n;i++)
scanf(" %c%d",&op[i],&dat[i]);
cur=n;
for(int i=n-;i;i--)
{
suf[i]=suf[i+]+dat[i];
if(op[i]=='-') nxt[i]=cur,cur=i;
}
cur=;
for(int i=;i<n;i++)
if(op[i]=='-')
res=max(res,cur-suf[i]+*suf[nxt[i]]),cur-=dat[i];
else cur+=dat[i];
res=max(res,cur);
printf("%lld",res+fst);
return ;
}
Solution B
F:
[ARC 066] Tutorial的更多相关文章
- AtCoder Regular Contest
一句话题解 因为上篇AGC的写的有点长……估计这篇也短不了所以放个一句话题解方便查阅啥的吧QwQ 具体的题意代码题解还是往下翻…… ARC 058 D:简单容斥计数. E:用二进制表示放的数字,然后状 ...
- HTML5 Canvas Arc Tutorial
HTML5 Canvas Arc Tutorial HTML5 Canvas Arc Tutorial
- [Hive - Tutorial] Built In Operators and Functions 内置操作符与内置函数
Built-in Operators Relational Operators The following operators compare the passed operands and gene ...
- 【转】Enable ARC in a Cocos2D Project: The Step-by-Step-How-To-Guide Woof-Woof!
On April 5, 2012, in idevblogaday, by Steffen Itterheim http://www.learn-cocos2d.com/2012/04/enablin ...
- GDI+ Tutorial for Beginners
原文 GDI+ Tutorial for Beginners GDI+ is next evolution of GDI. Using GDI objects in earlier versions ...
- MapServer Tutorial——MapServer7.2.1教程学习——第一节用例实践:Example1.7 Adding a wms layer
MapServer Tutorial——MapServer7.2.1教程学习——第一节用例实践:Example1.7 Adding a wms layer 前言 Add OGC WMS Layers( ...
- MapServer Tutorial——MapServer7.2.1教程学习——第一节用例实践:Example1.6 Defining Projections and Extents
MapServer Tutorial——MapServer7.2.1教程学习——第一节用例实践:Example1.6 Defining Projections and Extents 一.前言 当在m ...
- MapServer Tutorial——MapServer7.2.1教程学习——第一节用例实践:Example1.5 Adding a raster layer
MapServer Tutorial——MapServer7.2.1教程学习——第一节用例实践:Example1.5 Adding a raster layer 一.前言 MapServer不仅支持 ...
- Instruments Tutorial for iOS: How To Debug Memory Leaks【转】
If you're new here, you may want to subscribe to my RSS feed or follow me on Twitter. Thanks for vis ...
随机推荐
- bzoj 2786 DP
我们可以将=左右的两个数看成一个块,块内无顺序要求,把<分隔的看成两个块,那么我们设w[i][j]代表将i个元素分成j个块的方案数,那么显然w[i][j]=w[i-1][j]*j+w[i-1][ ...
- 解决ie9以下下不支持html5和媒体查询(Media Queries)
ie9以下不支持媒体查询和html5,可以使用补丁完美兼容 1.html5shiv ie6~8不识别html5的新元素,可以通过使用html5shiv来解决 <!--[if lt IE 9]&g ...
- mysql之基本数据库操作(二)
环境信息 数据库:mysql-5.7.20 操作系统:Ubuntu-16.04.3 mysql的启动.退出.重启 # 启动 $ sudo service mysqld start # 停止 $ sud ...
- Linux System.map文件【转】
转自:http://blog.csdn.net/ysbj123/article/details/51233618 当运行GNU链接器gld(ld)时若使用了"-M"选项,或者使用n ...
- x64dbg
https://x64dbg.com/ https://github.com/x64dbg/x64dbg https://sourceforge.net/projects/x64dbg/files/s ...
- sicily 1172. Queens, Knights and Pawns
Description You all are familiar with the famous 8-queens problem which asks you to place 8 queens o ...
- 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6152 Friend-Graph 暴暴暴暴力
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6152 题意:判定一个无向图是否有三个点的团或者三个点的独立集. 解法:Ramsey theorem,n ...
- FineReport——弹出新窗体选值并回调
主要实现的功能: 在主页面,通过单击按钮,弹出窗体,在窗体中通过下拉框选择值并查询,如果是多值,可以通过复选框选择,点击确定,将选中的行的字段值传递给主页面的下拉复选框,定义其编辑后事件进行查询.将想 ...
- leetcode 之Remove Duplicates from Sorted List(17)
很简单的一题,需要注意的是如果某结点重复了记得将其删除. ListNode *deleteDuplicates(ListNode *head) { if (head == nullptr) retur ...
- MiCode 40: 找小“3”
题目链接 这道题真的是zjb恶心, 看其起来像是个数位dp, 然而我并不会数位dp.然后就xjb乱写了个雷类似于动态规划的玩意, 然后调出了\(9\times 9 = 81\)种Bug, 终于过了. ...