A Hanoi Tower 递归

题意:

大家都很熟悉汉诺塔的递归程序,现在给你一个组合,询问你这个组合是否会出现在汉诺塔的递归过程中。

题解:

将汉诺塔的递归程序反过来思考,考虑当前最大的那个盘,我们只会将他从from移动到to,他上面的盘都移动到tmp,那么这个最大的盘一定不会在tmp。如果这个盘在from,说明当前正在进行的过程是将他上面的盘从from移动到tmp,如果这个盘在to,那么说明当前正在进行的过程是将他上面的盘从tmp移动到to。递归下去即可。

代码:

#include<string>
#include<algorithm>
#include<fstream>
using namespace std; string s;
int n;
bool dfs(char from,char to,char tmp,int i) {
if (i == n)return true;
if (s[i] == tmp)return false;
if (s[i] == from)return dfs(from, tmp, to, i + );
if (s[i] == to)return dfs(tmp, to, from, i + );
} int main() {
ifstream cin("Input.txt");
ofstream cout("Output.txt");
cin.sync_with_stdio(false);
cin >> n;
cin >> s;
reverse(s.begin(), s.end());
if (s[] == 'C') {
cout << "NO" << endl;
return ;
}
if (dfs('A', 'B', 'C', ))
cout << "YES" << endl;
else
cout << "NO" << endl;
return ;
}

B Island 模拟

题意:

给你一个加减号组成的图,问你有多少加号与减号相邻。

题解:

直接模拟就好了。

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <math.h>
#define INF 0x3f3f3f3f
#define pi 3.141592654 using namespace std; char str[][];
int n,m; int dx[]={,,,-},dy[]={,-,,}; bool canmove(int x,int y){
return x >= && x < n && y >= && y < m;
} int main() {
freopen("Input.txt", "r", stdin);
freopen("Output.txt", "w", stdout);
while(scanf("%d %d",&n,&m) != EOF){
for(int i=;i<n;i++)
scanf("%s",str[i]);
int ans = ;
for(int i=;i<n;i++) for(int j=;j<m;j++) if(str[i][j] == '+'){
for(int k=;k<;k++){
int nx = i + dx[k];
int ny = j + dy[k];
if(canmove(nx,ny) && str[nx][ny] != '+'){
ans++;
// printf("%d %d\n", i, j);
break;
}
}
}
printf("%d\n",ans);
}
return ;
}

C Sequence 打表找规律

题意:

给你一个序列生成器,定义$a[0]=1$,对于$i>0$,$a[i]=sort(a[i-1]*2)$,其中sort是按每一位排序,这个序列的前几项是1,2,4,8,16,23,46,29,58....

现在问你第n项是多少。

题解:

就把序列放到OEIS上,然后就知道规律了。

代码:

#include<cstring>
#include<cstdio>
#include<fstream>
#define MAX_N 50
using namespace std; long long n; long long a[MAX_N]={, , , , , , , , , , , , , , , , , , , , , , , , , , , , , }; int main() {
ifstream cin("Input.txt");
ofstream cout("Output.txt");
cin >> n;
n--;
if (n < )cout << a[n] << endl;
else cout << a[(n % ) + ] << endl;
return ;
}

H Milestones 主席树

题意:

给你一个序列,然后若干次询问,询问从L到R之间有多少不同的值。

题解:

就。。这题是队友做的,貌似就建若干棵主席树,然后搞搞。

代码:

