Codeforces Round #398 (Div. 2)

A.Snacktower

模拟

我和官方题解的命名神相似...$has$

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
const int N=1e5+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,has[N];
int main(){
//freopen("in","r",stdin);
n=read();
int now=n;
for(int i=;i<=n;i++){
has[read()]=;
while(has[now]) printf("%d ",now),now--;
puts("");
}
}

B.The Queue

比赛时花了1h然后弃疗

感觉好恶心...


C.Garland

题意:一颗二叉树切断两条边分成三部分使得三部分权值和相等

感觉比B简单(好写)多了,可以发现每部分权值和就是总权值的$\frac{1}{3}$

然后我用了两个树形DP,$d[i]$表示$i$子树权值和,$f[i]$表示$i$子树里有几个权值和等于$\frac{1}{3}$的

分类讨论选的两棵子树是否互相包含,比赛时各种$debug$,最后$5min$通过了...感动

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
const int N=1e6+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,d[N],fa,w[N],root;
struct edge{
int v,ne;
}e[N<<];
int h[N],cnt=;
inline void ins(int u,int v){
cnt++;
e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;
}
void dp(int u){
d[u]=w[u];
for(int i=h[u];i;i=e[i].ne)
dp(e[i].v),d[u]+=d[e[i].v];
}
int f[N],val;
void dp2(int u){
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
dp2(v);
if(f[v]==) f[u]+=d[v]==val;
else f[u]+=f[v];
}
}
int a[N],p;
int main(){
//freopen("in","r",stdin);
n=read();
for(int i=;i<=n;i++){
fa=read();w[i]=read();
if(fa==) root=i;
else ins(fa,i);
}
dp(root);
if(d[root]%!=) puts("-1");
else{
val=d[root]/;
dp2(root);
//for(int i=1;i<=n;i++) printf("%d %d %d\n",i,d[i],f[i]);
if(f[root]==) puts("-1");
else if(f[root]>=){
for(int i=;i<=n;i++) if(i!=root&&d[i]==val&&f[i]==) a[++p]=i;
printf("%d %d",a[],a[]);
}else{
int flag=,a,b;
for(int i=;i<=n;i++) if(i!=root&&d[root]-d[i]==val&&f[i]==){
flag=;a=i;break;
}
for(int i=;i<=n;i++) if(i!=root&&d[i]==val&&f[i]==) {b=i;break;}
if(flag) printf("%d %d",a,b);
else puts("-1");
}
}
}

D.Cartons of milk

赛后补题.....

