CF #368 div2
题目链接:http://codeforces.com/contest/707/problem/A
2 seconds
256 megabytes
standard input
standard output
Small, but very brave, mouse Brain was not accepted to summer school of young villains. He was upset and decided to postpone his plans of taking over the world, but to become a photographer instead.
As you may know, the coolest photos are on the film (because you can specify the hashtag #film for such).
Brain took a lot of colourful pictures on colored and black-and-white film. Then he developed and translated it into a digital form. But now, color and black-and-white photos are in one folder, and to sort them, one needs to spend more than one hour!
As soon as Brain is a photographer not programmer now, he asks you to help him determine for a single photo whether it is colored or black-and-white.
Photo can be represented as a matrix sized n × m, and each element of the matrix stores a symbol indicating corresponding pixel color. There are only 6 colors:
- 'C' (cyan)
- 'M' (magenta)
- 'Y' (yellow)
- 'W' (white)
- 'G' (grey)
- 'B' (black)
The photo is considered black-and-white if it has only white, black and grey pixels in it. If there are any of cyan, magenta or yellow pixels in the photo then it is considered colored.
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 100) — the number of photo pixel matrix rows and columns respectively.
Then n lines describing matrix rows follow. Each of them contains m space-separated characters describing colors of pixels in a row. Each character in the line is one of the 'C', 'M', 'Y', 'W', 'G' or 'B'.
Print the "#Black&White" (without quotes), if the photo is black-and-white and "#Color" (without quotes), if it is colored, in the only line.
2 2
C M
Y Y
#Color
3 2
W W
W W
B B
#Black&White
1 1
W
#Black&White 水题。。。不多说了。
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int n,m;
bool bo;
char ch;
int main(){
scanf("%d%d",&n,&m);bo=;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
for(ch=getchar();ch!='C'&&ch!='M'&&ch!='Y'&&ch!='W'&&ch!='G'&&ch!='B';ch=getchar());
if(ch!='W'&&ch!='G'&&ch!='B')bo=;
}
if(bo)printf("#Black&White\n");
else printf("#Color\n");
return ;
}
题目链接:http://codeforces.com/contest/707/problem/B
2 seconds
256 megabytes
standard input
standard output
Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. There are m bidirectional roads, each of whose connects some pair of cities.
To bake muffins in her bakery, Masha needs to establish flour supply from some storage. There are only k storages, located in different cities numbered a1, a2, ..., ak.
Unforunately the law of the country Masha lives in prohibits opening bakery in any of the cities which has storage located in it. She can open it only in one of another n - k cities, and, of course, flour delivery should be paid — for every kilometer of path between storage and bakery Masha should pay 1 ruble.
Formally, Masha will pay x roubles, if she will open the bakery in some city b (ai ≠ b for every 1 ≤ i ≤ k) and choose a storage in some city s (s = aj for some 1 ≤ j ≤ k) and b and s are connected by some path of roads of summary length x (if there are more than one path, Masha is able to choose which of them should be used).
Masha is very thrifty and rational. She is interested in a city, where she can open her bakery (and choose one of k storages and one of the paths between city with bakery and city with storage) and pay minimum possible amount of rubles for flour delivery. Please help Masha find this amount.
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 105, 0 ≤ k ≤ n) — the number of cities in country Masha lives in, the number of roads between them and the number of flour storages respectively.
Then m lines follow. Each of them contains three integers u, v and l (1 ≤ u, v ≤ n, 1 ≤ l ≤ 109, u ≠ v) meaning that there is a road between cities u and v of length of l kilometers .
If k > 0, then the last line of the input contains k distinct integers a1, a2, ..., ak (1 ≤ ai ≤ n) — the number of cities having flour storage located in. If k = 0 then this line is not presented in the input.
Print the minimum possible amount of rubles Masha should pay for flour delivery in the only line.
If the bakery can not be opened (while satisfying conditions) in any of the n cities, print - 1 in the only line.
5 4 2
1 2 5
1 2 3
2 3 4
1 4 10
1 5
3
3 1 1
1 2 3
3
-1
Image illustrates the first sample case. Cities with storage located in and the road representing the answer are darkened.
最优点一定是与k个特殊点直接相连的点,枚举就好了。。
#include<cstdio>
#include<cstring>
#include<iostream>
#define maxn 100005
using namespace std;
int n,m,k,tot,h[maxn],way[maxn*],fuck[maxn*],val[maxn*],a[maxn],f[maxn],ans;
bool b[maxn];
const int inf=2e9;
void insert(int x,int y,int z){way[++tot]=y;fuck[tot]=h[x];h[x]=tot;val[tot]=z;}
int main(){
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=n;i++)f[i]=inf;
for(int i=,x,y,z;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);insert(x,y,z);insert(y,x,z);
}
for(int i=;i<=k;i++){
scanf("%d",&a[i]);b[a[i]]=;
}
for(int i=;i<=k;i++){
for(int j=h[a[i]];j;j=fuck[j]){
if(b[way[j]]==)continue;
f[way[j]]=min(f[way[j]],val[j]);
}
}
ans=inf;
for(int i=;i<=n;i++)ans=min(ans,f[i]);
if(ans==inf)printf("-1\n");
else printf("%d\n",ans);
return ;
}
题目链接:http://codeforces.com/contest/707/problem/C
1 second
256 megabytes
standard input
standard output
Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are calledPythagorean triples.
For example, triples (3, 4, 5), (5, 12, 13) and (6, 8, 10) are Pythagorean triples.
Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse.
Katya had no problems with completing this task. Will you do the same?
The only line of the input contains single integer n (1 ≤ n ≤ 109) — the length of some side of a right triangle.
Print two integers m and k (1 ≤ m, k ≤ 1018), such that n, m and k form a Pythagorean triple, in the only line.
In case if there is no any Pythagorean triple containing integer n, print - 1 in the only line. If there are many answers, print any of them.
3
4 5
6
8 10
1
-1
17
144 145
67
2244 2245
Illustration for the first sample.
套公式。。如果a=2n+1,b=2n^2+2n, c=2n^2+2n+1。a=2n,b=n^2-1, c=n^2+1。
#include<cstdio>
#include<cstring>
#include<iostream>
long long a,b,c;
int main(){
scanf("%I64d",&a);
if(a==||a==){printf("-1\n");return ;}
if(a==){printf("3 5\n");return ;}
if(a&){a=(a-)/;b=*a*a+*a;c=b+;}
else{a/=;b=a*a-;c=a*a+;}
printf("%I64d %I64d\n",b,c);
return ;
}
题目链接:http://codeforces.com/contest/707/problem/D
2 seconds
512 megabytes
standard input
standard output
Recently in school Alina has learned what are the persistent data structures: they are data structures that always preserves the previous version of itself and access to it when it is modified.
After reaching home Alina decided to invent her own persistent data structure. Inventing didn't take long: there is a bookcase right behind her bed. Alina thinks that the bookcase is a good choice for a persistent data structure. Initially the bookcase is empty, thus there is no book at any position at any shelf.
The bookcase consists of n shelves, and each shelf has exactly m positions for books at it. Alina enumerates shelves by integers from 1to n and positions at shelves — from 1 to m. Initially the bookcase is empty, thus there is no book at any position at any shelf in it.
Alina wrote down q operations, which will be consecutively applied to the bookcase. Each of the operations has one of four types:
- 1 i j — Place a book at position j at shelf i if there is no book at it.
- 2 i j — Remove the book from position j at shelf i if there is a book at it.
- 3 i — Invert book placing at shelf i. This means that from every position at shelf i which has a book at it, the book should be removed, and at every position at shelf i which has not book at it, a book should be placed.
- 4 k — Return the books in the bookcase in a state they were after applying k-th operation. In particular, k = 0 means that the bookcase should be in initial state, thus every book in the bookcase should be removed from its position.
After applying each of operation Alina is interested in the number of books in the bookcase. Alina got 'A' in the school and had no problem finding this values. Will you do so?
The first line of the input contains three integers n, m and q (1 ≤ n, m ≤ 103, 1 ≤ q ≤ 105) — the bookcase dimensions and the number of operations respectively.
The next q lines describes operations in chronological order — i-th of them describes i-th operation in one of the four formats described in the statement.
It is guaranteed that shelf indices and position indices are correct, and in each of fourth-type operation the number k corresponds to some operation before it or equals to 0.
For each operation, print the number of books in the bookcase after applying it in a separate line. The answers should be printed in chronological order.
2 3 3
1 1 1
3 2
4 0
1
4
0
4 2 6
3 2
2 2 2
3 3
3 2
2 2 2
3 2
2
1
3
3
2
4
2 2 2
3 2
2 2 1
2
1
This image illustrates the second sample case.
如果是操作1,2,3,i-1向i连边,如果是4,k向i连边。然后一遍暴力dfs就行了。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#define maxn 1005
#define maxq 100005
using namespace std;
int n,m,q,a[maxq][],ans[maxq];
bool b[maxn][maxn];
vector <int> son[maxq];
void dfs(int x,int y){
bool bo=;
if(a[x][]==){
if(b[a[x][]][a[x][]]==)++y,bo=;
b[a[x][]][a[x][]]=;
}else if(a[x][]==){
if(b[a[x][]][a[x][]]==)--y,bo=;
b[a[x][]][a[x][]]=;
}else if(a[x][]==){
for(int i=;i<=m;i++){
if(b[a[x][]][i]==)y++;else y--;
b[a[x][]][i]=!b[a[x][]][i];
}
}
ans[x]=y;
int sz=son[x].size();
for(int i=;i<sz;i++)dfs(son[x][i],y);
if(a[x][]==){
if(bo)b[a[x][]][a[x][]]=;
}else if(a[x][]==){
if(bo)b[a[x][]][a[x][]]=;
}else if(a[x][]==){
for(int i=;i<=m;i++)b[a[x][]][i]=!b[a[x][]][i];
}
}
int main(){
scanf("%d%d%d",&n,&m,&q);
for(int i=;i<=q;i++){
scanf("%d",&a[i][]);
if(a[i][]==||a[i][]==)scanf("%d%d",&a[i][],&a[i][]),son[i-].push_back(i);
else{
scanf("%d",&a[i][]);
if(a[i][]==)son[i-].push_back(i);
else son[a[i][]].push_back(i);
}
}
dfs(,);
for(int i=;i<=q;i++)printf("%d\n",ans[i]);
return ;
}
题目链接:http://codeforces.com/contest/707/problem/E
3 seconds
256 megabytes
standard input
standard output
Like all children, Alesha loves New Year celebration. During the celebration he and his whole family dress up the fir-tree. Like all children, Alesha likes to play with garlands — chains consisting of a lightbulbs.
Alesha uses a grid field sized n × m for playing. The rows of the field are numbered from 1 to n from the top to the bottom and columns are numbered from 1 to m from the left to the right.
Alesha has k garlands which he places at the field. He does so in the way such that each lightbulb of each garland lies in the center of some cell in the field, and each cell contains at most one lightbulb. Of course lightbulbs, which are neighbours in some garland, appears in cells neighbouring by a side.
The example of garland placing.
Each garland is turned off or turned on at any moment. If some garland is turned on then each of its lightbulbs is turned on, the same applies for garland turned off. Each lightbulb in the whole garland set is unique, and thus, being turned on, brings Alesha some pleasure, described by an integer value. Turned off lightbulbs don't bring Alesha any pleasure.
Alesha can turn garlands on and off and wants to know the sum of pleasure value which the lightbulbs, placed in the centers of the cells in some rectangular part of the field, bring him. Initially all the garlands are turned on.
Alesha is still very little and can't add big numbers. He extremely asks you to help him.
The first line of the input contains three integers n, m and k (1 ≤ n, m, k ≤ 2000) — the number of field rows, the number of field columns and the number of garlands placed at the field respectively.
Next lines contains garlands set description in the following format:
The first line of a single garland description contains a single integer len (1 ≤ len ≤ 2000) — the number of lightbulbs in the garland.
Each of the next len lines contains three integers i, j and w (1 ≤ i ≤ n, 1 ≤ j ≤ m, 1 ≤ w ≤ 109) — the coordinates of the cell containing a lightbullb and pleasure value Alesha gets from it if it is turned on. The lightbulbs are given in the order they are forming a chain in the garland. It is guaranteed that neighbouring lightbulbs are placed in the cells neighbouring by a side.
The next line contains single integer q (1 ≤ q ≤ 106) — the number of events in Alesha's game. The next q lines describes events in chronological order. The i-th of them describes the i-th event in the one of the following formats:
- SWITCH i — Alesha turns off i-th garland if it is turned on, or turns it on if it is turned off. It is guaranteed that 1 ≤ i ≤ k.
- ASK x1 y1 x2 y2 — Alesha wants to know the sum of pleasure values the lightbulbs, placed in a rectangular part of the field. Top-left cell of a part has coordinates (x1, y1) and right-bottom cell has coordinates (x2, y2). It is guaranteed that 1 ≤ x1 ≤ x2 ≤ n and1 ≤ y1 ≤ y2 ≤ m. There is no more than 2000 events of this type in the input.
All the numbers in the input are integers.
Please note that the input is quite large, so be careful while using some input ways. In particular, it's not recommended to use cin in codes on C++ and class Scanner in codes on Java.
For each ASK operation print the sum Alesha wants to know in a separate line. Print the answers in chronological order.
4 4 3
5
1 1 2
1 2 3
2 2 1
2 1 4
3 1 7
4
1 3 1
2 3 3
2 4 3
1 4 1
7
4 1 1
4 2 9
3 2 8
3 3 3
4 3 4
4 4 1
3 4 1
2
ASK 2 2 3 3
ASK 1 1 4 4
15
52
4 4 1
8
4 1 1
3 1 2
2 1 1
1 1 7
1 2 5
2 2 4
2 3 1
1 3 1
3
ASK 1 1 3 2
SWITCH 1
ASK 1 1 3 2
19
0
This image illustrates the first sample case.
预处理出每条链对每个询问的贡献(o(n^2log^2n)),然后就可以o(k)地询问了。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#define maxn 2005
#define maxq 1000005
using namespace std;
int n,m,k,tot,q,a[maxq][],b[maxn];
long long val[maxn][maxn],t[maxn][maxn];
struct dian{int x,y,v;};
vector <dian> lian[maxn];
char s[];
bool pp[maxn];
void insert(int x,int y,int v){
for(int i=x;i<=n;i+=i&(-i))
for(int j=y;j<=m;j+=j&(-j))t[i][j]+=v;
}
long long query(int x,int y){
long long tt=;
for(int i=x;i;i-=i&(-i))
for(int j=y;j;j-=j&(-j))tt+=t[i][j];
return tt;
}
long long calc(int x1,int y1,int x2,int y2){return query(x2,y2)-query(x1-,y2)-query(x2,y1-)+query(x1-,y1-);}
int main(){
scanf("%d%d%d",&n,&m,&k);
for(int i=,x,y,z,w;i<=k;i++){
scanf("%d",&x);
while(x--){
scanf("%d%d%d",&y,&z,&w);lian[i].push_back((dian){y,z,w});
}
}
scanf("%d",&q);
for(int i=;i<=q;i++){
scanf("%s",s);
if(s[]=='A'){b[++tot]=i;a[i][]=tot;a[i][]=;scanf("%d%d%d%d",&a[i][],&a[i][],&a[i][],&a[i][]);}
else{a[i][]=;scanf("%d",&a[i][]);}
}
for(int i=;i<=k;i++){
int sz=lian[i].size();
for(int j=;j<sz;j++)insert(lian[i][j].x,lian[i][j].y,lian[i][j].v);
for(int j=;j<=tot;j++)val[i][j]=calc(a[b[j]][],a[b[j]][],a[b[j]][],a[b[j]][]);
for(int j=;j<sz;j++)insert(lian[i][j].x,lian[i][j].y,-lian[i][j].v);
}
for(int i=;i<=q;i++){
if(a[i][]==)pp[a[i][]]=!pp[a[i][]];
else{
long long ans=;
for(int j=;j<=k;j++){
if(pp[j]==)ans+=val[j][a[i][]];
}
printf("%I64d\n",ans);
}
}
return ;
}
CF #368 div2的更多相关文章
- cf 442 div2 F. Ann and Books(莫队算法)
cf 442 div2 F. Ann and Books(莫队算法) 题意: \(给出n和k,和a_i,sum_i表示前i个数的和,有q个查询[l,r]\) 每次查询区间\([l,r]内有多少对(i, ...
- CF#603 Div2
差不多半年没打cf,还是一样的菜:不过也没什么,当时是激情,现在已是兴趣了,开心就好. A Sweet Problem 思维,公式推一下过了 B PIN Codes 队友字符串取余过了,结果今天早上一 ...
- CF R631 div2 1330 E Drazil Likes Heap
LINK:Drazil Likes Heap 那天打CF的时候 开场A读不懂题 B码了30min才过(当时我怀疑B我写的过于繁琐了. C比B简单多了 随便yy了一个构造发现是对的.D也超级简单 dp了 ...
- CF#581 (div2)题解
CF#581 题解 A BowWow and the Timetable 如果不是4幂次方直接看位数除以二向上取整,否则再减一 #include<iostream> #include< ...
- CodeForces #368 div2 D Persistent Bookcase DFS
题目链接:D Persistent Bookcase 题意:有一个n*m的书架,开始是空的,现在有k种操作: 1 x y 这个位置如果没书,放书. 2 x y 这个位置如果有书,拿走. 3 x 反转这 ...
- [CF#286 Div2 D]Mr. Kitayuta's Technology(结论题)
题目:http://codeforces.com/contest/505/problem/D 题目大意:就是给你一个n个点的图,然后你要在图中加入尽量少的有向边,满足所有要求(x,y),即从x可以走到 ...
- CF 197 DIV2 Xenia and Bit Operations 线段树
线段树!!1A 代码如下: #include<iostream> #include<cstdio> #define lson i<<1 #define rson i ...
- CF#345 div2 A\B\C题
A题: 贪心水题,注意1,1这组数据,坑了不少人 #include <iostream> #include <cstring> using namespace std; int ...
- CF R303 div2 C. Woodcutters
C. Woodcutters time limit per test 1 second memory limit per test 256 megabytes input standard input ...
随机推荐
- Vim快捷键大全
vi(vim)是上Linux非常常用的编辑器,很多Linux发行版都默认安装了vi(vim).vi(vim)命令繁多但是如果使用灵活之后将会大大提高效率.vi是"visual interfa ...
- IOS之frame和bounds区别
用最简单的语言来解释就是:setFrame和setBounds都是为了把子view加载到父view上去,但设置的参数坐标系不同,setFrame是该view在父view坐标系统中的位置和大小,setB ...
- 利用Hibernate监听器实现用户操作日志
网上搜索发现,实现用户操作日志的方式有:自定义注解方式.Hibernate拦截器方式.Hibernate监听器方式等. 1.自定义注解方式较为麻烦,需要进行操作记录的方法均需要添加注解,但是相对的操作 ...
- Java学习笔记-File
//文件操作 //2015年4月30日15:40:21 package com.alfredsun.first.main; import java.io.File; import java.io.IO ...
- phonegap 随笔
开发者论坛 http://bbs.phonegapcn.com/forum.php phone调用android本地方法 http://blog.csdn.net/crazyman2010/artic ...
- Scala Singleton对象
Scala Object: scala没有静态的修饰符,例如Java中的static.但是Scala提供了Object类型,object下的成员都是静态的,比较像Java的静态类.不同在于Scala的 ...
- bzDemo
<Public> <property name="Types"> <get/> </property> <method nam ...
- 创建 userSettings/Microsoft.SqlServer.Configuration.LandingPage.Properties.Settings 的配置节处理程序时出错: 未能加载文件或程序集“System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”或它的某一个依赖项。系统没找到指定的文件
创建 userSettings/Microsoft.SqlServer.Configuration.LandingPage.Properties.Settings 的配置节处理程序时出错: 未能加载文 ...
- UML(Unified Modeling Language)同一建模语言
wiki定义: UML is a general-purpose, developmental, modeling language in the field of software engineer ...
- mysql 时间类型分类
MySQL:MySQL日期数据类型.MySQL时间类型使用总结 MySQL 日期类型:日期格式.所占存储空间.日期范围 比较. 日期类型 存储空间 日期格式 日期范围------------ ---- ...