A题:Free Ice Cream

注意要使用LL,避免爆int

#include <bits/stdc++.h>
#define scan(x,y) scanf("%d%d",&x,&y)
using namespace std;
typedef long long LL;
const int Max=1e5+;
int main()
{
int n,has,x;
while(~scan(n,has))
{
LL have=has;
char op;LL dis=;
for(int i=;i<n;i++)
{
cin>>op>>x;
if(op=='+')
{
have+=x;
}
else
{
if(have>=x) have-=x;
else dis++;
}
}
printf("%I64d %I64d\n",have,dis);
}
}

B题:Little Robber Girl's Zoo
不要想得太复杂,直接暴力就好

#include <bits/stdc++.h>
#define scan(x,y) scanf("%d%d",&x,&y)
using namespace std;
typedef long long LL;
const int Max=1e3+;
int a[Max][Max],L[Max],R[Max],best[Max],l,r;
int g=,n;
bool check()
{
int i=,cnt=,cunt=;
memset(L,,sizeof(L));memset(R,,sizeof(R));
while(i<n)
{
if(best[i]>best[i+])
{
cnt++;
swap(best[i],best[i+]);
if(cnt==) {L[cunt]=i;R[cunt++]=i+;}
else
{
if(i-==R[cunt-]) R[cunt-]=i+;
else {L[cunt]=i;R[cunt++]=i+;}
}
i+=;
}
else
{
i++;
}
}
if(cnt==) return ;
for(int i=;i<cunt;i++) printf("%d %d\n",L[i],R[i]);
return ;
}
int main()
{
while(~scanf("%d",&n))
{
g=;
for(int i=;i<=n;i++) scanf("%d",&best[i]);
memset(a,,sizeof(a));
while(!check());
}
return ;
}

C题:Robbers' watch
观察可知,由于时间表是7进制的,所以整个可行的序列长不会超过7位,因此可以套用数位dp的模式进行dfs搜索

#include <bits/stdc++.h>
using namespace std;
int ans,n,m;
int vis[];
int bit[][];
int pos[];
stack<int>st1,st;
void dfs2(int len,int flag,int id)
{
if(len==)
{
ans++;
return;
}
int end=flag?bit[id][len]:;
for(int i=;i<=end;i++)
{
if(!vis[i])
{
vis[i]=;
dfs2(len-,i==end&&flag,id);
vis[i]=;
}
}
}
void dfs(int len,int flag,int id)
{
if(len==)
{
if(id==) dfs2(pos[],,);
return;
}
int end=flag?bit[id][len]:;
for(int i=;i<=end;i++)
{
if(!vis[i])
{
vis[i]=;
dfs(len-,i==end&&flag,id);
vis[i]=;
}
}
}
int cacu(int x,int y)
{
pos[]=;
bit[][]=bit[][]=;
if(x<) bit[][++pos[]]=x;
else
{
while(x)
{
bit[][++pos[]]=x%;
x/=;
}
}
pos[]=;
if(y<) bit[][++pos[]]=y;
else
{
while(y)
{
bit[][++pos[]]=y%;
y/=;
}
}
if(pos[]+pos[]>) return ;
else dfs(pos[],,);
return ;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
ans=;
n--;m--;
printf("%d\n",cacu(n,m)==?:ans);
}
return ;
}

D题:Kay and Snowflake
1.重心的另一个定义:以这个点为根,那么所有的子树(不算整个树自身)
的大小都不超过整个树大小的一半。(这个对解这道题很有用)
题解思路:
对于以u为根节点的子树,重心肯定在它的重儿子之中,但是有一个范围
可以想到重心的下区间是重儿子的重心,上区间就是u自己
可以对于这个范围内的节点,利用性质1逐个检测。

#include <bits/stdc++.h>
using namespace std;
const int Max=3e5+;
const int E=Max*;
int son[Max],maxson[Max],fa[Max],zx[Max];
int head[Max],nex[E],pnt[E],edge;
void Init()
{
edge=;
memset(son,,sizeof(son));
memset(maxson,,sizeof(maxson));
memset(fa,-,sizeof(fa));
memset(head,-,sizeof(head));
}
void Addedge(int u,int v)
{
pnt[edge]=v;
nex[edge]=head[u];
head[u]=edge++;
}
void dfs1(int u)
{
son[u]=;
maxson[u]=;
int v;
for(int e=head[u];e!=-;e=nex[e])
{
v=pnt[e];
dfs1(v);
son[u]+=son[v];
maxson[u]=max(maxson[u],son[v]);
}
}
bool is_ok(int u,int v)
{
if(son[u]>=maxson[v]*&&son[u]>=(son[u]-son[v])*) return ;
//利用定义4,检测是否是正确的重心,存在u自身的情况
//所以不能son[u]>=son[v]*2;
return ;
}
void dfs2(int u)
{
if(son[u]==)
{
zx[u]=u;
return;
}
int v,z=u;
for(int e=head[u];e!=-;e=nex[e])
{
v=pnt[e];
dfs2(v);
if(maxson[u]==son[v])
{
z=zx[v];
}
}
while(!is_ok(u,z)&&fa[z]!=-)
{
z=fa[z];
}
zx[u]=z;
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
Init();
int u,v;
for(int i=;i<=n;i++)
{
scanf("%d",&u);
fa[i]=u;
Addedge(u,i);
}
dfs1();
dfs2();
while(m--)
{
scanf("%d",&u);
printf("%d\n",zx[u]);
}
}
return ;
}

