题目链接:https://codeforces.com/gym/101873

C. Joyride

记忆化搜索形式的dp

    #include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert>
//#include <unordered_map>
/* ⊂_ヽ
  \\ Λ_Λ 来了老弟
   \('ㅅ')
    > ⌒ヽ
   /   へ\
   /  / \\
   レ ノ   ヽ_つ
  / /
  / /|
 ( (ヽ
 | |、\
 | 丿 \ ⌒)
 | |  ) /
'ノ )  Lノ */ using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define boost ios::sync_with_stdio(false);cin.tie(0)
#define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<;
const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = ;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} inline void cmax(int &x,int y){if(x<y)x=y;}
inline void cmax(ll &x,ll y){if(x<y)x=y;}
inline void cmin(int &x,int y){if(x>y)x=y;}
inline void cmin(ll &x,ll y){if(x>y)x=y;}
#define MODmul(a, b) ((a*b >= mod) ? ((a*b)%mod + 2*mod) : (a*b))
#define MODadd(a, b) ((a+b >= mod) ? ((a+b)%mod + 2*mod) : (a+b)) /*-----------------------showtime----------------------*/
const int maxn = 1e3+;
int dp[maxn][maxn];
vector<int>mp[maxn];
int x,n,m,T;
int tim[maxn],cost[maxn];
int dfs(int u,int t){
if(t > x) return inf;
if(dp[u][t]!=-) return dp[u][t];
if(u == && t == x) return dp[u][t] = ;
dp[u][t] = inf;
for(int i=; i<mp[u].size(); i++){
int v = mp[u][i];
dp[u][t] = min(dp[u][t], dfs(v, t + T + tim[v]) + cost[v]);
}
dp[u][t] = min(dp[u][t], dfs(u, t+tim[u]) + cost[u]);
return dp[u][t];
}
int main(){
memset(dp, -, sizeof(dp));
scanf("%d", &x);
scanf("%d%d%d", &n, &m, &T);
rep(i, , m) {
int u,v; scanf("%d%d", &u, &v);
mp[u].pb(v);
mp[v].pb(u);
}
rep(i, , n) {
scanf("%d%d", &tim[i], &cost[i]);
}
int ans = dfs(, tim[]) + cost[];
if(ans < inf) printf("%d\n", ans);
else puts("It is a trap.");
return ;
}

E.Perpetuum Mobile

找正环,由于是乘法,所以用log变成加法,还有这题其实是看爱因斯坦那句话,和他助手说的没什么关系,所以要找一个环,环上的乘积大于1,即log >0。于是用spfa判正环就行了。

#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert>
//#include <unordered_map>
/* ⊂_ヽ
  \\ Λ_Λ 来了老弟
   \('ㅅ')
    > ⌒ヽ
   /   へ\
   /  / \\
   レ ノ   ヽ_つ
  / /
  / /|
 ( (ヽ
 | |、\
 | 丿 \ ⌒)
 | |  ) /
'ノ )  Lノ */ using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define boost ios::sync_with_stdio(false);cin.tie(0)
#define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<;
const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = ;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} inline void cmax(int &x,int y){if(x<y)x=y;}
inline void cmax(ll &x,ll y){if(x<y)x=y;}
inline void cmin(int &x,int y){if(x>y)x=y;}
inline void cmin(ll &x,ll y){if(x>y)x=y;}
#define MODmul(a, b) ((a*b >= mod) ? ((a*b)%mod + 2*mod) : (a*b))
#define MODadd(a, b) ((a+b >= mod) ? ((a+b)%mod + 2*mod) : (a+b)) /*-----------------------showtime----------------------*/
typedef pair<int,double> pid;
const int maxn = 1e3+;
vector<pid>mp[maxn]; int vis[maxn];
double dis[maxn];
queue<int>que;
bool spfa(int u){
vis[u] = true;
for(int i=; i<mp[u].size(); i++){
pid tmp = mp[u][i];
int v = tmp.fi;
double w = tmp.se;
if(dis[v] < dis[u] + w){
dis[v] = dis[u] + w;
if(vis[v]) return true;
if(spfa(v)) return true;
}
}
vis[u] = false;
return false;
}
int main(){
int n,m;
scanf("%d%d", &n, &m);
rep(i, , m) {
int u,v; double e;
scanf("%d%d%lf", &u, &v, &e);
mp[u].pb(pid(v, log(e)));
}
for (int i = ; i <= n; ++i) if(spfa(i)) return *puts("inadmissible");
puts("admissible");
return ;
}

