贪心

考场上因无优化与卡常T掉的\(n \log(n)\)

//#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long #define ON_DEBUG #ifdef ON_DEBUG #define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin); #else #define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ; #endif struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std; const int N = 100007; struct Day{
long long cost, taste;
bool operator < (const Day &com) const{
if(taste != com.taste) return taste > com.taste;
return cost > com.cost;
}
}day[N];
struct Food{
long long cost, taste;
bool operator < (const Food &com) const{
if(cost != com.cost) return cost < com.cost;
return taste < com.taste;
}
}food[N]; int n, m;
inline int FindFood(int price){
int l = 1, r = m;
while(l <= r){
int mid = (l + r) >> 1;
if(food[mid].cost >= price)
r = mid - 1;
else
l = mid + 1;
}
return l;
}
int vis[N]; int main(){
freopen("snack.in", "r", stdin);
freopen("snack.out", "w", stdout);
io >> n >> m;
R(i,1,n){
io >> day[i].cost >> day[i].taste;
} R(i,1,m){
io >> food[i].cost >> food[i].taste;
} if(m < n){
printf("-1");
return 0;
} sort(day + 1, day + n + 1);
sort(food + 1, food + m + 1); long long ans = 0;
R(i,1,n){
int pos = FindFood(day[i].cost);
while(vis[pos] == true) ++pos;
if(pos > m){
printf("-1"); return 0;
}
int flag = 0;
R(j,pos,m){
if(vis[j]) continue;
if(food[j].taste >= day[i].taste){
vis[j] = true;
ans += food[j].cost;
flag = 1;
break;
}
}
if(!flag){
printf("-1"); return 0;
}
} printf("%lld", ans); return 0;
}
/*
one money raise
one taste down
4 7
1 1
2 3
1 4
4 2
3 2
2 1
4 3
5 2
5 4
2 6
4 4
*/

正解用数据结构,或STL水

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long #define ON_DEBUG #ifdef ON_DEBUG #define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin); #else #define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ; #endif struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std;
#include <set> const int N = 100007; multiset<int> st;
pair<int,int> day[N], food[N];
int main(){
//FileOpen();
int n,m;
io >> n >> m;
R(i,1,n){
io >> day[i].second >> day[i].first;
}
R(i,1,m){
io >> food[i].second >> food[i].first;
} sort(day + 1, day + n + 1);
sort(food + 1, food + m + 1); int j = m;
long long ans = 0;
nR(i,n,1){
while(j && food[j].first >= day[i].first){
st.insert(food[j].second);
--j;
}
multiset<int>::iterator it = st.lower_bound(day[i].second); if(it == st.end()){
printf("-1"); return 0;
} ans += *it;
st.erase(it);
} printf("%lld", ans);
return 0;
}

最新文章

  1. 黑马程序员——【Java高新技术】——类加载器
  2. 希望有兴趣的加入,共同为项目智能化管理jar包而努力 第二篇
  3. SQL Server 2008 下载及安装教程
  4. 学会查看tomcat的日志文件
  5. 矩阵-DirectX与OpenGL的不同
  6. ansible服务模块和组模块使用
  7. CF Gym100548 K Last Defence 解题报告
  8. php动态滚动加载实例
  9. [转] Android学习系列(29)--App调试的几个命令实践
  10. java笔记9之switch
  11. 至Webserver构造svgz的文件需要http头,让你的浏览器中打开svgz档
  12. 【learning】多项式相关(求逆、开根、除法、取模)
  13. 【翻译】Ext JS——高效的编码风格指南
  14. openstack-KVM管理工具
  15. 活动代码页437--修改windows的系统编码
  16. 坑!vue.js在ios9中失效
  17. Centos6.3 下使用 Tomcat-6.0.43 非root用户 jsvc模式部署 生产环境 端口80 vsftp
  18. Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException
  19. keil里面填数据
  20. CentOS 7 下 ifconfig command not found 解决办法

热门文章

  1. 主管发话:一周搞不定用友U8 ERP跨业务数据分析,明天就可以“毕业”了
  2. 附001.Python多版本环境管理
  3. docker安装RabbitMQ及安装延迟插件
  4. C#.NET读取文本文件的几种办法
  5. Charles如何抓取https请求-移动端+PC端
  6. 粗谈对ajax的理解
  7. nifi从入门到实战(保姆级教程)——环境篇
  8. zabbix主动式和被动式
  9. Linux命令格式、终端类型和获取帮助的方法
  10. Linux YUM yum 命令详解