E题:Optimal Point
题解链接:http://codeforces.com/blog/entry/45558

#include <bits/stdc++.h>
using namespace std;
#define MP(x,y) make_pair(x,y);
typedef long long LL;
const int Max=1e5+;
const LL inf=*(LL)1e9*(LL)1e9+;
//(1e18不能直接写,会爆int)
const LL LM=(LL()<<)*(LL()<<)*(LL()<<)-;
const LL LMM=-(LL()<<)*(LL()<<)*(LL()<<)+;
LL max(LL x,LL y){return x>=y?x:y;}
LL min(LL x,LL y){return x<=y?x:y;}
int n;
struct coord{
LL x,y,z;
coord(){};
coord(LL xx,LL yy,LL zz):x(xx),y(yy),z(zz){}
}node[Max];
struct equa{
pair<LL,LL> s;
pair<LL,LL> a;
pair<LL,LL> b;
pair<LL,LL> c;
equa operator + (const equa e)
{
equa ans;
ans.s=MP(max(s.first,e.s.first),min(s.second,e.s.second));
ans.a=MP(max(a.first,e.a.first),min(a.second,e.a.second));
ans.b=MP(max(b.first,e.b.first),min(b.second,e.b.second));
ans.c=MP(max(c.first,e.c.first),min(c.second,e.c.second));
return ans;
}
};
void Acc()
{
std::iostream::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
}
LL Getmid(LL x)
{
return (x-(x&))/;
} struct coord Getsolu(const equa &x)
{
if(x.s.first>x.s.second||
x.a.first>x.a.second||
x.b.first>x.b.second||
x.c.first>x.c.second) return coord(LM,LM,LM);
if(x.a.first+x.b.first+x.c.first>x.s.second||
x.a.second+x.b.second+x.c.second<x.s.first)
return coord(LM,LM,LM); coord res;
res.x=x.a.first;
res.y=x.b.first;
res.z=x.c.first;
LL delta=max(LL(),x.s.first-res.x-res.y-res.z);
res.x+=min(delta,x.a.second-x.a.first);
delta-=min(delta,x.a.second-x.a.first);
res.y+=min(delta,x.b.second-x.b.first);
delta-=min(delta,x.b.second-x.b.first);
res.z+=min(delta,x.c.second-x.c.first);
delta-=min(delta,x.c.second-x.c.first);
assert(delta==);
return res;
}
struct coord Cacu(LL Maxlen)
{
//设Maxlen为最大距离
equa eq;
eq.s=eq.a=eq.b=eq.c=MP(LMM,LM);
for(int i=;i<=n;i++)
{
equa ne;
// -Maxlen<=A+B+C<=Maxlen
// -Maxlen<=A<=Maxlen
// -Maxlen<=B<=Maxlen
// -Maxlen<=C<=Maxlen
ne.s=MP(node[i].x+node[i].y+node[i].z-Maxlen,
node[i].x+node[i].y+node[i].z+Maxlen);
ne.a=MP(-node[i].x+node[i].y+node[i].z-Maxlen,
-node[i].x+node[i].y+node[i].z+Maxlen);
ne.b=MP(node[i].x-node[i].y+node[i].z-Maxlen,
node[i].x-node[i].y+node[i].z+Maxlen);
ne.c=MP(node[i].x+node[i].y-node[i].z-Maxlen,
node[i].x+node[i].y-node[i].z+Maxlen);
eq=eq+ne; //取最大距离
}
for(LL r=;r<=;r++)
{
//对二取余,可能余1或者余0
equa tr=eq;
tr.s.first=Getmid(tr.s.first-(*r-));
tr.a.first=Getmid(tr.a.first-(r-));
tr.b.first=Getmid(tr.b.first-(r-));
tr.c.first=Getmid(tr.c.first-(r-)); tr.s.second=Getmid(tr.s.second-(*r));
tr.a.second=Getmid(tr.a.second-(r));
tr.b.second=Getmid(tr.b.second-(r));
tr.c.second=Getmid(tr.c.second-(r)); coord solu=Getsolu(tr);
if(solu.x!=LM)
{
coord ans;
//(A,B奇偶性相同才会有整数解)
ans.x=r+solu.y+solu.z;
ans.y=r+solu.x+solu.z;
ans.z=r+solu.x+solu.y;
return ans;
}
}
return coord(LM,LM,LM); //有超过Maxlen的距离或者无整数解
}
int main()
{
Acc();
int T;
for(cin>>T;T;T--)
{
cin>>n;
for(int i=;i<=n;i++)
{
cin>>node[i].x>>node[i].y>>node[i].z;
}
LL l=,r=LM,answ=;
while(l<=r)
{
LL mid=l+(r-l)/;
if(Cacu(mid).x!=LM) r=mid-,answ=mid;
else l=mid+;
}
coord ans=Cacu(answ);
cout<<ans.x<<" "<<ans.y<<" "<<ans.z<<endl;
}
return ;
}