#include <stdio.h>
#include <algorithm>
#include <cstring>
#define maxn 10010 using namespace std; int tot, a[maxn], cnt; struct N { int ls, rs, w; } tree[ * maxn];
int roots[maxn], las[]; int build_tree(int l, int r) {
int newnode = tot++;
tree[newnode].w = ;
if (l != r) {
int mid = (l + r) / ;
tree[newnode].ls = build_tree(l, mid);
tree[newnode].rs = build_tree(mid + , r);
}
return newnode;
} int updata(int rt, int pos, int val) {
int newnode = tot++, tmp = newnode;
tree[newnode].w = tree[rt].w + val;
int l = , r = cnt;
while (l < r) {
int mid = (l + r) / ;
if (pos <= mid) {
tree[newnode].ls = tot++;
tree[newnode].rs = tree[rt].rs;
newnode = tree[newnode].ls;
rt = tree[rt].ls;
r = mid;
}
else {
tree[newnode].ls = tree[rt].ls;
tree[newnode].rs = tot++;
newnode = tree[newnode].rs;
rt = tree[rt].rs;
l = mid + ;
}
tree[newnode].w = tree[rt].w + val;
}
return tmp;
} int query(int rt, int k) {
int l = ,r = cnt;
int ans = ;
if(k == ) return tree[rt].w;
while(l < r){
int mid = (l + r) / ;
if(k <= mid){
ans += tree[tree[rt].rs].w;
rt = tree[rt].ls;
r = mid;
}
else{
rt = tree[rt].rs;
l = mid + ;
}
}
return ans;
} int n, q; void print(int rt, int l = , int r = cnt) {
printf("l = %d r = %d w = %d\n", l, r, tree[rt].w);
if (l != r) {
int mid = (l + r) / ;
print(tree[rt].ls, l, mid);
print(tree[rt].rs, mid + , r);
}
} int main() {
freopen("Input.txt", "r", stdin);
freopen("Output.txt", "w", stdout);
while (scanf("%d %d", &n, &q) != EOF) {
for (int i = ; i <= n; i++)
scanf("%d", &a[i]);
cnt = n;
tot = ;
memset(las, -, sizeof(las));
roots[] = build_tree(, cnt);
for (int i = ; i <= n; i++) {
if (las[a[i]] == -) {
roots[i] = updata(roots[i - ], i, );
}
else {
roots[i] = updata(roots[i - ], las[a[i]], -);
roots[i] = updata(roots[i], i, );
}
las[a[i]] = i;
}
int l, r;
while (q--) {
scanf("%d %d", &l, &r);
printf("%d\n", query(roots[r], l - ));
}
}
return ;
}

J Computer Network Tarjan+dp

题意:

给你一个图,问你加一条边,使得图的桥的数量减少到最少,输出这条边。

题解:

就Tarjan一发,令桥的权值是1,非桥的权值是0,在Tarjan树上寻找直径即可,整个过程可以在Tarjan的时候dp来完成。令dp[i]表示节点i到其子树的某个叶子的最长路径,然后维护每个点dp的最大和次大值,更新答案即可。

代码:

//#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
#include<set>
#define MAX_N 100004
#define MAX_M 100005
using namespace std; int dfn[MAX_N],low[MAX_N],ind=;
bool vis[MAX_N]; int dp[MAX_N];
int pre[MAX_N];
int ans = -;
int x,y; struct riGou {
public:
int u, v, id; riGou(int uu, int vv, int i) : u(uu), v(vv), id(i) { } riGou() { } bool operator<(const riGou &a) const {
if (u == a.u)
return v < a.v;
return u < a.u;
}
}; struct edge {
public:
int to, id; edge(int t, int i) : to(t), id(i) { } edge() { }
}; vector<edge> G[MAX_N]; set<riGou> se;
bool haSame[MAX_N]; void Tarjan(int u,int p) {
dfn[u] = low[u] = ++ind;
vis[u] = ;
int a = , b = ;
int nx = u, ny = u;
for (int i = ; i < G[u].size(); i++) {
int v = G[u][i].to;
if (v == p)continue;
if (!vis[v]) {
Tarjan(v, u);
low[u] = min(low[u], low[v]);
int ib = ;
if (low[v] > dfn[u] && haSame[G[u][i].id] == )
ib = ;
if (a <= dp[v] + ib)b = a, a = dp[v] + ib, ny = nx, nx = pre[v];
else if (b <= dp[v] + ib)b = dp[v] + ib, ny = pre[v];
}
else
low[u] = min(dfn[v], low[u]);
}
dp[u] = a;
pre[u] = nx;
if (ans < a + b) {
x = nx, y = ny;
ans = a + b;
}
} int n,m; int main() {
ifstream cin("Input.txt");
ofstream cout("Output.txt");
cin.sync_with_stdio(false);
cin >> n >> m;
for (int i = ; i < m; i++) {
int u, v;
cin >> u >> v;
riGou a(u, v, i);
riGou b(v, u, i);
auto it = se.find(a);
if (it != se.end()) {
haSame[it->id] = ;
continue;
}
it = se.find(b);
if (it != se.end()) {
haSame[it->id] = ;
continue;
}
G[u].push_back(edge(v, i));
G[v].push_back(edge(u, i));
se.insert(riGou(u, v, i));
}
Tarjan(, );
if (x == y)
cout << << " " << n << endl;
else
cout << x << " " << y << endl;
//cout << ans << endl;
return ;
}