H - Ratatoskr

枚举每个点作为根节点的最大深度,如果这个节点原本就有乌鸦,就只能考虑松鼠所在子树的深度,如果这个节点原本没有乌鸦,就只用考虑这棵树的深度

#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert>
//#include <unordered_map>
/* ⊂_ヽ
  \\ Λ_Λ 来了老弟
   \('ㅅ')
    > ⌒ヽ
   /   へ\
   /  / \\
   レ ノ   ヽ_つ
  / /
  / /|
 ( (ヽ
 | |、\
 | 丿 \ ⌒)
 | |  ) /
'ノ )  Lノ */ using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define boost ios::sync_with_stdio(false);cin.tie(0)
#define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<;
const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = ;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} inline void cmax(int &x,int y){if(x<y)x=y;}
inline void cmax(ll &x,ll y){if(x<y)x=y;}
inline void cmin(int &x,int y){if(x>y)x=y;}
inline void cmin(ll &x,ll y){if(x>y)x=y;}
#define MODmul(a, b) ((a*b >= mod) ? ((a*b)%mod + 2*mod) : (a*b))
#define MODadd(a, b) ((a+b >= mod) ? ((a+b)%mod + 2*mod) : (a+b)) /*-----------------------showtime----------------------*/
const int maxn = ;
vector<int>mp[maxn];
int dp[maxn],vis[maxn];
int s,b1,b2,n;
void dfs(int u,int fa){
dp[u] = ;
if(u == s) vis[u] = ;
for(int i=; i<mp[u].size(); i++){
int v = mp[u][i];
if(v == fa) continue;
dfs(v, u);
dp[u] = max(dp[u], dp[v] + );
if(vis[v]) vis[u] = ;
}
}
int main(){
scanf("%d%d%d%d", &n, &s, &b1, &b2);
rep(i, , n-) {
int u,v; scanf("%d%d", &u, &v);
mp[u].pb(v); mp[v].pb(u);
}
int ans = inf;
for(int i=; i<=n; i++)
{
if(i == b1 || i ==b2){
memset(vis, , sizeof(vis));
dfs(i, -); for(int j=; j<mp[i].size(); j++) {
int v = mp[i][j];
if(vis[v]) ans = min(ans, dp[v]);
}
}
else {
dfs(i, -);
ans = min(ans,dp[i]);
}
}
printf("%d\n", ans);
return ;
}

J-Word Clock

状压搜索,注意dfs的次序,还有就是只要关了同步,就不能用scanf

#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> /* ⊂_ヽ
  \\ Λ_Λ 来了老弟
   \('ㅅ')
    > ⌒ヽ
   /   へ\
   /  / \\
   レ ノ   ヽ_つ
  / /
  / /|
 ( (ヽ
 | |、\
 | 丿 \ ⌒)
 | |  ) /
