ZOJ3963 【gay gay 的贪心】
来源:互联网 发布:中国社科院大学知乎 编辑:程序博客网 时间:2024/04/25 20:03
(这个题的idea好棒啊)
思路:
一个值 val 的插入最好就是在一个最接近他的值的结点x (权值x <= val) 上插入。
so,拿个 set 维护一下所有的结点,每次二分找一下。
还有要维护结点所能插的位置数量,不能插了要删除。
PS: set.upper_bound()好像是log(N)的
//#include <bits/stdc++.h>#include<iostream>#include<cstdio>#include<cstring>#include<stack>#include<set>#include<map>#include<queue>#include<cmath>#include<algorithm>typedef long long LL;using namespace std;//#pragma comment(linker, "/STACK:102400000,102400000")const int N=1e5+10;int n;int node[N],cnt[N]; //node[] 代表结点位于树的编号,cnt[]代表该结点能插的个数vector<int>ans[N]; //存答案set<int>shu;int num;void init(){ num = 0; for(int i=0;i<=n;i++){ ans[i].clear(); cnt[i]=node[i]=0; } shu.clear();}void solve(int x,int pos){ set<int>::iterator it; it=shu.upper_bound(x); if(it == shu.begin()){ shu.insert(x); cnt[x] = 2; node[x] = num; ans[num].push_back(pos); num++; } else{ it--; if(x != *it){ cnt[*it]--; node[x] = node[*it]; if(!cnt[*it]) shu.erase(it); shu.insert(x); cnt[x] = 2; ans[node[x]].push_back(pos); } else { cnt[x]++; ans[node[x]].push_back(pos); } }}int main(){ int T,x; scanf("%d",&T); while(T--){ init(); scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&x); solve(x,i); } printf("%d\n",num); for(int i=0;i<num;i++) { int sz = ans[i].size(); printf("%d",sz); for(int j=0;j<sz;j++) printf(" %d",ans[i][j]); puts(""); } } return 0;}
0 0
- ZOJ3963 【gay gay 的贪心】
- 关于Gay的笑话
- 米帅真的是gay吗?
- 访徐昊:论证李商隐是Gay 的咨询师
- 我的男朋友是个GAY
- ZOJ3963 贪心
- WELCOME THE NEW GAY
- Gay+Groupon=GayPon
- GTY's gay friends
- hdu5172 GTY's gay friends
- hdu5172---GTY's gay friends
- HDU5172-GTY's gay friends
- HDU5172GTY's gay friends——区间查询(区间内的数互不相同)
- unity中的sun shafts(也称gay ray)的一种实现方法
- LZ的大学室友一个是女装癖,一个是双性恋,一个是GAY(转)
- HDU 5172 GTY's gay friends 线段树(排列,区间不同的数)
- Hdu 5172 GTY's gay friends
- hdu 5172 GTY's gay friends
- HDU1028 Ignatius and the Princess III(母函数)
- hashMap和hashTable
- CPU卡程序设计实例(八)ESAM模块卡上、下电
- 关于 iOS 证书,你必须了解的知识
- 关于java的运算符,优先级,数据转换
- ZOJ3963 【gay gay 的贪心】
- NYOJ541 最强DE 战斗力
- CSS3 设置盒子三边内阴影、双边内阴影、单边内阴影(全)
- Android针对保存用户设置的设计
- Graph
- Spark踩坑记:共享变量
- 一些实用的java窗体的笔记
- 弹框,安装apk
- NYOJ-传纸条(一)、探寻宝藏【双路(线)DP】