UVALive 6198 A Terribly Grimm Problem 二分匹配 求字典序最小解
来源:互联网 发布:淘宝抠图多少钱 编辑:程序博客网 时间:2024/06/05 05:34
题目链接:点击打开链接
题意:
给定一个区间[l,r]
每个数都选择他的其中一个因子。
使得任意两个数选择的因子各不相同,且字典序最小。
思路:
先对每个数分解质因数。
然后从l开始枚举,让i选择最小的因子,判断 [i+1,r]是否有可行解,若有则让i选择这个因子。
如此枚举下去即可
==复杂度不能直视
#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include <cmath>#include <map>#include<vector>template <class T>inline bool rd(T &ret) { char c; int sgn; if(c=getchar(),c==EOF) return 0; while(c!='-'&&(c<'0'||c>'9')) c=getchar(); sgn=(c=='-')?-1:1; ret=(c=='-')?0:(c-'0'); while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0'); ret*=sgn; return 1;}template <class T>inline void pt(T x) { if (x <0) { putchar('-'); x = -x; } if(x>9) pt(x/10); putchar(x%10+'0');}using namespace std;typedef long long ll;const int N = 1000000;ll prime[9592],primenum;//有primenum个素数 math.hvoid PRIME(ll Max_Prime){ primenum=0; prime[primenum++]=2; for(ll i=3;i<=Max_Prime;i+=2) for(ll j=0;j<primenum;j++) if(i%prime[j]==0)break; else if(prime[j]>sqrt((double)i) || j==primenum-1) { prime[primenum++]=i; break; }}void get_prime(ll x, vector<ll> &G){ // cout<<"i:"<<x<<"{"; for(ll i = 0; prime[i]*prime[i]<=x; i++) if(x%prime[i] == 0){ G.push_back(prime[i]); while(x%prime[i]==0)x/=prime[i]; } if(x!=1) G.push_back(x);//for(int i = 0; i < G.size(); i++)cout<<G[i]<<" ";cout<<endl;}ll l, r;vector<ll>G[N];void input(){ for(ll i = l; i <= r; i++) get_prime(i, G[i-l]);}map<ll,ll>lef, rig;//lef[v]表示Y集的点v 当前连接的点 , pn为x点集的点数map<ll,bool> T, must; //T[u] 表示Y集 u 是否已连接X集bool match(ll x){ // x和Y集 匹配 返回x点是否匹配成功 for(int i=0; i<G[x-l].size(); i++) { ll v = G[x-l][i]; if(!T[v] && !must[v]) { T[v] = 1; if((int)lef.count(v)==0 ||match(lef[v])) //match(lef[v]) : 原本连接v的X集点 lef[v] 能不能和别人连,如果能 则v这个点就空出来和x连 { lef[v] = x; return true; } } } return false;}bool find(ll x){ lef.clear(); for(x++; x <= r; x++) { T.clear(); if(!match(x))return false; } return true;}void solve(){ rig.clear(); must.clear(); for(ll i = l; i<= r; i++)//X集匹配,X集点标号从 1-pn 匹配边是G[左点].size() { for(int j = 0; j < G[i-l].size(); j++) { ll v = G[i-l][j]; if(must[v])continue; rig[i] = v; must[v] = 1; if(!find( i ))must[v] = 0; else break; } must[rig[i]] = 1; }}int main(){ PRIME(100000); while(cin>>l>>r, l+r){ input(); solve(); for(ll i = l; i <= r; i++) printf("%lld%c", rig[i], i==r?'\n':' '); for(ll i = l; i <= r; i++) G[i-l].clear(); } return 0;}
0 0
- UVALive 6198 A Terribly Grimm Problem 二分匹配 求字典序最小解
- UVALive 6198 A Terribly Grimm Problem
- URAL 1949 The Best Picture in the Galaxy 二分匹配 求字典序最小解
- 求字典序最小解+贪心
- HDU 1814 2-sat 求字典序最小解
- hdoj A Simple Problem 4143 (数学求方程最小解)
- uvalive 2966 求一个二分图 所有的完美匹配
- UVALive - 4043 Ants (二分图最大权匹配)
- UVALive-5013 Similarity(二分图最大权匹配)
- UVALive 6525 二分匹配
- 确定比赛名次 hdu 拓扑输出字典序最小解
- hdoj 4857 逃生 【拓扑排序 输出字典序最小解】
- 【2-SAT字典序最小解】HDU1814[Peaceful Commission]题解
- HDU 2458 二分匹配求最大团
- 二分图求最大匹配数,最完美匹配概念
- UVAlive 6525 二分图匹配
- UVALive 5963 (LA 5963) Confusion in the Problem Set 二分图匹配 (或 YY)
- 求最大权二分匹配的KM算法
- 实用的Linux/Unix快捷键
- 谈谈初学者该怎么学电脑
- 集合与多重集合(set和multiset)
- js获取div的坐标
- WIFI破解之胡言乱语
- UVALive 6198 A Terribly Grimm Problem 二分匹配 求字典序最小解
- CodeForces 34C Page Numbers
- Xcode设置项之Architectures和Valid Architectures
- hibernate简单实例
- 映射和多重映射(map和multimap)
- String Date Calendar Timestamp之间的转换
- hdu 5092 Seam Carving 2014上海全国邀请赛——题目重现
- 遇到问题了。。。
- Javascript 基础知识 - 数组