UESTC 86 Divide (二进制贪心)
来源:互联网 发布:程序员兼职的平台 编辑:程序博客网 时间:2024/05/17 04:40
题意:给你n个东西,每个东西的值是
题解:首先这题应该是贪心,怎么贪心比较好呢,如果从大到小考虑,这个大的多1个,就用两个小的来抵,再往下就是4个。。。。这样就会炒鸡大,就炸了。
所以应该是从小往大的来考虑,但是这个考虑也不是很简单,我也是瞄了下题解才知道。就是把小的往大的进位,如果这个地方被进位了,并且如果它是奇数的话,其实是可以等分的(退位到小的,然后等分),所以可以把被进位的位置都标记了,然后最后可以找到第一个没有被标记的奇数位置,这一个就是最大的一堆,然后就把比他小的位置的,不管有没有被标记的,只要是奇数的都放进去,然后就是拿大的减去这些小的,得到的答案就是全都是1,这些1的位置都是0,最小的一个1的位置是1,比它小的位置后面都是0.
主要开LL
代码:
#include <map>#include <set>#include <stack>#include <queue>#include <cmath>#include <string>#include <vector>#include <cstdio>#include <cctype>#include <cstring>#include <sstream>#include <cstdlib>#include <iostream>#include <algorithm>#pragma comment(linker, "/STACK:102400000,102400000")using namespace std;#define MAX 100005#define MAXN 6005#define maxnode 15#define sigma_size 30#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define lrt rt<<1#define rrt rt<<1|1#define middle int m=(r+l)>>1#define LL long long#define ull unsigned long long#define mem(x,v) memset(x,v,sizeof(x))#define lowbit(x) (x&-x)#define pii pair<int,int>#define bits(a) __builtin_popcount(a)#define mk make_pair#define limit 10000//const int prime = 999983;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f;const double pi = acos(-1.0);//const double inf = 1e18;const double eps = 1e-8;const LL mod = 1e9+7;const ull mx = 133333331;/*****************************************************/inline void RI(int &x) { char c; while((c=getchar())<'0' || c>'9'); x=c-'0'; while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0'; }/*****************************************************/LL vis[MAX];int viss[MAX];int ans[MAX];int main(){ int t,kase=0; cin>>t; while(t--){ kase++; int n; cin>>n; int maxn=0; mem(viss,0); mem(vis,0); for(int i=1;i<=n;i++){ int a,b; scanf("%d%d",&a,&b); vis[a]+=(LL)b; maxn=max(maxn,a); } for(int i=0;i<=maxn;i++){ if(vis[i]>=2){ vis[i+1]+=vis[i]/2; viss[i+1]=1; } vis[i]%=2; } int tmp=-1; for(int i=maxn;i>=0;i--){ if(!viss[i]&&vis[i]){ tmp=i; break; } } printf("Case #%d: ",kase); if(tmp==-1) printf("0\n"); else{ vector<int> v; for(int i=0;i<=tmp;i++){ if(vis[i]) v.push_back(i); ans[i]=1; } for(int i=0;i<v[0];i++) ans[i]=0; for(int i=1;i<v.size();i++){ ans[v[i]]=0; } int flag=-1; for(int i=tmp;i>=0;i--){ if(ans[i]){ flag=i; break; } } if(flag==-1) printf("0\n"); else{ for(int i=flag;i>=0;i--) printf("%d",ans[i]); printf("\n"); } } } return 0;}
0 0
- UESTC 86 Divide (二进制贪心)
- UESTC 86 Divide(贪心+二进制)
- UESTC 86 Divide (脑洞题+二进制)
- UESTC 1711 Divide
- UESTC 1711 Divide 解题报告
- UESTC 1711 Divide 位操作模拟题
- UESTC--1271--Search gold(贪心)
- 【UESTC 1510 渐变字符串】 + 贪心
- HDU5783 Divide the Sequence(贪心)
- UESTC 1214 divide && PKU 3390 Print Words in Lines [DP]
- LeetCode 29. Divide Two Integers 二进制分解
- UESTC ACM 1529 并查集+贪心
- UESTC 1817 Complete Building the Houses 贪心
- UESTC 65 CD Making 贪心法
- UESTC 1255 斓少摘苹果 贪心法
- UESTC 1018 王之新学期 贪心法
- UESTC 929 Post office【前缀和+贪心】
- UESTC 1047 Alice's birthday【贪心】
- react 的 bind this 几种方法
- 转载:用Hadoop,还是不用Hadoop?(使用Hadoop的场景)
- LeetCode:Valid Perfect Square
- 开源GIS浅谈
- php 导入excel 方法
- UESTC 86 Divide (二进制贪心)
- 各种取整
- Matlab和C混合编程的一个靠谱思路
- JMM(内存模型)
- (2)linux基础系统调优
- shell编程初探
- SQlite数据库的增删改查
- Podfile文件详解
- 中图法分类号