一开始贪心乱搞失败,然后发现直接二分答案就好了.....然而是$O(n log^{2}n)$.....

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N=2e6+;
inline int read(){
char c=getchar();int x=;
while(c<''||c>''){c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x;
}
int n,m,k,a[N];
struct data{
int id,b;
bool operator <(const data &r) const{
return b>r.b;
}
}b[N];
int p,c[N];
bool check(int x){//printf("check %x\n",x);
ll now=;
p=;
for(int i=;i<=n;i++) c[++p]=a[i];
for(int i=;i<=x;i++) c[++p]=b[i].b;
sort(c+,c++p);
//for(int i=1;i<=p;i++) printf("%d ",c[i]);puts("");
for(int i=;i<=p;i++){
now++;//printf("now %d %d %d\n",i,c[i],now);
if(now>(ll)k*c[i]) return false;
}
return true;
}
int main(){
//freopen("in","r",stdin);
n=read();m=read();k=read();
for(int i=;i<=n;i++) a[i]=read()+;
for(int i=;i<=m;i++) b[i].b=read()+,b[i].id=i;
sort(a+,a++n);
sort(b+,b++m);
int l=,r=m,ans=-;
while(l<=r){
int mid=(l+r)>>;
if(check(mid)) ans=mid,l=mid+;
else r=mid-;
}
printf("%d\n",ans);
if(ans!=-){
memset(c,,sizeof(c));
for(int i=;i<=ans;i++) c[b[i].id]=;
for(int i=;i<=m;i++) if(c[i]) printf("%d ",i);
}
}

看了一个别人的代码发现贪心也能做并且很快

$f[i]$记录时间i选了几个,贪心选择放在最晚到期,然后从后往前$>k$的给前面就行了,$O(n)$,时间只有二分答案的$\frac{1}{3}$

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N=2e6+,M=1e7+;
inline int read(){
char c=getchar();int x=;
while(c<''||c>''){c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x;
}
int n,m,k,a[N];
struct data{
int id,b;
bool operator <(const data &r) const{
return b>r.b;
}
}b[N];
int f[M],mx;
bool vis[N];
int main(){
//freopen("in","r",stdin);
n=read();m=read();k=read();
for(int i=;i<=n;i++) a[i]=read()+,f[a[i]]++,mx=max(mx,a[i]);
for(int i=;i<=m;i++) b[i].b=read()+,b[i].id=i,mx=max(mx,b[i].b);
sort(a+,a++n);
for(int i=mx;i>=;i--) if(f[i]>k) f[i-]+=f[i]-k,f[i]=k;
if(f[]) puts("-1");
else{
sort(b+,b++m);
for(int i=;i<=m;i++) f[b[i].b]++;
for(int i=mx;i>=;i--) if(f[i]>k) f[i-]+=f[i]-k,f[i]=k;
int p=m-f[];
printf("%d\n",p);
for(int i=;i<=p;i++) vis[b[i].id]=;
for(int i=;i<=m;i++) if(vis[i]) printf("%d ",i);
}
}

Codeforces Round #398 (Div. 2)的更多相关文章

  1. Codeforces Round #398 (Div. 2) A. Snacktower 模拟

    A. Snacktower 题目连接: http://codeforces.com/contest/767/problem/A Description According to an old lege ...

  2. Codeforces Round #398 (Div. 2) C. Garland —— DFS

    题目链接:http://codeforces.com/contest/767/problem/C 题解:类似于提着一串葡萄,用剪刀剪两条藤,葡萄分成了三串.问怎样剪才能使三串葡萄的质量相等. 首先要做 ...

  3. Codeforces Round #398 (div.2)简要题解

    这场cf时间特别好,周六下午,于是就打了打(谁叫我永远1800上不去div1) 比以前div2的题目更均衡了,没有太简单和太难的...好像B题难度高了很多,然后卡了很多人. 然后我最后做了四题,E题感 ...

  4. Codeforces Round #398 (Div. 2) A,B,C,D

    A. Snacktower time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  5. Codeforces Round #398 (Div. 2) A B C D 模拟 细节 dfs 贪心

    A. Snacktower time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  6. Codeforces Round #398 (Div. 2) B,C

    B. The Queue time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  7. 【DFS】Codeforces Round #398 (Div. 2) C. Garland

    设sum是所有灯泡的亮度之和 有两种情况: 一种是存在结点U和V,U是V的祖先,并且U的子树权值和为sum/3*2,且U不是根,且V的子树权值和为sum/3. 另一种是存在结点U和V,他们之间没有祖先 ...

  8. 【枚举】【贪心】 Codeforces Round #398 (Div. 2) B. The Queue

    卡题意……妈的智障 一个人的服务时间完整包含在整个工作时间以内. 显然,如果有空档的时间,并且能再下班之前完结,那么直接输出即可,显然取最左侧的空档最优. 如果没有的话,就要考虑“挤掉”某个人,就是在 ...

  9. 【暴力】Codeforces Round #398 (Div. 2) A. Snacktower

    题意不复述. 用个bool数组记录一下,如果某一天,当前剩下的最大的出现了的话,就输出一段. #include<cstdio> using namespace std; int n; bo ...

随机推荐

  1. [国嵌笔记][033-034][设置svc模式]

    [设置svc模式] 设置CPU为SVC模式 1.因为初始化系统需要有很高的权限,SVC模式具有该权限,所以首先要使系统工作在SVC(0b10011)模式 2.设置cprs为0xd3(0b1101001 ...

  2. HDU 4763 Theme Section

    题目: It's time for music! A lot of popular musicians are invited to join us in the music festival. Ea ...

  3. WebService短信网关配置

    第一步:WebService框架选择[以CXF为例] 1.下载地址:http://cxf.apache.org/download.html,请事先安装好JDK(本人使用的是apache-cxf-2.7 ...

  4. php页面zend加密乱码的解决办法

    http://www.chinaz.com/program/2008/1021/41485.shtml?qq-pf-to=pcqq.group 今天在服务器部署一个php程序是有zend加密的页面出现 ...

  5. php对数组进行分页

      3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 ...

  6. Hive 多分隔符的使用 (转载)

    方法一)通过org.apache.hadoop.hive.contrib.serde2.RegexSerDe格式的serde. 1) 建表语句 #指定以^|~作为分隔符 CREATE TABlE ta ...

  7. salesforce零基础学习(八十一)更改标准字段的label名称(Admin)

    我们在开发中往往需要考虑国际化功能,salesforce 提供了国际化功能,在search部分搜索translate,便可以找到translate部分,从而对需要的进行translate.比如pick ...

  8. tomcat 部署war项目

    前提是 jdk环境已配好 把项目war包放到tomcat的webapps目录下 启动tomcat: 这里我把8080端口修改成了80 IP也修改了 如果没修改直接输入localhost:8080/te ...

  9. 全新的软件项目,好的开始决定了成功一半!(需求&计划)

    刚看完“无问西东”,电影里说人总归还是要留下些足迹(文字)的,那么赶紧跑图书馆来留下些文字. 最近去瑞士启动了一个新的项目,那么早上做项目,晚上总结留下了一张张思维导图来记录当时的感受, 手稿如下,字 ...

  10. linux 动态库的符号冲突问题

    最近,给同事定位了一个符号表的冲突问题,简单记录一下. A代码作为静态链接库,被包含进了B代码,然后编译成了动态链接库,B.so A代码同时作为静态链接库,被编译进入了main的主代码. main函数 ...