Codeforces Round #398 (Div. 2) BCD
B:The Queue
题目大意:你要去办签证,那里上班时间是[s,t), 你知道那一天有n个人会来办签证,他们分别是在时间点ai来的。每个人办业务要花相同的时间x,问你什么时候来 排队等待的时间最少。 (如果你和某个人同时来排队,你会排在他后面) 所有时间为正整数。
题解:
首先可以模拟出 每个人的业务什么时候会办好,那么最优解要么是在第一个人来之前的一分钟来,即a1-1,要么是在某个人的业务刚办好的时候来。 分别求出要等待的时间即可。
注意如果有多个人同时来,那么只能在这些人里的最后的业务办好之后来。
代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <cstdlib>
#include <set>
#include <queue>
using namespace std; #define X first
#define Y second
#define Mod 1000000007
#define N 1000010
#define M 101 typedef long long ll;
typedef pair<int,int> pii; int n;
ll s,t,x,ans1,ans2;
ll v[N]; int main()
{
//freopen("in.in","r",stdin);
//freopen("out.out","w",stdout); scanf("%I64d%I64d%I64d",&s,&t,&x);
scanf("%d",&n); for (int i=;i<=n;i++) scanf("%I64d",&v[i]);
while (n && v[n]+x>t) n--;
if (n==)
{
printf("%I64d\n",s);
return ;
} ans1=v[]-; ans2=s-ans1; ll cur=s;
for (int i=;i<=n;i++)
{
if (v[i]<=cur) cur+=x;
else cur=v[i]+x;
if (i<n && v[i]==v[i+]) continue;
if (i<n)
{
ll tmp=v[i+]-,tt=max(0ll,cur-tmp);
if (tt<ans2) ans1=tmp,ans2=tt;
}
//cout<<i<<' '<<cur<<endl;
}
if (cur+x<=t)
{
ans1=cur,ans2=;
}
printf("%I64d\n",ans1);
return ;
}
C:Garland
题目大意:
给出一棵树,要求分成3部分,每个部分的点权和相同。
题解:
首先所有点的点权之和必须是3的倍数,否则无解。记s[x]为以x为根的子树点权和,tmp=所有点权和/3。假设我们选了u,v这两个点,并且切掉了它们到它们的父节点的边。那么符合要求的只有2种情况:
1. s[u]=s[v]=tmp. lca(u,v)!=u && lca(u,v)!=v.
2. s[u]=tmp*2,s[v]=tmp, lca(u,v)=u.
首先做一次dfs求出所有s[x]。
然后做第二次dfs:对于第2种情况,只要记录从根到当前节点是否存在s[u]=tmp*2的点, 如果存在,且当前节点s[v]=tmp,那么就找到了一种分割方案。
对于第1种情况, 对于当前节点v, 且s[v]=tmp, 我们需要知道是否存在一个点u,满足s[u]=tmp*2,且u不在 根到v的路径中。 这里用点小技巧, 假设dfs到了v,那么根到v的路径中的点都还在栈里, 所以只要考虑已经不在栈里的点u。 具体实现看代码。
代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <cstdlib>
#include <set>
#include <queue>
using namespace std; #define X first
#define Y second
#define Mod 1000000007
#define N 1000010
#define M 101 typedef long long ll;
typedef pair<int,int> pii; int n,t1,t2,rt,tmp,tt;
int father[N],v[N],s[N];
vector<int> g[N]; void Dfs(int x)
{
s[x]=v[x];
for (int i=;i<g[x].size();i++)
{
int y=g[x][i]; if (y==father[x]) continue;
Dfs(y); s[x]+=s[y];
}
} void Dfs2(int x,int pre)
{
if (pre && s[x]==tmp) t1=pre,t2=x;
if (tt && s[x]==tmp) t1=tt,t2=x;
for (int i=;i<g[x].size();i++)
{
int y=g[x][i]; if (y==father[x]) continue;
if (x!=rt && s[x]==tmp*) Dfs2(y,x);
else Dfs2(y,pre);
}
if (s[x]==tmp) tt=x;
} int main()
{
//freopen("in.in","r",stdin);
//freopen("out.out","w",stdout); scanf("%d",&n); int sum=;
for (int i=;i<=n;i++)
{
scanf("%d%d",&father[i],&v[i]);
if (father[i]) g[father[i]].push_back(i);
else rt=i;
sum+=v[i];
}
if (sum%){printf("-1\n"); return ;}
tmp=sum/; Dfs(rt);
Dfs2(rt,);
if (t1 && t2) printf("%d %d\n",t1,t2);
else printf("-1\n");
return ;
}
D:
有n瓶牛奶,分别还有ai天过期,每天最多喝k瓶。 超市里有m瓶牛奶,分别还有bi天过期, 问最多能从超市里买多少瓶牛奶,使得买来的牛奶加上本来已有的,都可以在过期之前喝完。
n<=100w.
题解:
显然要先买保质期长的牛奶,所以可以考虑二分答案。 如何判断可行性呢? 根据贪心策略,显然要先喝保质期短的牛奶。所以只要把牛奶按保质期排序就好。这里就涉及到将两个单调的序列合并成一个单调序列的问题。 数据范围100w应该是为了卡掉暴力sort合并的O(nlognlogn)解法.
代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <cstdlib>
#include <set>
#include <queue>
using namespace std; #define X first
#define Y second
#define Mod 1000000007
#define N 1000010
#define M 101 typedef long long ll;
typedef pair<int,int> pii; int n,m,k;
int a[N],q[N<<]; struct node
{
int v,id;
bool operator < (const node &t)const
{
return v<t.v;
}
}b[N]; bool check(int mid)
{
int i=,j=mid,cnt=;
while (i<n || j<m)
{
if (i>=n) q[cnt]=b[j].v,j++;
else if (j>=m) q[cnt]=a[i],i++;
else
{
if (a[i]<b[j].v) q[cnt]=a[i],i++;
else q[cnt]=b[j].v,j++;
}
if (cnt/k>q[cnt]) return false;
cnt++;
}
return true;
} int main()
{
//freopen("in.in","r",stdin);
//freopen("out.out","w",stdout); scanf("%d%d%d",&n,&m,&k);
for (int i=;i<n;i++) scanf("%d",&a[i]);
for (int i=;i<m;i++) scanf("%d",&b[i].v),b[i].id=i+;
sort(a,a+n);sort(b,b+m);
bool flag=true;
for (int i=;i<n;i++) if (a[i]<i/k) flag=false;
if (!flag){printf("-1\n"); return ;} int l=,r=m,mid,ans;
while (l<r)
{
mid=(l+r)>>;
if (check(mid)) r=mid;
else l=mid+;
} ans=m-l;
printf("%d\n",ans);
for (int i=m-ans;i<m;i++) printf("%d ",b[i].id);
printf("\n");
return ;
}
Codeforces Round #398 (Div. 2) BCD的更多相关文章
- Codeforces Round #398 (Div. 2)
Codeforces Round #398 (Div. 2) A.Snacktower 模拟 我和官方题解的命名神相似...$has$ #include <iostream> #inclu ...
- Codeforces Round #398 (Div. 2) A. Snacktower 模拟
A. Snacktower 题目连接: http://codeforces.com/contest/767/problem/A Description According to an old lege ...
- Codeforces Round #398 (Div. 2) C. Garland —— DFS
题目链接:http://codeforces.com/contest/767/problem/C 题解:类似于提着一串葡萄,用剪刀剪两条藤,葡萄分成了三串.问怎样剪才能使三串葡萄的质量相等. 首先要做 ...
- Codeforces Round #398 (div.2)简要题解
这场cf时间特别好,周六下午,于是就打了打(谁叫我永远1800上不去div1) 比以前div2的题目更均衡了,没有太简单和太难的...好像B题难度高了很多,然后卡了很多人. 然后我最后做了四题,E题感 ...
- 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 ...
- 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 ...
- 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 ...
- 【DFS】Codeforces Round #398 (Div. 2) C. Garland
设sum是所有灯泡的亮度之和 有两种情况: 一种是存在结点U和V,U是V的祖先,并且U的子树权值和为sum/3*2,且U不是根,且V的子树权值和为sum/3. 另一种是存在结点U和V,他们之间没有祖先 ...
- 【枚举】【贪心】 Codeforces Round #398 (Div. 2) B. The Queue
卡题意……妈的智障 一个人的服务时间完整包含在整个工作时间以内. 显然,如果有空档的时间,并且能再下班之前完结,那么直接输出即可,显然取最左侧的空档最优. 如果没有的话,就要考虑“挤掉”某个人,就是在 ...
随机推荐
- 关于java的关键字 transient
我们都知道一个对象只要实现了Serilizable接口,这个对象就可以被序列化,Java的这种序列化模式为开发者提供了很多便利,我们可以不必关系具体序列化的过程,只要这个类实现了Serilizable ...
- C#中使用 HttpWebRequest 向网站提交数据
HttpWebRequest 是 .NET 基类库中的一个类,在命名空间 System.Net 里,用来使用户通过 HTTP 协议和服务器交互. HttpWebRequest 对 HTTP 协议进行了 ...
- java中List和Array相互转换
List to Array List 提供了toArray的接口,所以可以直接调用转为object型数组 List<String> list = new ArrayList<Stri ...
- Hadoop之Azkaban详解
工作流调度器azkaban1 为什么需要工作流调度系统 1)一个完整的数据分析系统通常都是由大量任务单元组成:shell脚本程序,java程序,mapreduce程序.hive脚本等 2)各任务单元之 ...
- 常见BUG问题汇总[待更新]
1.字符串数据库长度问题,特别是与java接口对接的过程中要注意 2.存储数据库之前所有的数据都需要在存储前进行验证
- cmdb是什么
CMDB -- Configuration Management Database 配置管理数据库,CMDB存储与管理企业IT架构中设备的各种配置信息,它与所有服务支持和服务交付流程都紧密相联,支持这 ...
- iOS 调用第三方地图进行导航
//支持的地图 { _model = model; //支持的地图 NSMutableArray *maps = [NSMutableArray array]; //苹果原生地图-苹果原生地图方法和其 ...
- Activity具体解释(生命周期、启动方式、状态保存,全然退出等)
一.什么是Activity? 简单的说:Activity就是布满整个窗体或者悬浮于其它窗体上的交互界面. 在一个应用程序中通常由多个Activity构成,都会在Manifest.xml中指定一个主的A ...
- python 利用numpy进行数据分析
一.numpy.loadtxt读取数据 data=numpy.loadtxt('数据路径.txt',delimiter=',',usecols=(0,1,2,3) , dtype=float)#读取后 ...
- python中MySQL模块TypeError: %d format: a number is required, not str异常解决
转载自:http://www.codeif.com/topic/896 python代码: attr_sql = "INSERT INTO `ym_attribute` (`attr_nam ...