codeforces练习
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
System Crawler (2014-07-07)
Description
DZY loves colors, and he enjoys painting.
On a colorful day, DZY gets a colorful ribbon, which consists of n units (they are numbered from 1 to n from left to right). The color of the i-th unit of the ribbon is i at first. It is colorful enough, but we still consider that the colorfulness of each unit is 0 at first.
DZY loves painting, we know. He takes up a paintbrush with color x and uses it to draw a line on the ribbon. In such a case some contiguous units are painted. Imagine that the color of unit i currently is y. When it is painted by this paintbrush, the color of the unit becomes x, and the colorfulness of the unit increases by |x - y|.
DZY wants to perform m operations, each operation can be one of the following:
- Paint all the units with numbers between l and r (both inclusive) with color x.
- Ask the sum of colorfulness of the units between l and r (both inclusive).
Can you help DZY?
Input
The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105).
Each of the next m lines begins with a integer type (1 ≤ type ≤ 2), which represents the type of this operation.
If type = 1, there will be 3 more integers l, r, x (1 ≤ l ≤ r ≤ n; 1 ≤ x ≤ 108) in this line, describing an operation 1.
If type = 2, there will be 2 more integers l, r (1 ≤ l ≤ r ≤ n) in this line, describing an operation 2.
Output
For each operation 2, print a line containing the answer — sum of colorfulness.
Sample Input
- 3 3
1 1 2 4
1 2 3 5
2 1 3
- 8
- 3 4
1 1 3 4
2 1 1
2 2 2
2 3 3
- 3
2
1
- 10 6
1 1 5 3
1 2 7 9
1 10 10 11
1 3 8 12
1 1 10 3
2 1 10
- 129
Hint
In the first sample, the color of each unit is initially [1, 2, 3], and the colorfulness is [0, 0, 0].
After the first operation, colors become [4, 4, 3], colorfulness become [3, 2, 0].
After the second operation, colors become [4, 5, 5], colorfulness become [3, 3, 2].
So the answer to the only operation of type 2 is 8.
线段树成段更新
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- using namespace std ;
- #define ls ( o << 1 )
- #define rs ( o << 1 | 1 )
- #define lson ls , l , m
- #define rson rs , m + 1 , r
- #define rt o , l , r
- #define root 1 , 1 , n
- #define mid ( ( l + r ) >> 1 )
- #define clear( a , x ) memset ( a , x , sizeof a )
- typedef long long LL ;
- const int MAXN = ;
- int set[MAXN << ] ;
- LL sum[MAXN << ] ;
- LL add[MAXN << ] ;
- void pushUp ( int o , int l , int r ) {
- set[o] = ( set[ls] == set[rs] ? set[ls] : ) ;
- sum[o] = sum[ls] + sum[rs] ;
- }
- void pushDown ( int o , int l , int r ) {
- int m = mid ;
- if ( set[o] ) set[ls] = set[rs] = set[o] ;
- if ( add[o] ) {
- sum[ls] += add[o] * ( m - l + ) ;
- add[ls] += add[o] ;
- sum[rs] += add[o] * ( r - m ) ;
- add[rs] += add[o] ;
- add[o] = ;
- }
- }
- void build ( int o , int l , int r ) {
- add[o] = set[o] = sum[o] = ;
- if ( l == r ) {
- set[o] = l ;
- return ;
- }
- int m = mid ;
- build ( lson ) ;
- build ( rson ) ;
- }
- void update ( int x , int L , int R , int o , int l , int r ) {
- if ( L <= l && r <= R ) {
- if ( set[o] ) {
- add[o] += abs ( x - set[o] ) ;
- sum[o] += ( LL ) abs ( x - set[o] ) * ( r - l + ) ;
- set[o] = x ;
- return ;
- }
- }
- pushDown ( rt ) ;
- int m = mid ;
- if ( L <= m ) update ( x , L , R , lson ) ;
- if ( m < R ) update ( x , L , R , rson ) ;
- pushUp ( rt ) ;
- }
- LL query ( int L , int R , int o , int l , int r ) {
- if ( L <= l && r <= R ) return sum[o] ;
- pushDown ( rt ) ;
- int m = mid ;
- LL ans = ;
- if ( L <= m ) ans += query ( L , R , lson ) ;
- if ( m < R ) ans += query ( L , R , rson ) ;
- return ans ;
- }
- void work () {
- int n , m ;
- int type , l , r , x ;
- while ( ~scanf ( "%d%d" , &n , &m ) ) {
- build ( root ) ;
- while ( m -- ) {
- scanf ( "%d%d%d" , &type , &l , &r ) ;
- if ( type == ) {
- scanf ( "%d" , &x ) ;
- update ( x , l , r , root ) ;
- }
- else printf ( "%I64d\n" , query ( l , r , root ) ) ;
- }
- }
- }
- int main () {
- work () ;
- return ;
- }
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
System Crawler (2014-07-08)
Description
DZY loves chessboard, and he enjoys playing with it.
He has a chessboard of n rows and m columns. Some cells of the chessboard are bad, others are good. For every good cell, DZY wants to put a chessman on it. Each chessman is either white or black. After putting all chessmen, DZY wants that no two chessmen with the same color are on two adjacent cells. Two cells are adjacent if and only if they share a common edge.
You task is to find any suitable placement of chessmen on the given chessboard.
Input
The first line contains two space-separated integers n and m(1 ≤ n, m ≤ 100).
Each of the next n lines contains a string of m characters: the j-th character of the i-th string is either "." or "-". A "." means that the corresponding cell (in the i-th row and the j-th column) is good, while a "-" means it is bad.
Output
Output must contain n lines, each line must contain a string of m characters. The j-th character of the i-th string should be either "W", "B" or "-". Character "W" means the chessman on the cell is white, "B" means it is black, "-" means the cell is a bad cell.
If multiple answers exist, print any of them. It is guaranteed that at least one answer exists.
Sample Input
- 1 1
.
- B
- 2 2
..
..
- BW
WB
- 3 3
.-.
---
--.
- B-B
---
--B
Hint
In the first sample, DZY puts a single black chessman. Of course putting a white one is also OK.
In the second sample, all 4 cells are good. No two same chessmen share an edge in the sample output.
In the third sample, no good cells are adjacent. So you can just put 3 chessmen, no matter what their colors are.
先暴力出一个相邻都不同颜色的表,'.'根据表输出结果,'-'直接输出'-'。
- #include<stdio.h>
- #define maxn 109
- int map[maxn][maxn];
- void init()
- {
- int i,j,t;
- for(i = ;i<maxn;i++)
- {
- if(i%) t=;
- else t=;
- for(j=;j<maxn;j++)
- {
- map[i][j]=t;
- t=!t;
- }
- }
- }
- int main()
- {
- int i,j;
- init();
- int n,m;
- char ss;
- while(~scanf("%d %d",&n,&m))
- for(i = ;i<n;i++)
- {
- getchar();
- for(j=;j<m;j++)
- {
- ss=getchar();
- if(ss == '-')
- printf("-");
- else
- printf("%c",map[i][j]?'B':'W');
- }
- printf("\n");
- }
- return ;
- }
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
System Crawler (2014-07-09)
Description
DZY loves chemistry, and he enjoys mixing chemicals.
DZY has n chemicals, and m pairs of them will react. He wants to pour these chemicals into a test tube, and he needs to pour them in one by one, in any order.
Let's consider the danger of a test tube. Danger of an empty test tube is 1. And every time when DZY pours a chemical, if there are already one or more chemicals in the test tube that can react with it, the danger of the test tube will be multiplied by 2. Otherwise the danger remains as it is.
Find the maximum possible danger after pouring all the chemicals one by one in optimal order.
Input
The first line contains two space-separated integers n and m.
Each of the next m lines contains two space-separated integers xi and yi(1 ≤ xi < yi ≤ n). These integers mean that the chemical xiwill react with the chemical yi. Each pair of chemicals will appear at most once in the input.
Consider all the chemicals numbered from 1 to n in some order.
Output
Print a single integer — the maximum possible danger.
Sample Input
- 1 0
- 1
- 2 1
1 2
- 2
- 3 2
1 2
2 3
- 4
题目大意:有n种化学物质跟m种反应关系,试管初始的危险系数为1,每次往试管中添加物质,若与前面的物质发生了化学反应那么危险系数乘以2,求最大危险系数。
解题思路:用并查集找出各个集合,每个结合的最大发生反应次数为它物质的个数-1。换种思路,就是求s=物质的总数-集合个数,结果为2^t。
- #include<stdio.h>
- #define maxn 105
- typedef long long LL;
- int fa[maxn];
- int find(int x)
- {
- while(fa[x] != x)
- {
- x = fa[x];
- }
- return x;
- }
- void merge(int x,int y)
- {
- int a = find(x);
- int b = find(y);
- if(a != b)
- fa[a] = b;
- }
- int main()
- {
- int n,m,i,a,b;
- scanf("%d%d",&n,&m);
- for(i=;i<maxn;i++)
- fa[i] = i;
- for(i = ;i<m;i++)
- {
- scanf("%d%d",&a,&b);
- merge(a,b);
- }
- int t=;
- LL ans =;
- for(i = ;i<=n;i++)
- {
- if(fa[i] == i)t++;
- }
- ans = ans<<(n-t);
- printf("%lld\n",ans);
- return ;
- }
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
System Crawler (2014-07-14)
Description
As we know, DZY loves playing games. One day DZY decided to play with a n × m matrix. To be more precise, he decided to modify the matrix with exactly k operations.
Each modification is one of the following:
- Pick some row of the matrix and decrease each element of the row by p. This operation brings to DZY the value of pleasure equal to the sum of elements of the row before the decreasing.
- Pick some column of the matrix and decrease each element of the column by p. This operation brings to DZY the value of pleasure equal to the sum of elements of the column before the decreasing.
DZY wants to know: what is the largest total value of pleasure he could get after performing exactly k modifications? Please, help him to calculate this value.
Input
The first line contains four space-separated integers n, m, k and p(1 ≤ n, m ≤ 103; 1 ≤ k ≤ 106; 1 ≤ p ≤ 100).
Then n lines follow. Each of them contains m integers representing aij (1 ≤ aij ≤ 103) — the elements of the current row of the matrix.
Output
Output a single integer — the maximum possible total pleasure value DZY could get.
Sample Input
- 2 2 2 2
1 3
2 4
- 11
- 2 2 5 2
1 3
2 4
- 11
Hint
For the first sample test, we can modify: column 2, row 2. After that the matrix becomes:
- 1 1
0 0
For the second sample test, we can modify: column 2, row 2, row 1, column 1, column 2. After that the matrix becomes:
- -3 -3
-2 -2
- 题目大意:一个n*m的矩阵有k次操作,每次操作选一行或一列同时减去p操作的价值为这一行或一列没操作之前的和,求最大的价值。
解题思路:一共操作k次,可以设行操作i次,列操作k-i次,每次都是最优操作即选那一行或那一列的和最大的,对i从0到k枚举结果。
- #include <iostream>
- #include <cstdio>
- #include <queue>
- #include <cstring>
- using namespace std;
- typedef __int64 LL;
- const int maxn=;
- const int maxm=;
- int map[maxn][maxn];
- LL C[maxm],R[maxm];
- priority_queue<LL>sc,sr;
- LL max(LL a,LL b){return a>b?a:b;}
- int main()
- {
- int i,j,n,m,k,p;
- LL ans,s;
- while(~scanf("%d %d %d %d",&n,&m,&k,&p))
- {
- while(!sc.empty()) sc.pop();
- while(!sr.empty()) sr.pop();
- memset(C,,sizeof(C));
- memset(R,,sizeof(R));
- for(i=;i<n;i++)
- for(j=;j<m;j++)
- scanf("%d",&map[i][j]);
- for(i=;i<n;i++)
- {
- s=;
- for(j=;j<m;j++)
- s+=map[i][j];
- sc.push(s);
- }
- for(j=;j<m;j++)
- {
- s=;
- for(i=;i<n;i++)
- s+=map[i][j];
- sr.push(s);
- }
- for(i=;i<=k;i++)
- {
- s=sc.top();sc.pop();
- C[i]=C[i-]+s;
- s=s-p*m;
- sc.push(s);
- }
- for(i=;i<=k;i++)
- {
- s=sr.top();sr.pop();
- R[i]=R[i-]+s;
- s=s-p*n;
- sr.push(s);
- }
- ans=-1e18;
- for(i=;i<=k;i++)
- ans=max(ans,C[i]+R[k-i]-(LL)i*(k-i)*p);
- printf("%I64d\n",ans);
- }
- return ;
- }
System Crawler (2014-06-20)
Description
Artem has an array of n positive integers. Artem decided to play with it. The game consists of n moves. Each move goes like this. Artem chooses some element of the array and removes it. For that, he gets min(a, b) points, where a and b are numbers that were adjacent with the removed number. If the number doesn't have an adjacent number to the left or right, Artem doesn't get any points.
After the element is removed, the two parts of the array glue together resulting in the new array that Artem continues playing with. Borya wondered what maximum total number of points Artem can get as he plays this game.
Input
The first line contains a single integer n(1 ≤ n ≤ 5·105) — the number of elements in the array. The next line contains n integers ai(1 ≤ ai ≤ 106) — the values of the array elements.
Output
In a single line print a single integer — the maximum number of points Artem can get.
Sample Input
- 5
3 1 5 2 6
- 11
- 5
1 2 3 4 5
- 6
- 5
1 100 101 100 1
- 102
- 题目大意:给一个n的整数序列,每次删除一个数此次操作的价值为它左右两个数间的较小值,若左边或右边没有数那此次操作的价值为0,求最大价值。
解题思路:本着这波不亏的贪心思想,先从那些删了这个数不亏的开始,即左右两个数均大于等于它。当这样的好事做完后,新序列有三种情况(1)单调递增(2)单调递减(3)先增后减。这种情况价值最大的情况为序列和减去最大的两个数(绝对不坑)。
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- using namespace std;
- typedef long long LL;
- inline int min(int a,int b){ return a<b?a:b;}
- const int maxn=;
- int n,f[maxn];
- int main()
- {
- int i,top,x;
- LL ans;
- while(~scanf("%d",&n))
- {
- ans=;top=;
- for(i=;i<n;i++)
- {
- scanf("%d",&x);
- while(top>&&f[top-]>=f[top-]&&x>=f[top-])
- {
- ans+=min(x,f[top-]);
- top--;
- }
- f[top++]=x;
- }
- sort(f,f+top);
- for(i=;i<top-;i++) ans+=f[i];
- printf("%lld\n",ans);
- }
- return ;
- }
codeforces练习的更多相关文章
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
- CodeForces - 148D Bag of mice
http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...
- CodeForces - 453A Little Pony and Expected Maximum
http://codeforces.com/problemset/problem/453/A 题目大意: 给定一个m面的筛子,求掷n次后,得到的最大的点数的期望 题解 设f[i]表示掷出 <= ...
随机推荐
- Python——序列封包与序列解包
一.序列封包与序列解包 把多个值赋给一个变量时,Python会自动的把多个值封装成元组,称为序列封包. 把一个序列(列表.元组.字符串等)直接赋给多个变量,此时会把序列中的各个元素依次赋值给每个变量, ...
- LiteIDE 错误: 进程无法启动
问题 运行 01_hello.go,提示以下错误 新建文件夹().exe [C:/Users/Administrator/Desktop/新建文件夹()] 错误: 进程无法启动. 原因 工程目录名不能 ...
- php常见验证
/** * 文件上传 * @param $file 要上传的文件 * @param $size 大小设置 * @param $ext 文件类型 * @return bool 是否上传成功 */func ...
- 01_8_Struts用DomainModel接收参数
01_8_Struts用DomainModel接收参数 1. 配置struts.xml文件 <package name="user" namespace="/use ...
- Unity学习之路——主要类
学习https://blog.csdn.net/VRunSoftYanlz/article/details/78881752 1.Component类gameObject:组件附加的游戏对象.组件总是 ...
- leetcode-26-exercise_linked-list
141. Linked List Cycle Given a linked list, determine if it has a cycle in it. 解题思路: 需要检查before和afte ...
- poj-1011 sticks(搜索题)
George took sticks of the same length and cut them randomly until all parts became at most 50 units ...
- C#语言入门
1.基础知识 2.数据类型 3.控制语句 4.
- ubuntu更新内核后卡在自检无法开机的解决方法
下载deb包安装,重启后卡在自检,黑屏. 重启进旧内核,仍然卡在自检,黑屏. 强制关机后再重启,在grub按e修改启动项,改成直接进命令行模式.使用 sudo apt-get remove linux ...
- CSS3中的border-radius兼容IE低版本解决方法
ie-css3.htc先说道说道这斯是弄啥嘞ie-css3.htc是一个可以让IE浏览器支持部份CSS3属性的htc文件,不只是box-shadow,它还可以让你的IE浏览器支持圆角属性border- ...