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 ...
随机推荐
- Bluetooth Baseband介绍
目录 1. 概述 1.1 Clock(时钟) 1.2 寻址方式 2. 物理信道(Physical Channels) 3. 物理链路(Physical Links) 4. 逻辑传输层(Logical ...
- NSString的常见方法
//1.创建常量字符串. NSString *astring = @"This is a String!"; //2.创建空字符串,给予赋值. NSString *astrin ...
- 點擊按鈕后彈出新頁面導致原頁面CSS失效
比方说在页面里面有个LinkButton,要点击以后要打开新窗口,而且新窗口的URL是根据用户选择结果动态产生的.LinkButton的代码这样写: protected void Service ...
- ADB not responding. If you'd like to retry, then please manually kill "adb.exe" and click 'Restart'
ADB not responding. If you'd like to retry, then please manually kill "adb.exe" and click ...
- javascript 模式方面的学习
看了好多网上的文章,基本上得到一个结论:一些写类工具函数或框架的写类方式本质上都是 构造函数+原型 1.用构造函数来定义类属性(字段).2.用原型方式来定义类的方法. 具体文章请参阅 JavaScri ...
- Gulp自动化工具之图片压缩
一.安装node https://nodejs.org/download/ 根据需要选择对应的版本 安装好了之后可以通过node -v参看一下版本 node -v 二.安装gulp npm insta ...
- curl get post 数据
1.get方式传值 function testGet(){ $ch = curl_init (); //初始化一个cURL会话 $url = "127.0.0.1/testPage?test ...
- Installing OpenCV 2.4.10 in Ubuntu 12.04 LTS
转自 http://www.samontab.com/web/2012/06/installing-opencv-2-4-1-ubuntu-12-04-lts/ EDIT: I published a ...
- 使用Aspose.Cell控件实现Excel高难度报表的生成(三)
在之前几篇文章中,介绍了关于Apsose.cell这个强大的Excel操作控件的使用,相关文章如下: 使用Aspose.Cell控件实现Excel高难度报表的生成(一) 使用Aspose.Cell控件 ...
- SQLPlus Error handle
SQLPlus directive "WHENEVER SQLERROR EXIT 1" will return a specified code when any SQL err ...