'ノ )  Lノ */ using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define boost ios::sync_with_stdio(false);cin.tie(0)
#define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<;
const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} inline void cmax(int &x,int y){if(x<y)x=y;}
inline void cmax(ll &x,ll y){if(x<y)x=y;}
inline void cmin(int &x,int y){if(x>y)x=y;}
inline void cmin(ll &x,ll y){if(x>y)x=y;} /*-----------------------showtime----------------------*/
int n,h,w;
string str[],t[];
pii dp[][<<],nxt[][<<];
int dis[][]; pii dfs(int u,int state){
if(dp[u][state].fi != - && dp[u][state].se != -) return dp[u][state];
if(state == (<<n) - ) return dp[u][state] = pii(,t[u].length());
pii ans = pii(inf, inf); for(int i=; i<=n; i++){
if(!(state & ( << (i-)))) {
pii tmp; tmp = dfs(i, state | (<<(i-))); int he,wi;
int cen = tmp.fi;
if(tmp.se + dis[u][i] > w) {
he = tmp.fi + ;
wi = t[u].length();
}
else {
he = tmp.fi;
wi = tmp.se + dis[u][i];
} if(he < ans.fi || (he==ans.fi && ans.se > wi)) {
ans.fi = he; ans.se = wi;
nxt[u][state] = pii(i, cen);
}
}
}
return dp[u][state] = ans;
}
bool cmp(string a,string b){
return a.length() < b.length();
}
vector<int> vec[];
int main(){
//scanf("%d%d%d", &h, &w, &n);
boost;
cin>>h>>w>>n;
memset(dis, , sizeof(dis));
memset(dp, -, sizeof(dp));
int sum = ; for(int i=; i<=n; i++) cin>>str[i],sum+=str[i].length();
sort(str+, str++n, cmp);
int tot = ;
for(int i=; i<=n; i++){
if(str[i].length() > w) {
puts("impossible");
return ;
}
int flag = ;
for(int j=i+; j<=n; j++){
if(str[j].find(str[i]) != string::npos) flag = ;
}
if(flag) t[++tot] = str[i];
}
n = tot; for(int i=; i<=n; i++) {
for(int j=; j<=n; j++){
int l1 = t[i].length(), l2 = t[j].length();
int len = min(l1, l2);
for(int k=; k<=len; k++){
if(t[i].substr(l1-k,k) ==t[j].substr(, k)) dis[i][j] = l1 - k;
}
}
} pii tmp = dfs(, );
// cout<<tmp.fi<<" , "<<tmp.se<<endl;
if(tmp.fi>h || tmp.se > w) return *puts("impossible"); int st = ,u = ;
while(st < (<<n) - ) {
int id = nxt[u][st].fi;
// cout<<id<<" "<<nxt[u][st].se<<endl;
vec[nxt[u][st].se].pb(id);
u = id;
st |= ( << (id - ));
} for(int i=; i<=h; i++) {
string ans = "";
for(int j=; j<vec[i].size(); j++){
string tmp = t[vec[i][j]];
int l1 = ans.length(),l2 = tmp.length();
int len = min(l1, l2),q;
for(int k=; k<=len; k++){
if(ans.substr(l1-k,k) ==tmp.substr(, k)) q = k;
}
for(int k=q; k<l2; k++) ans += tmp[k];
}
while(ans.length() < w) ans += "A";
cout<<ans<<endl;
} return ;
} /*
5 10 12
ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE TEN ELEVEN TWELVE
5 10 12
UNO DUE TRE QUATTRO CINQUE SEI SETTE OTTO NOVE DIECI UNDICI DODICI
*/