套题 codeforces 359的更多相关文章

  1. 套题 codeforces 361

    A题((Mike and Cellphone) 看起来好像需要模拟数字键位的运动,可是,只要判断出那些必然YES的数字组合不就好了么 #include <cstdio> #include ...

  2. 套题 codeforces 360

    A题:Opponents 直接模拟 #include <bits/stdc++.h> using namespace std; ]; int main() { int n,k; while ...

  3. 套题 Codeforces Round #277 (Div. 2)

    A. Calculating Function 水题,分奇数偶数处理一下就好了 #include<stdio.h> #include<iostream> using names ...

  4. Educational Codeforces Round 15 套题

    这套题最后一题不会,然后先放一下,最后一题应该是大数据结构题 A:求连续最长严格递增的的串,O(n)简单dp #include <cstdio> #include <cstdlib& ...

  5. 第46套题【STL】【贪心】【递推】【BFS 图】

    已经有四套题没有写博客了.今天改的比较快,就有时间写.今天这套题是用的图片的形式,传上来不好看,就自己描述吧. 第一题:单词分类 题目大意:有n个单词(n<=10000),如果两个单词中每个字母 ...

  6. 【套题】qbxt国庆刷题班D1

    Day1 事实上D1的题目还是比较简单的= =然而D1T2爆炸了就十分尴尬--错失一波键盘 看题 T1 传送门 Description 现在你手里有一个计算器,上面显示了一个数\(S\),这个计算器十 ...

  7. Moscow Pre-Finals Workshop 2016. Japanese School OI Team Selection. 套题详细解题报告

    写在前面 谨以此篇题解致敬出题人! 真的期盼国内也能多出现一些这样质量的比赛啊.9道题中,没有一道凑数的题目,更没有码农题,任何一题拿出来都是为数不多的好题.可以说是这一年打过的题目质量最棒的五场比赛 ...

  8. Codeforces Round #579 (Div. 3) 套题 题解

    A. Circle of Students      题目:https://codeforces.com/contest/1203/problem/A 题意:一堆人坐成一个环,问能否按逆时针或者顺时针 ...

  9. Codeforces Round #361 (Div. 2) 套题

    A - Mike and Cellphone 问有没有多解,每个点按照给出的序列用向量法跑一遍 #include<cstdio> #include<cstring> #incl ...

随机推荐

  1. spring mvc定时任务的简单使用

    版权声明:本文为楼主原创文章,未经楼主允许不得转载,如要转载请注明来源. 说起定时任务,开发的小伙伴们肯定不陌生了.有些事总是需要计算机去完成的,而不是傻傻的靠我们自己去.可是好多人对定时器总感觉很陌 ...

  2. sscanf函数和正则表达式

    看了几篇介绍sscanf函数,真是发现自己好多东西没理解透,详细介绍使用在sscanf中使用正则表达式. 第一篇: 此文所有的实验都是基于下面的程序: char str[10]; for (int i ...

  3. html text加提示语

    <input type="text" id="key" name="key" value=" 请输入关键词" on ...

  4. MySQL_关于用嵌套表计算的可以不用 20161205

    计算求和类的指标,其实用不到嵌套表,比如计算各城市产品分类的订单额. 如果要计算不重复的指标 比如一个用户一天下了多个订单 用这样的表计算一天有多少用户下单 这个用户肯定是去重的 下多个订单也应该视为 ...

  5. 三级联动---DropDownList控件

    AutoPostBack属性:意思是自动回传,也就是说此控件值更改后是否和服务器进行交互比如Dropdownlist控件,若设置为True,则你更换下拉列表值时会刷新页面(如果是网页的话),设置为fl ...

  6. 关于sql注入

    删除表,先猜表名,可以使用下面的语名: Select * from A where A.a = ‘testdata’; drop table A---’; If a field only allow ...

  7. Asp.Net 获取FileUpload控件的文件路径、文件名、扩展名

    string fileNameNo = Path.GetFileName(FileUploadImg.PostedFile.FileName); //获取文件名和扩展名string Directory ...

  8. 编程实践中C语言的一些常见细节

    对于C语言,不同的编译器采用了不同的实现,并且在不同平台上表现也不同.脱离具体环境探讨C的细节行为是没有意义的,以下是我所使用的环境,大部分内容都经过测试,且所有测试结果基于这个环境获得,为简化起见, ...

  9. BootLoader 详解(1)

    1. Boot Loader的概念 BootLoader就是在操作系统内核运行前之前运行的一段小程序.通过这段小程序,可以初始化硬件设备.建立内存空间映射图,从而将系统的软硬件带到一个合适的状态,以便 ...

  10. System.IO中的File、FileInfo、Directory与DirectoryInfo类(实例讲解)

    一.建立的文件夹(对这些文件进行以上四个类的操作): 父目录: 父目录的子目录以及父目录下的文件: 子目录下的文件: 二.效果图 三.代码实现 using System; using System.I ...