Codeforces Round #288 (Div. 2)
A. Pasha and Pixels
题意就是给一个n*m的矩阵,k次操作,一开始矩阵全白,一次操作可以染黑一个格子,问第几次操作可以使得矩阵中存在一个2*2的黑色矩阵。直接模拟即可
代码:
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstring>
#include<vector>
#include<cmath>
#include<string>
#define N 100010
#define M 1010
#define P 1000000007
using namespace std;
int n,m,k,i,a[N],b[N],f[M][M];
int check(int x,int xx,int y,int yy)
{
int w=;
w=f[x][y]+f[x][yy]+f[xx][y]+f[xx][yy];
if (w==) return ;else return ;
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
for (i=;i<=k;i++)
scanf("%d%d",&a[i],&b[i]);
for (i=;i<=k;i++)
{
f[a[i]][b[i]]=;
if (check(a[i]-,a[i],b[i]-,b[i])) break;
if (check(a[i]-,a[i],b[i],b[i]+)) break;
if (check(a[i],a[i]+,b[i]-,b[i])) break;
if (check(a[i],a[i]+,b[i],b[i]+)) break;
}
if (i<=k)
printf("%d",i);
else
printf("");
}
B. Anton and currency you all know
题意是给一个奇数,你可以交换其中两位,使得其变成一个偶数,并且要求这个偶数尽可能大。由于给的数字是奇数,因此必然是个位数和一个其他位上的数交换,并且交换的这个数得是偶数,如果数字全为奇数那明显不可以。如果交换的这个数大于个位,那么交换以后的数明显会比原数大,因此尽可能的选取高位的数字,使得其比个位数大,那么直接交换即可。若不存在交换的数比个位要大,说明交换后的数字一定会比原数小,那么则应选取一个位数最小的偶数,和个位交换。
代码:
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstring>
#include<vector>
#include<cmath>
#include<string>
#define N 100010
#define M 1010
#define P 1000000007
using namespace std;
char s[N],t;
int tmp,len,i;
int main()
{
tmp=-;
scanf("%s",s);
len=strlen(s);
for (i=;i<len-;i++)
if (s[i]%==)
{
if (s[len-]>s[i]) break;
tmp=i;
}
if (i<len-)
{
t=s[i];s[i]=s[len-];s[len-]=t;
}
else
if (tmp!=-)
{
t=s[tmp];s[tmp]=s[len-];s[len-]=t;
}
else
{
printf("-1");
return ;
}
printf("%s",s);
}
C. Anya and Ghosts
首先需要注意这一题是允许蜡烛在0时之前点的,一时刻只能点亮一根蜡烛。要使蜡烛使用的最少,明显可以采取贪心的做法,能不放则不放,如果到了指定时刻蜡烛数目没有要求的数目,那么在放。
代码:
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstring>
#include<vector>
#include<cmath>
#include<string>
#define N 100010
#define M 1010
#define P 1000000007
using namespace std;
int m,t,r,i,j,L,R,v[N],w[N];
map<int,int>ma;
int main()
{
scanf("%d%d%d",&m,&t,&r);
for (i=;i<=m;i++)
scanf("%d",&w[i]);
L=;R=;
for (i=;i<=m;i++)
{
while ((L<=R)&&(v[L]<w[i])) L++;
if (R-L+<r)
for (j=r-(R-L+);j>=;j--)
{
R++;
if (ma[w[i]-j+t]==)
{
printf("-1");
return ;
}
ma[w[i]-j+t]=;
v[R]=w[i]-j+t;
if (v[R]<w[i])
{
printf("-1");
return ;
}
}
}
printf("%d",R);
}
D. Tanya and Password
这一题可以转换成求一个欧拉通路,具体的做法每个串都转化成一条边,例如"abc"这个串,可以转换成"ab"->"bc"。也就是前两个字母视为一个节点,后两个字母视为另一个节点,然后连一条有向边。
若有向图中存在一条欧拉通路,则(1)图联通(2)全部点的入度都等于出度或者有一个点入度=出度+1,并且还有一个点出度=入度+1。
代码:
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstring>
#include<vector>
#include<cmath>
#include<string>
#include<iostream>
#define N 500010
#define M 1010
#define P 1000000007
using namespace std;
map<string,int> ma;
map<int,string> o;
string s,s1,s2;
int n,i,a,b;
int p[N],tt[N],pre[N],dp,tot,rd[N],cd[N],cnt1,cnt2,st,ed,f[N];
char ans[N];
void link(int x,int y)
{
dp++;pre[dp]=p[x];p[x]=dp;tt[dp]=y;
}
int gf(int x)
{
int p,t;
p=x;
while (p!=f[p])p=f[p];
while (x!=t)
{
t=f[x];
f[x]=p;
x=t;
}
return p;
}
void dfs(int x)
{
int i,tmp=;
i=p[x];
while (i)
{
p[x]=pre[i];
dfs(tt[i]);
i=p[x];
}
tot++;ans[tot]=o[x][];
}
int main()
{
scanf("%d",&n);
for (i=;i<=n;i++)
{
cin>>s;
s1="";
s1=s1+s[]+s[];
s2="";
s2=s2+s[]+s[];
if (ma[s1]==)
{
tot++;
ma[s1]=tot;
o[tot]=s1;
f[tot]=tot;
}
if (ma[s2]==)
{
tot++;
ma[s2]=tot;
o[tot]=s2;
f[tot]=tot;
}
a=ma[s1];
b=ma[s2];
link(a,b);
rd[b]++;cd[a]++;
f[gf(a)]=gf(b);
}
for (i=;i<=tot;i++)
if (gf(i)!=gf())
{
printf("NO");
return ;
}
st=;
for (i=;i<=tot;i++)
{
if (cd[i]-rd[i]==) {
st=i;
cnt1++;
}
else
if (rd[i]-cd[i]==)
cnt2++;
else
if (cd[i]-rd[i]!=)
{
printf("NO");
return ;
}
}
if ((cnt1+cnt2==)||(cnt1*cnt2==))
{
printf("YES\n");
printf("%c",o[st][]);
tot=;
dfs(st);
for (i=tot;i>=;i--)
printf("%c",ans[i]);
}
else
printf("NO");
}
E. Arthur and Brackets
题意是给一个n,表示有n个左括号,n个右括号,构成长度为2n的括号序列,接下来第i行给的L[i]和R[i]表示从左往右第i个左括号的对应的右括号与他的距离范围(注意给的是距离范围而不是在序列中的实际位置范围),问是否存在一个合法的括号序列,如果存在,那么随意输出一个。
做法是dp,f[i][j]表示是否存在从第i对括号到第j对括号组成的合法括号序列,dp方程有两种情况
(1)假设i<=k<j,如果f[i][k]=1,f[k+1][j]=1,那么很明显f[i][j]=1,因为如果两个序列是合法的,那么他们左右拼接明显也是合法的。
(2)如果f[i+1][j]=1,那么如果要加上第i对括号后也合法,那么第i个左括号和其所对应的右括号的距离应该是(j-i)*2+1,如果这个距离在其的距离范围内,那么则可行,否则不存在这种情况。
复杂度O(n^3)
代码:
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstring>
#include<vector>
#include<cmath>
#include<string>
#define N 100010
#define M 1010
#define P 1000000007
using namespace std;
int tot,f[M][M],l[M],r[M],i,j,k,L,R,z,n;
char s[N];
void dfs(int l,int r)
{
int i;
if (l>r) return;
for (i=l;i<=r-;i++)
if ((f[l][i])&&(f[i+][r]))
{
dfs(l,i);
dfs(i+,r);
return;
}
if (f[l+][r])
{
s[tot]='(';tot++;
dfs(l+,r);
s[tot]=')';tot++;
} }
int main()
{
scanf("%d",&n);
for (i=;i<=n;i++)
f[i+][i]=;
for (i=;i<=n;i++)
scanf("%d%d",&l[i],&r[i]);
for (i=;i<=n;i++)
for (j=;j<=n-i+;j++)
{
L=j;R=j+i-;
for (k=L;k<=R-;k++)
f[L][R]=(f[L][R]|(f[L][k]&f[k+][R]));
if ((f[L+][R])&&(l[j]<=*i-)&&(*i-<=r[j]))
z=;
else
z=;
f[L][R]=(f[L][R]|z);
}
if (f[][n]==)
printf("IMPOSSIBLE");
else
{
dfs(,n);
printf("%s",s);
}
}
Codeforces Round #288 (Div. 2)的更多相关文章
- 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts
题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...
- 贪心 Codeforces Round #288 (Div. 2) B. Anton and currency you all know
题目传送门 /* 题意:从前面找一个数字和末尾数字调换使得变成偶数且为最大 贪心:考虑两种情况:1. 有偶数且比末尾数字大(flag标记):2. 有偶数但都比末尾数字小(x位置标记) 仿照别人写的,再 ...
- Codeforces Round #288 (Div. 2) E. Arthur and Brackets
题目链接:http://codeforces.com/contest/508/problem/E 输入一个n,表示有这么多对括号,然后有n行,每行输入一个区间,第i行的区间表示从前往后第i对括号的左括 ...
- Codeforces Round #288 (Div. 2)D. Tanya and Password 欧拉通路
D. Tanya and Password Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/508 ...
- 欧拉回路/通路 Codeforces Round #288 (Div. 2)
http://codeforces.com/contest/508/problem/D 以上是题目链接 题目大意 给n个字符串看能不能链接在一起 因为 三个三个分割 所以字符串 如abc ab作为起点 ...
- 模拟 Codeforces Round #288 (Div. 2) A. Pasha and Pixels
题目传送门 /* 模拟水题:给定n*m的空白方格,k次涂色,将(x,y)处的涂成黑色,判断第几次能形成2*2的黑色方格,若不能,输出0 很挫的判断四个方向是否OK */ #include <cs ...
- Codeforces Round #288 (Div. 2) C. Anya and Ghosts 模拟
C. Anya and Ghosts time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #288 (Div. 2) E. Arthur and Brackets 贪心
E. Arthur and Brackets time limit per test 2 seconds memory limit per test 128 megabytes input stand ...
- Codeforces Round #288 (Div. 2) C. Anya and Ghosts 模拟 贪心
C. Anya and Ghosts time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- Visual Studio 2010扩展让JS与CSS实现折叠
在Visaul Studio 2010中写js或css代码,缺少像写C#代码时的那种折叠功能,当代码比较多时,就很不方便. 今天发现,已经有VS2010扩展支持这个功能,它就是——JSEnhancem ...
- 【转】MySQL USE NAMES 'UTF8'
先说MySQL的字符集问题.Windows下可通过修改my.ini内的 # CLIENT SECTION [mysql] default-character-set=utf8 # SERVER SEC ...
- 20145211 《Java程序设计》实验报告三:敏捷开发与XP实践
实验内容 使用 git上传代码 使用 git相互更改代码 实现代码的重载 XP基础 XP核心实践 相关工具 一.git上传代码 这一部分是与我的partner合作的,详见他的博客- 20145326蔡 ...
- Magento Table Rate运费国家代码汇总
Magento Table Rate是三种内置未调用第三方API运费方式中最强大的一个.通过设置国家,区域,邮编,价格来划分不同的运费等级.该方式基本能够满足轻量级的B2C商城的运费模式.这里收集下国 ...
- JavaScript学习基础部分
JavaScript学习基础 一.简介 1.JavaScript 是因特网上最流行的脚本语言,并且可在所有主要的浏览器中运行,比方说 Internet Explorer. Mozilla.Firefo ...
- [转]ANDROID L——Material Design详解(动画篇)
转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 转自:http://blog.csdn.net/a396901990/article/de ...
- ASINetworkQueue 队列下载
我们通过一个例子介绍一下请求队列使用,我们设计了一个应用,用户点击GO按钮从服务器同时下载两张图片显示在画面中. 我们直接看看主视图控制器ViewController.h代码如下: #import “ ...
- Python-S13-day2-之购物车
Python-S13-day2 需求: 1.写一个购物小程序,用户开始先输入自己的工资,然后可以不断的买东西并加入购物车,如果钱不够了提示余额不足,用户中途可以选择查看自己购物车里面的商品,以及余额, ...
- Objective-C中 Self和 Super详解
Objective-C中 Self和 Super详解 Objective-C 中Self 和 Super 详解本文要介绍的内容,在 Objective-C 中的类实现中经常看到这两个关键字 self ...
- 使用sh-x调试shell脚本_转
参考:http://blog.chinaunix.net/uid-20564848-id-73502.html 1. 通过sh -x 脚本名 #显示脚本执行过程2.脚本里set -x选项,轻松跟踪调 ...