gym 100430 G【贪心+map瞎搞】
来源:互联网 发布:通达信股票行情软件 编辑:程序博客网 时间:2024/05/21 04:21
题意:
给你N个东西,其中任意两种东西可以合成。
问你最多可以合成多少东西。
在最多合成东西的情况下,要使得字典序最小。
思路:
先按照贪心的方法构造出字典序列最优的解。
然后有可能有一个剩下了一些的数量的东西(也可能没有)
那么就按照字典序从后往前扫,强行将最后的几对拆开来和剩余的匹配。
#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <iostream>#include <map>#define MAXN 100005using namespace std;typedef long long ll;map< pair<int, int>, long long > Ans;map< pair<int, int> , long long, greater< pair<int, int> > > Res;long long a[MAXN];int n;void update(int x, int y, ll z){ if(z == 0) return; if(Ans.find(make_pair(x,y)) != Ans.end()) Ans[make_pair(x,y)] += z; else Ans[make_pair(x,y)] = z;}int main(){ freopen("magic.in", "r", stdin); freopen("magic.out", "w", stdout); cin>>n; for(int i=1; i<=n; i++) cin>>a[i]; Ans.clear(); Res.clear(); for(int i=1; i<=n; i++) for(int j=i+1; j<=n && a[i]>0; j++) { ll siz = min(a[i],a[j]); if(siz == 0) continue; if(Res.find(make_pair(i,j)) != Res.end()) Res[make_pair(i,j)] += siz; else Res[make_pair(i,j)] = siz; a[i] -= siz; a[j] -= siz; } int remain_id = -1; ll remain_num = 0; for(int i=1; i<=n; i++) if(a[i] > 0) { remain_id = i; break; } if(remain_id!=-1) remain_num = a[remain_id]/2; else remain_num = 0; for(auto it: Res) { int i = it.first.first, j = it.first.second, ff, ee; ll res = it.second; if(i != remain_id && j != remain_id) { if(remain_num != 0) { ll siz = min(res, remain_num); ff = min(remain_id, i), ee = max(remain_id, i); update(ff, ee, siz); ff = min(remain_id, j), ee = max(remain_id, j); update(ff, ee, siz); update(i, j, res - siz); remain_num -= siz; } else { update(i, j, res); } } else { update(i, j, res); } } cout<<Ans.size()<<endl; for(auto it: Ans) { cout<<it.first.first<<" "<<it.first.second<<" "<<it.second<<endl; } return 0;}
// whn6325689// Mr.Phoebe// http://blog.csdn.net/u013007900#include <algorithm>#include <iostream>#include <iomanip>#include <cstring>#include <climits>#include <complex>#include <fstream>#include <cassert>#include <cstdio>#include <bitset>#include <vector>#include <deque>#include <queue>#include <stack>#include <ctime>#include <set>#include <map>#include <cmath>#include <functional>#include <numeric>#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;#define eps 1e-9#define PI acos(-1.0)#define INF 0x3f3f3f3f#define LLINF 1LL<<62#define speed std::ios::sync_with_stdio(false);typedef long long ll;typedef unsigned long long ull;typedef long double ld;typedef pair<ll, ll> pll;typedef complex<ld> point;typedef pair<int, int> pii;typedef pair<pii, int> piii;typedef vector<int> vi;#define CLR(x,y) memset(x,y,sizeof(x))#define CPY(x,y) memcpy(x,y,sizeof(x))#define clr(a,x,size) memset(a,x,sizeof(a[0])*(size))#define cpy(a,x,size) memcpy(a,x,sizeof(a[0])*(size))#define debug(a) cout << #a" = " << (a) << endl;#define debugarry(a, n) for (int i = 0; i < (n); i++) { cout << #a"[" << i << "] = " << (a)[i] << endl; }#define mp(x,y) make_pair(x,y)#define pb(x) push_back(x)#define lowbit(x) (x&(-x))#define MID(x,y) (x+((y-x)>>1))#define getidx(l,r) (l+r | l!=r)#define ls getidx(l,mid)#define rs getidx(mid+1,r)#define lson l,mid#define rson mid+1,rtemplate<class T>inline bool read(T &n){ T x = 0, tmp = 1; char c = getchar(); while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar(); if(c == EOF) return false; if(c == '-') c = getchar(), tmp = -1; while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar(); n = x*tmp; return true;}template <class T>inline void write(T n){ if(n < 0) { putchar('-'); n = -n; } int len = 0,data[20]; while(n) { data[len++] = n%10; n /= 10; } if(!len) data[len++] = 0; while(len--) putchar(data[len]+48);}//-----------------------------------const int MAXN=100010;map<pii,ll> Ans,Res;int a[MAXN],n;int main(){ freopen("magic.in", "r", stdin); freopen("magic.out", "w", stdout); cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; Ans.clear(); Res.clear(); for(int i=1;i<=n;i++) for(int j=i+1;j<=n && a[i]>0;j++) { if(a[j]==0) continue; ll siz=min(a[i],a[j]); Res[mp(i,j)]+=siz; a[i]-=siz;a[j]-=siz; } int remain_id=-1; ll remain_num=0; for(int i=1;i<=n;i++) if(a[i]>0) { remain_id=i; break; } if(remain_id!=-1) remain_num=a[remain_id]/2; else remain_num=0; for(map<pii,ll>::reverse_iterator it=Res.rbegin();it!=Res.rend();it++) { int i=it->first.first,j=it->first.second,ff,ee; ll res=it->second; if(i!=remain_id && j!=remain_id) { if(remain_num>res) { ll siz=res; ff=min(remain_id,i),ee=max(remain_id,i); Ans[mp(ff,ee)]+=siz; ff=min(remain_id,j),ee=max(remain_id,j); Ans[mp(ff,ee)]+=siz; remain_num-=siz; } else if(remain_num!=0) { ll siz=remain_num; ff=min(remain_id,i),ee=max(remain_id,i); Ans[mp(ff,ee)]+=siz; ff=min(remain_id,j),ee=max(remain_id,j); Ans[mp(ff,ee)]+=siz; if(res!=siz) Ans[mp(i,j)]+=res-siz; remain_num=0; } else { Ans[mp(i,j)]+=res; } } else Ans[mp(i,j)]+=res; } cout<<Ans.size()<<endl; for(auto it:Ans) { cout<<it.first.first<<" "<<it.first.second<<" "<<it.second<<endl; } return 0;}
0 0
- gym 100430 G【贪心+map瞎搞】
- Gym 100801G Graph(贪心构造)
- Gym 100345G Pulp Fiction 贪心
- GYM 100030 G.Procrastination(贪心)
- Problem G. Pulp Fiction-Gym 100345G-贪心
- GYM 100488 G.Change-making Problem(贪心)
- HDU 5738 Eureka(map瞎搞)
- Gym 100712E 贪心
- CodeForces 398A Cards 贪心 暴力 瞎搞
- UVA 12545 Bits Equalizer(瞎搞 贪心)
- HDU6227(2017acm-沈阳) 贪心/思维/瞎搞
- codeforces gym 100548G
- Gym 100269G-Garage
- CodeForces Gym 100735G
- CodeForces Gym 100735G
- Gym 100971G Repair
- Gym 100685G
- Codeforces-GYM-100923G
- Linux 内核态与用户态
- UITableView上下滚动卡顿(获取网络数据,下载图片之后)
- SDUT 1519 数据结构实验之链表八:Farey序列
- 多元回归分析r语言代码
- HDU 4974 A simple water problem
- gym 100430 G【贪心+map瞎搞】
- Android 5.1 Contacts源码分析(三):Contacts模块Fargment结构
- FastCGI简介
- 我的Docker学习之旅<三>
- httppost 远程服务器返回错误: (400) 错误的请求错误
- hdu 5456 Matches Puzzle Game(记忆化搜索)
- 自定义Tabbar封装
- ios 内存小结
- 链路层介绍,转载自实验楼