ACM俱乐部算法基础练习赛(1)
A:
水题
代码:
#include<cstdio>
#include<algorithm>
using namespace std; int she[]; int n,m,c; int main()
{
int ca=,a;
while(scanf("%d%d%d",&n,&m,&c)&&(n+m+c))
{
printf("Sequence %d\n",ca++);
for(int i=; i<=n; i++)
scanf("%d",&she[i]);
int ans=,ma=;
while(m--)
{
scanf("%d",&a);
ans+=she[a];
ma=max(ma,ans);
she[a]=-she[a];
}
if(ma>c)puts("Fuse was blown.");
else
{
puts("Fuse was not blown.");
printf("Maximal power consumption was %d amperes.\n",ma);
}
puts("");
}
return ;
}
B:
模拟;
代码:
#include<cstdio>
#include<cstring>
#include<queue>
#define maxn 55
using namespace std; bool map[maxn][maxn]; int n,flag; struct node
{
int x,y;
}; queue<node>q; void move(char s)
{
node cur=q.back();
node next=q.front();
q.pop();
map[next.x][next.y]=;
if(s=='E')
{
next.x=cur.x;
next.y=cur.y+;
}
else if(s=='N')
{
next.x=cur.x-;
next.y=cur.y;
}
else if(s=='W')
{
next.x=cur.x;
next.y=cur.y-;
}
else if(s=='S')
{
next.x=cur.x+;
next.y=cur.y;
}
if(map[next.x][next.y]==)
{
flag=;
return;
}
else if(next.x<=||next.x>||next.y<=||next.y>)
{
flag=-;
return;
}
else
{
q.push(next);
map[next.x][next.y]=;
}
} char s[];
int main()
{
while(scanf("%d",&n)&&n)
{
node no;
flag=;
memset(map,,sizeof map);
while(!q.empty())q.pop();
for(int i=; i<=; i++)
{
no.x=;
no.y=i;
map[][i]=;
q.push(no);
}
scanf("%s",s);
int i;
for(i=; i<n; i++)
{
move(s[i]);
if(flag<)break;
}
if(flag==)printf("The worm successfully made all %d moves.\n",n);
else if(flag==-)printf("The worm ran off the board on move %d.\n",i+);
else if(flag==)printf("The worm ran into itself on move %d.\n",i+);
}
return ;
}
C:
我用的是bfs;
代码:
#include<cstdio>
#include<queue>
#include<vector>
#define maxn 10005
using namespace std; vector<int>ve[maxn];
int di[maxn];
queue<int>q;
int main()
{
int n,x;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&x);
ve[x].push_back(i);
}
int l=ve[].size();
for(int i=;i<l;i++)
{
q.push(ve[][i]);
di[ve[][i]]=;
}
while(!q.empty())
{
int cur=q.front();
q.pop();
l=ve[cur].size();
for(int i=;i<l;i++)
{
q.push(ve[cur][i]);
di[ve[cur][i]]=di[cur]+;
}
}
int ans=;
for(int i=;i<=n;i++)
if(ans<di[i])ans=di[i];
printf("%d\n",ans);
}
D:
dfs;
我先用一个表来存,一直都是T;
没想到用状态压缩就可以过了;
代码:
#include<cstdio>
#include<vector>
#define maxn 23
using namespace std; vector<int>ve[maxn];
int n,s,m; bool dfs(int x,int t)
{
if(t==s)return x==n-;
int l=ve[x].size();
for(int i=;i<l;i++)
{
int a=ve[x][i];
if(~(t>>a)&)
if(dfs(a,t|(<<a)))
return ;
}
return ;
} int main()
{
int x,y;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<n;i++)
ve[i].clear();
for(int i=;i<m;i++)
{
scanf("%d%d",&x,&y);
x--,y--;
ve[x].push_back(y);
ve[y].push_back(x);
}
s=(<<n)-;
if(dfs(,))puts("");
else puts("");
}
return ;
}
ACM俱乐部算法基础练习赛(1)的更多相关文章
- Levenberg-Marquardt算法基础知识
Levenberg-Marquardt算法基础知识 (2013-01-07 16:56:17) 转载▼ 什么是最优化?Levenberg-Marquardt算法是最优化算法中的一种.最优化是寻找使 ...
- acm常见算法及例题
转自:http://blog.csdn.net/hengjie2009/article/details/7540135 acm常见算法及例题 初期:一.基本算法: (1)枚举. (poj17 ...
- ACM常用算法及练习(2)
ACM常用算法及练习 知识类型 重要度 容易度 应掌握度 典型题 其他 数据结构(5) 链表 ★★☆ ★★★ ★★☆ 栈 stack ★★★ ★★★ ★★★ HLoj120 ...
- ACM常用算法及练习(1)
ACM常用算法及练习 第一阶段:练经典常用算法,下面的每个算法给我打上十到二十遍,同时自己精简代码,因为太常用,所以要练到写时不用想,10-15分钟内打完,甚至关掉显示器都可以把程序打出来. 1.最短 ...
- 解读Raft(一 算法基础)
最近工作中讨论到了Raft协议相关的一些问题,正好之前读过多次Raft协议的那paper,所以趁着讨论做一次总结整理. 我会将Raft协议拆成四个部分去总结: 算法基础 选举和日志复制 安全性 节点变 ...
- 腾讯2017年暑期实习生编程题【算法基础-字符移位】(C++,Python)
算法基础-字符移位 时间限制:1秒 空间限制:32768K 题目: 小Q最近遇到了一个难题:把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,且不能申请额外的空间. 你能帮帮小Q吗? ...
- 算法基础_递归_求杨辉三角第m行第n个数字
问题描述: 算法基础_递归_求杨辉三角第m行第n个数字(m,n都从0开始) 解题源代码(这里打印出的是杨辉三角某一层的所有数字,没用大数,所以有上限,这里只写基本逻辑,要符合题意的话,把循环去掉就好) ...
- ACM主要算法
ACM主要算法ACM主要算法介绍 初期篇 一.基本算法(1)枚举(poj1753, poj2965)(2)贪心(poj1328, poj2109, poj2586)(3)递归和分治法(4)递推(5)构 ...
- 毕业设计预习:SM3密码杂凑算法基础学习
SM3密码杂凑算法基础学习 术语与定义 1 比特串bit string 由0和1组成的二进制数字序列. 2 大端big-endian 数据在内存中的一种表示格式,规定左边为高有效位,右边为低有效位.数 ...
随机推荐
- 使用摘要流获取文件的MD5
摘要流是过滤流的一种,使用它可以再读取和写入流时获取流的摘要信息(MD5/SHA). 使用摘要流包装流时,需要额外传递一个MessageDigest对象, MessageDigest md=Messa ...
- 2016iweb峰会参会总结
2016年8月27日去国家会议中心参加iweb峰会. 8点半开始签到入场,8点20分排队签到的人已经排到另一个门口,人超级多啊. 9点一如既往的由h5女神娜姐开场. 上午场 基本是各公司的大佬们介绍各 ...
- air2调用本地exe的文章
流传了两种配置app.xml的方法,分别是: <supportedProfiles>extendedDesktop</supportedProfiles> <suppo ...
- install lua client for redis-server on Mac
1. lua client library for redis-server https://github.com/nrk/redis-lua 2. dependent luasocket https ...
- HTTP请求、响应报文格式
HTTP请求报文格式: HTTP请求报文主要由请求行.请求头部.空行以及请求正文4部分组成 1,请求行由3部分组成,分别为:请求方式,URI(注意这里不是URL)以及协议版本组成,之间由空格分隔 请求 ...
- c中计时的几种方法
C计时的几种方法说明及例程 1. 使用clock() 函数 头文件:<time.h> clock()函数,返回“自程序启动到调用该函数,CPU时钟的计时单元数(clock tick)” 每 ...
- 解析包时出现错误,用代码安装apk出现问题
Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file),"appli ...
- 利用Merge生成或更新新记录
-- ============================================= -- Author: <华仔> -- Create date: <2016,6,7& ...
- Ext.Net学习笔记02:Ext.Net用法概览
这两天越来越觉得Ext.Net很强大,如果运用熟练可以极大的提高编程效率.如果你也要学习Ext.Net,推荐你看一下<Ext.Net Web 应用程序开发教程>. Ext.Net与ExtJ ...
- XMPP适配IPV6 (GCDAsyncSocket适配IPV6)
苹果公司要求在6月1号之后上架Appstore的应用必须通过ipv6兼容测试. 最近到了八月份,开始发现新上架的app没有通过,查看了下原因,说没有适配IPV6. 首先在本地搭建一个IPV6的测试环境 ...