gym/101873/GCPC2017的更多相关文章

  1. gym 101873

    题还没补完 以下是牢骚:删了 现在只有六个...太恐怖了,我发现四星场我连300人的题都不会啊. C:最短路加一维状态就好了叭..嗯,一开始没看到输出的那句话 那个  "."也要输 ...

  2. Gym 101873F Plug It In(二分图匹配)

    题目链接:http://codeforces.com/gym/101873/problem/F 题意:有n个插孔,m个机器,和一个插板,一个插孔可以连接一个机器,插板可以使一个插孔连接三个机器,找到最 ...

  3. Gym 101873C - Joyride - [最短路变形][优先队列优化Dijkstra]

    题目链接:http://codeforces.com/gym/101873/problem/C 题意: 这是七月的又一个阳光灿烂的日子,你决定和你的小女儿一起度过快乐的一天.因为她真的很喜欢隔壁镇上的 ...

  4. Gym 101873D - Pants On Fire - [warshall算法求传递闭包]

    题目链接:http://codeforces.com/gym/101873/problem/D 题意: 给出 $n$ 个事实,表述为 "XXX are worse than YYY" ...

  5. Gym 101873G - Water Testing - [皮克定理]

    题目链接:http://codeforces.com/gym/101873/problem/G 题意: 在点阵上,给出 $N$ 个点的坐标(全部都是在格点上),将它们按顺序连接可以构成一个多边形,求该 ...

  6. Gym 101873I - Uberwatch - [DP]

    题目链接:http://codeforces.com/gym/101873/problem/I 题意: 给出 $n(1 \le n \le 300000)$ 个单位时间,每个单位时间给出一个 $x_i ...

  7. Gym 101873K - You Are Fired - [贪心水题]

    题目链接:http://codeforces.com/gym/101873/problem/K 题意: 现在给出 $n(1 \le n \le 1e4)$ 个员工,最多可以裁员 $k$ 人,名字为 $ ...

  8. codeforces gym #101873B. Buildings(Polya定理)

    参考博客: https://blog.csdn.net/liangzhaoyang1/article/details/72639208 题目链接: https://codeforces.com/gym ...

  9. Joyride (spaf)

    题目链接:https://codeforces.com/gym/101873/problem/C spaf的复杂度有点迷,按道理来说,一个简单的spaf在这题的复杂度是1e9,所以不敢写,然后用优先队 ...

随机推荐

  1. 角度转弧度&根据弧度计算圆周上点的坐标的方法

    角度转弧度: #define AngleToRadian(angle) (M_PI/180.0f)*angle 以正东面为0度起点计算指定角度所对应的圆周上的点的坐标: float radian = ...

  2. 并发栅栏CyclicBarrier---简单问2

    并发栅栏CyclicBarrier---简单问 背景:前几天在网上看到关于Java并发包java.concurrent中一个连环炮的面试题,整理下以备不时之需. CyclicBarrier简介: 栅栏 ...

  3. Linux内核OOM killer机制

    程序运行了一段时间,有个进程挂掉了,正常情况下进程不会主动挂掉,简单分析后认为可能是运行时某段时间内存占用过大,系统内存不足导致触发了Linux操作系统OOM killer机制,将运行中的进程杀掉了. ...

  4. S2:log4j

    配置步骤 1.引入jar,放到lib中,jar包被项目管理 2.在src目录下copy了一个文件log4j.properties 3.使用Logger   String word="会员登记 ...

  5. ajax具体实现学习记录

    记录自己对ajax\的理解, 首先要明白ajax是为了解决什么问题,简单来讲就是为了局部刷新页面,而不刷新整个界面.就比如现在有一个实时热度的显示,它是不断变化的,所以你肯定要不停的从数据库当中获取热 ...

  6. Linux打开网易云的问题

    网易云需要ROOT权限启动,期间终端不能关闭退出,否则网易云音乐会自动退出.    终端输入:sudo netease-cloud-music &u

  7. 微信公众平台注册及AppID和AppSecret的获取

    一.注册公众平台 1.入口 浏览器搜索“微信公众平台”,进入官网,点右上角立即注册. 2.选择账号类型 注册前需要选择一个账号类型,共有4个账号类型可以选择,每种类型能提供不同的功能,功能区别见下图. ...

  8. Spider & CrawlSpider

    CrawlSpide 最重要的是分析源码官方文档百度收搜 Spider document 就是官方文档了1.3的源码分析CrawlSpide 是爬取一个网站常用的规则 它是对spider进一步的包装 ...

  9. 洛谷 P3628 [APIO2010]特别行动队

    题意简述 将n个士兵分为若干组,每组连续,编号为i的士兵战斗力为xi 若i~j士兵为一组,该组初始战斗力为\( s = \sum\limits_{k = i}^{j}xk \),实际战斗力\(a * ...

  10. 100天搞定机器学习|day39 Tensorflow Keras手写数字识别

    提示:建议先看day36-38的内容 TensorFlow™ 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库.节点(Nodes)在图中表示数学操作,图中的线(edge ...