Codeforces711


A
ZS the Coder and Chris the Baboon are travelling to Udayland! To get there, they have to get on the special IOI bus. The IOI bus has nrows of seats. There are 4 seats in each row, and the seats are separated into pairs by a walkway. When ZS and Chris came, some places in the bus was already occupied.
ZS and Chris are good friends. They insist to get a pair of neighbouring empty seats. Two seats are considered neighbouring if they are in the same row and in the same pair. Given the configuration of the bus, can you help ZS and Chris determine where they should sit?
The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the number of rows of seats in the bus.
Then, n lines follow. Each line contains exactly 5 characters, the first two of them denote the first pair of seats in the row, the third character denotes the walkway (it always equals '|') and the last two of them denote the second pair of seats in the row.
Each character, except the walkway, equals to 'O' or to 'X'. 'O' denotes an empty seat, 'X' denotes an occupied seat. See the sample cases for more details.
If it is possible for Chris and ZS to sit at neighbouring empty seats, print "YES" (without quotes) in the first line. In the next n lines print the bus configuration, where the characters in the pair of seats for Chris and ZS is changed with characters '+'. Thus the configuration should differ from the input one by exactly two charaters (they should be equal to 'O' in the input and to '+' in the output).
If there is no pair of seats for Chris and ZS, print "NO" (without quotes) in a single line.
If there are multiple solutions, you may print any of them.
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
#include<map>
#include<vector>
#include<set>
#define il inline
#define re register
using namespace std;
char a[][];
int n;
int main(){
bool flag=true;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%s",a[i]+);
if(a[i][]=='O'&&a[i][]=='O'&&flag){
flag=false;
a[i][]='+';
a[i][]='+';
}
if(a[i][]=='O'&&a[i][]=='O'&&flag){
flag=false;
a[i][]='+';
a[i][]='+';
}
}
if(flag){
cout<<"NO";return ;
}
cout<<"YES\n";
for(int i=;i<=n;i++)
cout<<(a[i]+)<<endl;
return ;
}
B
ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a n × n magic grid on the entrance which is filled with integers. Chris noticed that exactly one of the cells in the grid is empty, and to enter Udayland, they need to fill a positive integer into the empty cell.
Chris tried filling in random numbers but it didn't work. ZS the Coder realizes that they need to fill in a positive integer such that the numbers in the grid form a magic square. This means that he has to fill in a positive integer so that the sum of the numbers in each row of the grid (
), each column of the grid (
), and the two long diagonals of the grid (the main diagonal —
and the secondary diagonal —
) are equal.
Chris doesn't know what number to fill in. Can you help Chris find the correct positive integer to fill in or determine that it is impossible?
The first line of the input contains a single integer n (1 ≤ n ≤ 500) — the number of rows and columns of the magic grid.
n lines follow, each of them contains n integers. The j-th number in the i-th of them denotes ai, j (1 ≤ ai, j ≤ 109 or ai, j = 0), the number in the i-th row and j-th column of the magic grid. If the corresponding cell is empty, ai, j will be equal to 0. Otherwise, ai, j is positive.
It is guaranteed that there is exactly one pair of integers i, j (1 ≤ i, j ≤ n) such that ai, j = 0.
Output a single integer, the positive integer x (1 ≤ x ≤ 1018) that should be filled in the empty cell so that the whole grid becomes a magic square. If such positive integer x does not exist, output - 1 instead.
If there are multiple solutions, you may print any of them.
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
#include<map>
#include<vector>
#include<set>
#define il inline
#define re register
using namespace std;
typedef long long ll;
int n;
long long a[][],x,y,s,t,tmp;
il bool chk(){
if(a[x][y]<=){
return false;
}
for(int i=;i<=n;i++){
t=;
for(int j=;j<=n;j++)
t+=a[i][j];
if(t!=s){
return false;
}
}
for(int j=;j<=n;j++){
t=;
for(int i=;i<=n;i++)
t+=a[i][j];
if(t!=s){
return false;
}
}
t=;
for(int i=;i<=n;i++)
t+=a[i][i];
if(t!=s) return false;
t=;
for(int i=;i<=n;i++)
t+=a[i][n-i+];
if(t!=s) return false;
return true;
}
int main(){
scanf("%d",&n);
if(n==){
cout<<"";return ;
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&a[i][j]);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(a[i][j]==){
x=i;y=j;
}
for(int i=;i<=n;i++)
if(i!=x){
for(int j=;j<=n;j++)
s+=a[i][j];
break;
}
for(int i=;i<=n;i++)
tmp+=a[x][i];
a[x][y]=s-tmp;
if(!chk()){
cout<<"-1";
}
else{
cout<<a[x][y]<<endl;
}
return ;
}
C
ZS the Coder and Chris the Baboon has arrived at Udayland! They walked in the park where n trees grow. They decided to be naughty and color the trees in the park. The trees are numbered with integers from 1 to n from left to right.
Initially, tree i has color ci. ZS the Coder and Chris the Baboon recognizes only m different colors, so 0 ≤ ci ≤ m, where ci = 0 means that tree i is uncolored.
ZS the Coder and Chris the Baboon decides to color only the uncolored trees, i.e. the trees with ci = 0. They can color each of them them in any of the m colors from 1 to m. Coloring the i-th tree with color j requires exactly pi, j litres of paint.
The two friends define the beauty of a coloring of the trees as the minimum number of contiguous groups (each group contains some subsegment of trees) you can split all the n trees into so that each group contains trees of the same color. For example, if the colors of the trees from left to right are 2, 1, 1, 1, 3, 2, 2, 3, 1, 3, the beauty of the coloring is 7, since we can partition the trees into 7 contiguous groups of the same color : {2}, {1, 1, 1}, {3}, {2, 2}, {3}, {1}, {3}.
ZS the Coder and Chris the Baboon wants to color all uncolored trees so that the beauty of the coloring is exactly k. They need your help to determine the minimum amount of paint (in litres) needed to finish the job.
Please note that the friends can't color the trees that are already colored.
The first line contains three integers, n, m and k (1 ≤ k ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of trees, number of colors and beauty of the resulting coloring respectively.
The second line contains n integers c1, c2, ..., cn (0 ≤ ci ≤ m), the initial colors of the trees. ci equals to 0 if the tree number i is uncolored, otherwise the i-th tree has color ci.
Then n lines follow. Each of them contains m integers. The j-th number on the i-th of them line denotes pi, j (1 ≤ pi, j ≤ 109) — the amount of litres the friends need to color i-th tree with color j. pi, j's are specified even for the initially colored trees, but such trees still can't be colored.
Print a single integer, the minimum amount of paint needed to color the trees. If there are no valid tree colorings of beauty k, print - 1.
因为cf机子很快,暴力DP就可以过了
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
#include<map>
#include<vector>
#include<set>
#define il inline
#define re register
using namespace std;
typedef long long ll;
int n,m,K,c[][],s[];
ll f[][][],ans=(1ll<<);
int main(){
scanf("%d%d%d",&n,&m,&K);
for(int i=;i<=n;i++)
scanf("%d",&s[i]);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
scanf("%d",&c[i][j]);
for(int i=;i<=;i++)
for(int j=;j<=;j++)
for(int k=;k<=;k++)
f[i][j][k]=(1ll<<);
if(s[]==){
for(int i=;i<=m;i++)
f[][i][]=c[][i];
}
else{
f[][s[]][]=;
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(s[i]>&&j!=s[i]) continue;
for(int k=;k<=K;k++){
for(int l=;l<=m;l++){
if(l==j){
f[i][j][k]=min(f[i][j][k],f[i-][l][k]+c[i][j]*(s[i]==));
}
else if(k>){
f[i][j][k]=min(f[i][j][k],f[i-][l][k-]+c[i][j]*(s[i]==));
}
}
// cout<<i<<" "<<j<<" "<<k<<" "<<f[i][j][k]<<endl;
}
}
}
for(int i=;i<=m;i++)
ans=min(ans,f[n][i][K]);
if(ans<(1ll<<)) cout<<ans;
else cout<<"-1";
return ;
}
D
ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it consists of n towns numbered from1 to n.
There are n directed roads in the Udayland. i-th of them goes from town i to some other town ai (ai ≠ i). ZS the Coder can flip the direction of any road in Udayland, i.e. if it goes from town A to town B before the flip, it will go from town B to town A after.
ZS the Coder considers the roads in the Udayland confusing, if there is a sequence of distinct towns A1, A2, ..., Ak (k > 1) such that for every 1 ≤ i < k there is a road from town Ai to town Ai + 1 and another road from town Ak to town A1. In other words, the roads are confusing if some of them form a directed cycle of some towns.
Now ZS the Coder wonders how many sets of roads (there are 2n variants) in initial configuration can he choose to flip such that after flipping each road in the set exactly once, the resulting network will not be confusing.
Note that it is allowed that after the flipping there are more than one directed road from some town and possibly some towns with no roads leading out of it, or multiple roads between any pair of cities.
The first line of the input contains single integer n (2 ≤ n ≤ 2·105) — the number of towns in Udayland.
The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n, ai ≠ i), ai denotes a road going from town i to town ai.
Print a single integer — the number of ways to flip some set of the roads so that the resulting whole set of all roads is not confusing. Since this number may be too large, print the answer modulo 109 + 7.
因为这样一个每个点只会连出去一个有向边的图必然会变成一个环+内向树的样子,可以肯定的是在一个环上的边如果有m条,那么这个环上的边的答案是2^m-2至于不在环上的东西,随便都不影响,于是只要tarjan缩环即可
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
#include<map>
#include<vector>
#include<set>
#define il inline
#define re register
#define mod 1000000007
using namespace std;
struct edge{int next,to;
} e[];
int n,M,g[],dfn[],ind,q[],low[],inq[],val[],scc,top;
long long ans=,tmp;
il void addedge(int x,int y){
e[++M]=(edge){g[x],y};g[x]=M;
}
il void tarjan(int x){
dfn[x]=low[x]=++ind;
q[++top]=x;inq[x]=;
for(int i=g[x];i;i=e[i].next)
if(!dfn[e[i].to])
tarjan(e[i].to),low[x]=min(low[x],low[e[i].to]);
else if(inq[e[i].to])
low[x]=min(low[x],dfn[e[i].to]);
if(low[x]==dfn[x])
{
int now=-;scc++;
while(now!=x)
{
now=q[top--];inq[now]=;
val[scc]++;
}
}
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
int x;
scanf("%d",&x);
addedge(i,x);
}
for(int i=;i<=n;i++)
if(!dfn[i]) tarjan(i);
int rest=n;
for(int i=;i<=scc;i++){
if(val[i]==) continue;
rest-=val[i];
tmp=;
for(int j=;j<=val[i];j++) tmp=(tmp+tmp)%mod;
tmp=(tmp-+mod)%mod;
ans=ans*tmp%mod;
}
tmp=;
for(int j=;j<=rest;j++)
tmp=(tmp+tmp)%mod;
ans=ans*tmp%mod;
cout<<ans;
return ;
}
Codeforces711的更多相关文章
随机推荐
- 【Unity3d】MenuItem修饰的方法无法触发的可能原因
遇到了MenuItem修饰的方法无法触发的情况,顺利解决. 类放在Editor目录下,该类下其他方法被MenuItem修饰可以触发. 后来发现我修饰的方法和该类下另一个方法重名了. 改方法名,问题解决 ...
- C#实现仪器的自动化控制
1.概述 生产测试当中,测试仪器不可或缺,如果是小规模生产,手动测试可以对付:但是要想到达大批量生产的目的,为了简化测试,节约时间,就需要进行自动化测试.出于这样的需求,对仪器的自动化程控就有了需求. ...
- TensorFlow深度学习实战---图像数据处理
图像的亮度.对比度等属性对图像的影响非常大,这些因素都会影响最后的识别结构.当然,复杂的预处理过程可能会导致训练效率的下降(利用TensorFlow中多线程处理输入数据的解决方案). 同一不同的原始数 ...
- Jmeter直连postgresql数据库进行压测
关于Jmeter直连数据库进行压测,网上有好多教程了,pg数据库的相对少一些,今天自己测试了下,还是挺简单的,有个别需要注意的地方.相较于Loadrunner这么全面庞大的压测工具,Jmeter在数据 ...
- Python接口测试实战5(上) - Git及Jenkins持续集成
如有任何学习问题,可以添加作者微信:lockingfree 课程目录 Python接口测试实战1(上)- 接口测试理论 Python接口测试实战1(下)- 接口测试工具的使用 Python接口测试实战 ...
- 个人安装ss的一个记录
在ubuntu16.04安装ss服务.由于lantern最近极其不稳定(我还花钱的qaq),经常断联以至于几乎废了,莫得办法,只好花钱搭一个了orz...呵,贫穷.... 安装shadowsocks ...
- 吴恩达机器学习笔记——正规方程(Normal Equation)
问题描述:m examples : (x(1),y(1)), (x(2),y(2)),..., (x(m),y(m)) and n features; 计算方法:θ = (XTX)-1XTy; 计算过 ...
- 20135313_exp5
课程:Java程序与设计 班级:1353 姓 名:吴子怡 学号:20135313 小组成员: 20135113肖昱 成绩: 指导教师:娄嘉鹏 实验日期:2 ...
- 团队计划会议(二)——WBS
一.会议及WBS 因为是第一次开发android应用,所以我们对这次开发心里也没底,最后我们商量暂时先实现主要的几个骨架功能,之后再慢慢完善. 会议期间,我们根据自己的能力大致先估算了完成这些功能需要 ...
- 又要开始新的征程了hhh(这次内容比较感兴趣)
因为做英雄部分,既是我比较感兴趣,又很符合这次c++学习的目的,所以我很开心. 其实从小玩的RPG,即时战略和回合制游戏不算少,对于属性方法其实都算不上陌生.但是还是在网上找了一些学习资源. http ...