【CF】222 Div.1 B Preparing for the Contest
这样类似的题目不少,很多都是一堆优化条件求最优解,这个题的策略就是二分+贪心。
对时间二分, 对费用采用贪心。
/* 377B */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 typedef struct node_t {
int x, id;
node_t() {}
node_t(int x_, int id_):
x(x_), id(id_) {}
friend bool operator< (const node_t& a, const node_t& b) {
return a.x > b.x;
}
} node_t; typedef struct student {
int x, p, id;
student() {}
student(int x_, int p_, int id_):
x(x_), p(p_), id(id_) {}
friend bool operator< (const student& a, const student& b) {
return a.x > b.x;
}
} student; const int maxn = 1e5+;
int a[maxn];
int n, m, s;
node_t N[maxn];
student S[maxn]; bool comp(const student& a , const student& b) {
return a.x > b.x;
} bool judge(int t) {
priority_queue<int,vi, greater<int> > Q;
int i, j, k;
int p;
int tot = s; i = ; // i for student
j = ; // j for Q
while (j < m) {
while (i<n && S[i].x>=N[j].x) {
Q.push(S[i].p);
++i;
}
if (Q.empty())
break;
p = Q.top();
Q.pop();
if (p > tot)
return false;
tot -= p;
j += t;
} return j>=m;
} void bfs(int t) {
priority_queue<pii,vpii, greater<pii> > Q;
int i, j, k, id;
int m_; i = ; // i for student
j = ; // j for Q
while (j < m) {
while (i<n && S[i].x>=N[j].x) {
Q.push(mp(S[i].p, S[i].id));
++i;
}
id = Q.top().sec;
Q.pop();
m_ = min(m, j+t);
for (k=j; k<m_; ++k)
a[N[k].id] = id;
j += t;
}
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int ans = -; scanf("%d %d %d", &n, &m, &s);
rep(i, , m) {
scanf("%d", &N[i].x);
N[i].id = i;
}
rep(i, , n) {
scanf("%d", &S[i].x);
S[i].id = i+;
}
rep(i, , n)
scanf("%d", &S[i].p); sort(N, N+m);
sort(S, S+n); if (S[].x < N[].x) {
puts("NO");
return ;
} int l = ;
int r = m+;
int mid; while (l <= r) {
mid = (l+r)>>;
if (judge(mid)) {
ans = mid;
r = mid - ;
} else {
l = mid + ;
}
} if (ans > ) {
puts("YES");
bfs(ans);
rep(i, , m)
printf("%d ", a[i]);
putchar('\n');
} else {
puts("NO");
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
【CF】222 Div.1 B Preparing for the Contest的更多相关文章
- Codeforces Round #222 (Div. 1) B. Preparing for the Contest 二分+线段树
B. Preparing for the Contest 题目连接: http://codeforces.com/contest/377/problem/B Description Soon ther ...
- 【CF】121 Div.1 C. Fools and Roads
题意是给定一棵树.同时,给定如下k个查询: 给出任意两点u,v,对u到v的路径所经过的边进行加计数. k个查询后,分别输出各边的计数之和. 思路利用LCA,对cnt[u]++, cnt[v]++,并对 ...
- 【CF】310 Div.1 C. Case of Chocolate
线段树的简单题目,做一个离散化,O(lgn)可以找到id.RE了一晚上,额,后来找到了原因. /* 555C */ #include <iostream> #include <str ...
- 【CF】110 Div.1 B. Suspects
这题目乍眼一看还以为是2-sat.其实很水的,O(n)就解了.枚举每个人,假设其作为凶手.观察是否满足条件.然后再对满足的数目分类讨论,进行求解. /* 156B */ #include <io ...
- 【CF】207 Div.1 B.Xenia and Hamming
这题目一看很牛逼,其实非常easy.求求最小公倍数,最大公约数,均摊复杂度其实就是O(n). /* 356B */ #include <iostream> #include <str ...
- 【CF】142 Div.1 B. Planes
SPFA.注意状态转移条件,ans的求解需要在bfs中间求解.因为只要到了地点n,则无需等待其他tourist.还是蛮简单的,注意细节. /* 229B */ #include <iostrea ...
- 【CF】196 Div.2 D. Book of Evil
显然这个图是一课树,看着题目首先联想到LCA(肯定是可以解的).但是看了一下数据大小,应该会TLE.然后,忽然想到一个前面做过的题目,大概是在一定条件下树中某结点旋转成为根后查询最长路径.结果灵感就来 ...
- 【CF】223 Div.1 C Sereja and Brackets
水线段树. /* 380C */ #include <iostream> #include <string> #include <map> #include < ...
- 【CF】259 Div.1 B Little Pony and Harmony Chest
还蛮有趣的一道状态DP的题目. /* 435B */ #include <iostream> #include <string> #include <map> #i ...
随机推荐
- RazorEngine在非MVC下的使用,以及使用自定义模板
---恢复内容开始--- RazorEngine模板引擎大大的帮助了我们简化字符串的拼接与方法的调用,开源之后,现在在简单的web程序,winform程序,甚至控制台程序都可以利用它来完成. 但如何在 ...
- linux安装rzsz
rz,sz是Linux/Unix同Windows进行ZModem文件传输的命令行工具优点:比ftp命令方便,而且服务器不用打开FTP服务. sz:将选定的文件发送(send)到本地机器rz:运行该命令 ...
- Eclipse使用Maven创建Web项目
一.Maven插件下载.jdk下载 1.maven下载地址: http://maven.apache.org/download.cgi 2.jdk下载地址: http://www.oracle.com ...
- java中的JSON对象的使用
申明:没工作之前都没听过JSON,可能是自己太菜了.可能在前台AJAX接触到JSON,这几天要求在纯java的编程中,返回JSON字符串形式. 网上有两种解析JSON对象的jar包:JSON-lib. ...
- get share button count
class shareCount { private $url,$timeout; function __construct($url,$timeout=10) { $this->url=raw ...
- jsp 嵌套iframe 从iframe中表单提交并传值到外层
今天因需求迭代 更改元来代码 遇到了这么个问题 就是想在 iframe中提交后进行整个页面的跳转 并把iframe中的值传到外层jsp 大概就是这个样子 外层 a.jsp <div id=&qu ...
- php 商务网站购物车联动地址
数据表如下: CREATE TABLE IF NOT EXISTS `china` (`region_id` smallint(5) unsigned NOT NULL, `parent_id` s ...
- shell for循环+case的脚本(监控程序状态)
分享一个shell for循环+case的脚本(监控程序状态) 分享一个for循环+case的脚本(监控程序状态并执行相关操作) ,供大家学习参考. 复制代码代码如下: #/bin/bash set ...
- Linux - tar命令过滤某个目录
tar -zcvf abc.20140325.tar.gz --exclude=./abc/kkk/--exclude=./abc/hhh/ ./abc/ 发现没有过滤成功,后来发现这种方法是不对的( ...
- 【 java版坦克大战--事件处理】 让坦克动起来--事件处理的准备
要能够控制坦克运动,必须用到事件处理的知识. 事件处理的一个demo. /** * 事件处理机制:委派事件模型.指当事件发生的时候,产生事件的对象(事件源),会把此 * "消息"传 ...