Codeforces - 71E 状压DP
参考官方题解
#include<bits/stdc++.h>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
using namespace std;
string s[100]={"H","He","Li","Be","B",
"C","N","O","F","Ne",
"Na","Mg","Al","Si","P",
"S","Cl","Ar","K","Ca",
"Sc","Ti","V","Cr","Mn","Fe","Co","Ni","Cu","Zn","Ga","Ge","As","Se","Br","Kr",
"Rb","Sr","Y","Zr","Nb","Mo","Tc","Ru","Rh","Pd","Ag","Cd","In","Sn","Sb","Te","I","Xe","Cs","Ba","La",
"Ce","Pr","Nd","Pm","Sm","Eu","Gd","Tb","Dy","Ho","Er","Tm","Yb","Lu","Hf","Ta","W","Re","Os","Ir","Pt","Au","Hg","Tl",
"Pb","Bi","Po","At","Rn","Fr","Ra","Ac","Th","Pa","U","Np","Pu","Am","Cm","Bk","Cf","Es","Fm"};
int num[111],ans[111][111],vec[111],que[111],p[1<<18|1];
int dp1[1<<18|1],dp2[1<<18|1];
int n,k,sum1,sum2;
string str;
bool go(){
memset(dp1,0,sizeof dp1);
memset(dp2,0,sizeof dp2);
// memset(dp2,-1,sizeof dp2);
rep(S,1,(1<<n)-1){
rep(i,1,n){
if((S>>(i-1))&1){
dp1[S]+=vec[i];
}
}
}
rep(S,1,(1<<n)-1){
dp2[S]=-1;
for(int S0=S;S0;S0=(S0-1)&S){
if(dp2[S^S0]!=-1&&que[dp2[S^S0]+1]==dp1[S0]){
dp2[S]=dp2[S^S0]+1;
p[S]=S^S0;
}
}
}
if(dp2[(1<<n)-1]!=k) return 0;
int x=(1<<n)-1;
rrep(i,k,1){
rep(j,1,n){
if(1<<(j-1)&(x^p[x])){
ans[i][++num[i]]=j;
}
}
x=p[x];
}
return 1;
}
inline void print(){
cout<<"YES"<<endl;
rep(i,1,k){
cout<<s[vec[ans[i][1]]-1];
rep(j,2,num[i]){
cout<<"+"<<s[vec[ans[i][j]]-1];
}
cout<<"->"<<s[que[i]-1]<<endl;
}
}
int main(){
while(cin>>n>>k){
sum1=sum2=0;
memset(num,0,sizeof num);
rep(i,1,n){
cin>>str;
rep(j,0,100-1){
if(str==s[j]){
vec[i]=j+1;
sum1+=j+1;
break;
}
}
}
rep(i,1,k){
cin>>str;
rep(j,0,100-1){
if(str==s[j]){
que[i]=j+1;
sum2+=j+1;
break;
}
}
}
if(sum1==sum2&&go()) print();
else cout<<"NO"<<endl;
}
return 0;
}
Codeforces - 71E 状压DP的更多相关文章
- Codeforces 678E 状压DP
题意:有n位选手,已知n位选手之间两两获胜的概率,问主角(第一个选手)最终站在擂台上的概率是多少? 思路:一看数据范围肯定是状压DP,不过虽然是概率DP,但是需要倒着推:我们如果正着推式子的话,初始状 ...
- Codeforces 8C 状压DP
题意:有个人想收拾行李,而n个物品散落在房间的各个角落里(n < 24).现在给你旅行箱的坐标(人初始在旅行箱处),以及n个物品的坐标,你一次只能拿最多两个物品,并且拿了物品就必须放回旅行箱,不 ...
- Codeforces 1215E 状压DP
题意:给你一个序列,你可以交换序列中的相邻的两个元素,问最少需要交换多少次可以让这个序列变成若干个极大的颜色相同的子段. 思路:由于题目中的颜色种类很少,考虑状压DP.设dp[mask]为把mask为 ...
- CodeForces 11D(状压DP 求图中环的个数)
Given a simple graph, output the number of simple cycles in it. A simple cycle is a cycle with no re ...
- codeforces 1185G1 状压dp
codeforces 1185G1. Playlist for Polycarp (easy version)(动态规划) 传送门:https://codeforces.com/contest/118 ...
- Codeforces 1155F 状压DP
题意:给你一张图,问最少保留多少条边,使得这张图是边双联通分量. 思路:如果一个点集中的点已经是边双联通分量,那么从这个点集中的点x出发,经过若干个不是点集中的点,回到点集中的点y(x可能等于y),那 ...
- codeforces Diagrams & Tableaux1 (状压DP)
http://codeforces.com/gym/100405 D题 题在pdf里 codeforces.com/gym/100405/attachments/download/2331/20132 ...
- Codeforces Gym 100015F Fighting for Triangles 状压DP
Fighting for Triangles 题目连接: http://codeforces.com/gym/100015/attachments Description Andy and Ralph ...
- Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP
Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10061 ...
随机推荐
- Python学习笔记_一个Tkinter示例,使用FileDialog
为了使用Python进行数据分析,编写一个图形界面,选择一个Excel文件(或CSV),然后进行后续处理. 一.本示例涵盖如下知识点: 1.FileDialog的使用 2.退出程序 3.消息提示框的示 ...
- 面试题:String StringBufere StringBuilder 不用看
一.String 使用 private final char value[]来实现字符串存储 所以String对象创建之后就不能再修改此对象中存储的字符串内容,所以说String本质是字符数组char ...
- Python pip命令
一.简介 pip 是一个安装和管理 Python 包的工具 , 是 easy_install 的一个替换品.distribute是setuptools的取代(Setuptools包后期不再维护了),p ...
- .Net插入大批量数据
1. 使用SqlDataAdapter /// <summary> /// 实现数据库事务,大批量新增数据 /// </summary> ...
- 一步一步带你构建第一个 Laravel 项目
参考链接:https://laravel-news.com/your-first-laravel-application 简介 按照以下的步骤,你会创建一个简易的链接分享网站. 安装 Laravel ...
- Jmeter接口测试-新用户注册API
新用户注册 新用户注册的接口是POST /register username/password/password_confirmation 该接口需要提供3个参数,分别是 username 用户名 p ...
- 编写高质量代码改善C#程序的157个建议——建议33:避免在泛型类型中声明静态成员
建议33:避免在泛型类型中声明静态成员 在上一建议中,已经理解了应该将MyList<int>和MyList<string>视作两个完全不同的类型,所以,不应该将MyList&l ...
- 转Delphi中XLSReadWrite控件的使用(3) 读和写Excel
unit OpExcell; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Fo ...
- HackSix 为ViewGroup的子视图添加悦目的动画效果
1.默认情况下他,添加到viewGrop的子视图是直接显示出来的.有一个比较简单的方法可以为这个过程增加动画效果. 2.知识点: 给子视图添加动画效果就用:LayoutAnimationCon ...
- angular 第二种依赖注入
import { Injectable } from '@angular/core'; import { ProductServiceService, Product } from './produc ...