2012-2013 ACM-ICPC, NEERC, Central Subregional Contest的更多相关文章

  1. 2018-2019 ICPC, NEERC, Southern Subregional Contest

    目录 2018-2019 ICPC, NEERC, Southern Subregional Contest (Codeforces 1070) A.Find a Number(BFS) C.Clou ...

  2. Codeforces 2018-2019 ICPC, NEERC, Southern Subregional Contest

    2018-2019 ICPC, NEERC, Southern Subregional Contest 闲谈: 被操哥和男神带飞的一场ACM,第一把做了这么多题,荣幸成为7题队,虽然比赛的时候频频出锅 ...

  3. 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror) Solution

    从这里开始 题目列表 瞎扯 Problem A Find a Number Problem B Berkomnadzor Problem C Cloud Computing Problem D Gar ...

  4. Codeforces1070 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)总结

    第一次打ACM比赛,和yyf两个人一起搞事情 感觉被两个学长队暴打的好惨啊 然后我一直做傻子题,yyf一直在切神仙题 然后放一波题解(部分) A. Find a Number LINK 题目大意 给你 ...

  5. codeforce1070 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred) 题解

    秉承ACM团队合作的思想懒,这篇blog只有部分题解,剩余的请前往星感大神Star_Feel的blog食用(表示男神汉克斯更懒不屑于写我们分别代写了下...) C. Cloud Computing 扫 ...

  6. 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)

    A. Find a Number 找到一个树,可以被d整除,且数字和为s 记忆化搜索 static class S{ int mod,s; String str; public S(int mod, ...

  7. 2018.10.20 2018-2019 ICPC,NEERC,Southern Subregional Contest(Online Mirror, ACM-ICPC Rules)

    i207M的“怕不是一个小时就要弃疗的flag”并没有生效,这次居然写到了最后,好评=.= 然而可能是退役前和i207M的最后一场比赛了TAT 不过打得真的好爽啊QAQ 最终结果: 看见那几个罚时没, ...

  8. 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred) Solution

    A. Find a Number Solved By 2017212212083 题意:$找一个最小的n使得n % d == 0 并且 n 的每一位数字加起来之和为s$ 思路: 定义一个二元组$< ...

  9. 2012-2013 ACM-ICPC, NEERC, Central Subregional Contest J Computer Network1 (缩点+最远点对)

    题意:在连通图中,求一条边使得加入这条边以后的消除的桥尽量多. 在同一个边双连通分量内加边肯定不会消除桥的, 求边双连通分量以后缩点,把桥当成边,实际上是要选一条最长的链. 缩点以后会形成一颗树,一定 ...

  10. 2012-2013 ACM-ICPC, NEERC, Central Subregional Contest H Milestones1 (暴力)

    预处理+暴力,每个颜色都是独立的,求个前缀和,减一减判断一个在区间内颜色是否存在. 算了算复杂度好像有点勉强,但是还是过了,学了主席树以后用主席树在做一下 #include<bits/stdc+ ...

随机推荐

  1. Mac远程访问Ubuntu

    MacOS和Ubuntu连接到同一个网络使用ping命令可以通信即可.SSH使用SSH可以很方便的在MacOS上访问Ubuntu,不过只能用命令行操作,相当于连接了Ubuntu的终端. 1. Ubun ...

  2. python爬虫基础17-抓包工具使用

    01 抓包工具原理 HTTP 由于HTTP请求是没有加密的,也没有做任何验证,所以抓包工具直接将请求转发即可. HTTPS 由于HTTPS请求,客户端会使用服务端的证书来加密数据,而且会验证服务端是否 ...

  3. Linux入门学习笔记1:VI常用命令

    常用命令 yy 复制 p 黏贴 shift+v 多行选中 shift+ctrl+< 左移 shift+ctrl+> 右移 ndd 删除光标所在行及其后n-1行 i 进入编辑状态 esc 退 ...

  4. sqli-labs less1 &&less3&&less4学习心得

    0x01.less1 id=1/ id=1 and 1=1结果正常 id=1 and 1=2结果正常,不合理 id=1'提示:

  5. ACM-ICPC 2017 Asia Urumqi A. Coins

    Alice and Bob are playing a simple game. They line up a row of n identical coins, all with the heads ...

  6. German Collegiate Programming Contest 2018​

    // Coolest Ski Route #include <iostream> #include <cstdio> #include <cstring> #inc ...

  7. LayoutInflater的用法

    Instantiates a layout XML file into its corresponding View objects. It is never used directly. Inste ...

  8. layui的upload组件使用以及上传阻止测试

    背景:页面上一个按钮,点击弹出上传框,从按钮的方法代码开始写:处理未选择文件阻止上传:通过判断选择文件的数量,显示或隐藏上传按钮: 在js中定义: function  uploadFile(){ la ...

  9. hibernate源码分析1-保存一个对象

    要点 用了event的方式贯穿CRUD的过程 值得学习 用dynamic-insert 支持 插入时 可选 全字段插入 还是仅仅有值的字段插入. 返回主键的值 用了 Serializable 类型作为 ...

  10. datatable 修改点击列头进行排序顺序

    一般点击排序时,是先升序后降序 可以通过如下代码修改排序规则 jQuery(function ($) { $(".datatable").dataTable({ "pag ...