CodeForces刷题:Power Consumption Calculation、Increasing Sequence

记录CodeForces刷题C语言QAQ


一、Power Consumption Calculation

题面翻译

有一台电脑,在活跃状态下,每秒钟耗能 P 1 P1 P1,在没人碰之后的 T 1 T1 T1 秒后,进入休息状态,每秒钟耗能 P 2 P2 P2,如果再有 T 2 T2 T2 秒没人碰的话,就会进入睡眠状态,每秒钟耗能 P 3 P3 P3

给你这个人碰电脑的区间时间,问你这台电脑的耗能是多少

感谢@Dreamer丶杨 提供的翻译

题目描述

Tom is interested in power consumption of his favourite laptop. His laptop has three modes. In normal mode laptop consumes $ P_{1} $ watt per minute. $ T_{1} $ minutes after Tom moved the mouse or touched the keyboard for the last time, a screensaver starts and power consumption changes to $ P_{2} $ watt per minute. Finally, after $ T_{2} $ minutes from the start of the screensaver, laptop switches to the “sleep” mode and consumes $ P_{3} $ watt per minute. If Tom moves the mouse or touches the keyboard when the laptop is in the second or in the third mode, it switches to the first (normal) mode. Tom’s work with the laptop can be divided into $ n $ time periods $ [l_{1},r_{1}],[l_{2},r_{2}],…,[l_{n},r_{n}] $ . During each interval Tom continuously moves the mouse and presses buttons on the keyboard. Between the periods Tom stays away from the laptop. Find out the total amount of power consumed by the laptop during the period $ [l_{1},r_{n}] $ .

输入格式

The first line contains 6 integer numbers $ n $ , $ P_{1} $ , $ P_{2} $ , $ P_{3} $ , $ T_{1} $ , $ T_{2} $ ( $ 1<=n<=100,0<=P_{1},P_{2},P_{3}<=100,1<=T_{1},T_{2}<=60 $ ). The following $ n $ lines contain description of Tom’s work. Each $ i $ -th of these lines contains two space-separated integers $ l_{i} $ and $ r_{i} $ ( $ 0<=l_{i}<r_{i}<=1440 $ , $ r_{i}<l_{i+1} $ for $ i<n $ ), which stand for the start and the end of the $ i $ -th period of work.

输出格式

Output the answer to the problem.

样例 #1

样例输入 #1

1 3 2 1 5 10
0 10

样例输出 #1

30

样例 #2

样例输入 #2

2 8 4 2 5 10
20 30
50 100

样例输出 #2

570

代码如下

#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>

struct USE{
	int start;
	int end;
	int seconds;
}a[105];
int main(){
	int p1,p2,p3,t1,t2,n,ans=0;
    scanf("%d%d%d%d%d%d",&n,&p1,&p2,&p3,&t1,&t2);
	for(int i=1;i<=n;i++){
		scanf("%d%d",&a[i].start,&a[i].end);
		a[i].seconds=a[i].end-a[i].start;
		ans+=a[i].seconds*p1;
	}
	for(int i=1;i<n;i++){
		int t=a[i+1].start-a[i].end;
		if(t<=t1) ans+=p1*t;
		else if(t>t1 && t<=t2+t1) ans+=p1*t1+p2*(t-t1);
		else ans+=p1*t1+p2*t2+p3*(t-t1-t2);
	}
	printf("%d",ans);
	return 0;
}

二、Increasing Sequence

题面翻译

题面描述

把数列中一个数加上 d d d 算作一次操作,求最少几次操作能把这个数列变为递增数列。

输入格式

输入共 2 2 2 行。

第一行 2 2 2 个整数 n n n d d d n n n 表示数列中数的数量, d d d 含义如题目所示。

第二行 n n n 个整数,表示原始数列。

输出格式

一行一个整数 m m m,表示最少 m m m 次操作能把这个数列变为递增数列。

题目描述

A sequence $ a_{0},a_{1},…,a_{t-1} $ is called increasing if $ a_{i-1}<a_{i} $ for each $ i:0<i<t $ .

You are given a sequence $ b_{0},b_{1},…,b_{n-1} $ and a positive integer $ d $ . In each move you may choose one element of the given sequence and add $ d $ to it. What is the least number of moves required to make the given sequence increasing?

输入格式

The first line of the input contains two integer numbers $ n $ and $ d $ ( $ 2<=n<=2000,1<=d<=10^{6} $ ). The second line contains space separated sequence $ b_{0},b_{1},…,b_{n-1} $ ( $ 1<=b_{i}<=10^{6} $ ).

输出格式

Output the minimal number of moves needed to make the sequence increasing.

样例 #1

样例输入 #1

4 2
1 3 3 2

样例输出 #1

3

代码如下

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>        
#define ll long long int
using namespace std;
int main()
{
	ll n, i, d, a, b=0, ans=0; //a指前一个数, b指后一个数,ans存次数
	cin >> n >> d;
	for (i = 0; i < n; i++)
	{
		if(i==0) 
		    cin >> a;
		else
		{
			cin >> b;
			if (b <= a)
			{
				ans += ((a - b) / d + 1);  //如果b<=a,加上要加的次数
				a = b+((a - b) / d + 1)*d;  //将b更新后的值(即b>a后b的值)赋值给a,等新的数输入
			}
			else
			{
				a = b;  //原理同上,更新
			}
			
		}
	}
	cout << ans << endl;
	return 0;
}

三、Super Agent

题面翻译

给定一个 3 × 3 3 \times 3 3×3 的矩形,每个元素不是X就是.,问这个矩形是否是中心对称的。

题目描述

There is a very secret base in Potatoland where potato mash is made according to a special recipe. The neighbours from Porridgia decided to seize this recipe and to sell it to Pilauland. For this mission they have been preparing special agent Pearlo for many years. When, finally, Pearlo learned all secrets of espionage, he penetrated into the Potatoland territory and reached the secret base.

Now he is standing at the entrance, but to get inside he need to pass combination lock. Minute ago one of the workers entered the password on the terminal and opened the door. The terminal is a square digital keyboard $ 3×3 $ with digits from $ 1 $ to $ 9 $ .

Pearlo knows that the password consists from distinct digits and is probably symmetric with respect to the central button of the terminal. He has heat sensor which allowed him to detect the digits which the worker pressed. Now he wants to check whether the password entered by the worker is symmetric with respect to the central button of the terminal. This fact can Help Pearlo to reduce the number of different possible password combinations.

输入格式

Input contains the matrix of three rows of three symbols each. Symbol «X» means that the corresponding button was pressed, and «.» means that is was not pressed. The matrix may contain no «X», also it may contain no «.».

输出格式

Print YES if the password is symmetric with respect to the central button of the terminal and NO otherwise.

样例 #1

样例输入 #1

XX.
...
.XX

样例输出 #1

YES

样例 #2

样例输入 #2

X.X
X..
...

样例输出 #2

NO

提示

If you are not familiar with the term «central symmetry», you may look into http://en.wikipedia.org/wiki/Central_symmetry

代码如下



#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>




char a[4][4];
int main()
{
	scanf("%s",&a[1]);
	scanf("%s",&a[2]);
	scanf("%s",&a[3]);
	if(a[1][0] == a[3][2] && a[1][1] == a[3][2] && a[1][2] == a[3][0] && a[2][0] == a[2][2])
		{
			printf("YES\n");
			}	
			else
				{
					printf("NO\n");
				}
	return 0;			
 }

四、Letter

题面翻译

题目描述

给定一 N × M N \times M N×M 规模的矩阵,输出最小的包含所有 * 的矩阵。

输入格式

一行两个整数, N N N M M M

然后一个 N × M N \times M N×M 大小的矩阵。

输出格式

输出最小的包含所有 * 的矩阵。

数据范围

1 ≤ N , M ≤ 50 1 \leq N,M \leq 50 1N,M50

题目描述

A boy Bob likes to draw. Not long ago he bought a rectangular graph (checked) sheet with $ n $ rows and $ m $ columns. Bob shaded some of the squares on the sheet. Having seen his masterpiece, he decided to share it with his elder brother, who lives in Flatland. Now Bob has to send his picture by post, but because of the world economic crisis and high oil prices, he wants to send his creation, but to spend as little money as possible. For each sent square of paper (no matter whether it is shaded or not) Bob has to pay 3.14 burles. Please, help Bob cut out of his masterpiece a rectangle of the minimum cost, that will contain all the shaded squares. The rectangle’s sides should be parallel to the sheet’s sides.

输入格式

The first line of the input data contains numbers $ n $ and $ m $ ( $ 1<=n,m<=50 $ ), $ n $ — amount of lines, and $ m $ — amount of columns on Bob’s sheet. The following $ n $ lines contain $ m $ characters each. Character «.» stands for a non-shaded square on the sheet, and «*» — for a shaded square. It is guaranteed that Bob has shaded at least one square.

输出格式

Output the required rectangle of the minimum cost. Study the output data in the sample tests to understand the output format better.

样例 #1

样例输入 #1

6 7
.......
..***..
..*....
..***..
..*....
..***..

样例输出 #1

***
*..
***
*..
***

样例 #2

样例输入 #2

3 3
***
*.*
***

样例输出 #2

***
*.*
***

代码如下

#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>

char a[1001][1001];
int main()
{
	int x,y,i,j,xl=99999,yl=99999,xm=-1,ym=-1;
    //xl和yl表示开始的行数,和开始的列数,所以这里把99999定义为无穷大
    //xm和ym表示结束的行数,和结束的列数,当然,因为没有负数组,所以赋值为-1就好了。
	scanf("%d %d",&x,&y);
	for(i=0;i<=x-1;i++)
	{
		scanf("%s",a[i]);
		for(j=0;j<=y-1;j++)
		{
			if(a[i][j]=='*')
			{
				xl=xl<i? xl:i;//求开始的行数
                //如果当前的行数,比开始的行数小,那开始的行数,就变为当前的行数
                
				yl=j<yl? j:yl;//求开始的列数
                //如果当前的列数,比开始的列数小,那开始的列数,就变为当前的列数
                
				xm=i>xm? i:xm;//求结束的行数
                //如果当前的行数,比结束的行数大,那结束的行数,就变为当前的行数
                
				ym=j>ym? j:ym;//求结束的列数
                //如果当前的列数,比结束的列数小,那结束的列数,就变为当前的列数 
			}
		}
	}
	for(i=xl;i<=xm;i++)//根据开始行、结束行逐行输出
	{
		for(j=yl;j<=ym;j++)//根据每行的开始列、结束列逐列输出
		{
			printf("%c",a[i][j]);
		}
		printf("\n");//记得换行哦~
	}
	return 0;
} 

五、Flag

题面翻译

题目描述

根据一项新的 ISO 标准,每一个国家的国旗应该是一个 n × m n\times m n×m 的格子场,其中每个格子最多有 10 10 10 种不同的颜色。并且国旗应该有条纹:旗帜的每一行应包含相同颜色的方块,相邻的行的颜色应该是不同的。Berland 政府要求你找出他们的国旗是否符合新的 ISO 标准。

输入格式

输入的第一行包含数 n n n m m m,其中 n n n 为行数, m m m 为列数。

接下来是对旗的描述:以下 n n n 行中的每一行包含 m m m 个字符。每个字符是 0 0 0 9 9 9 之间的数字,代表相应正方形的颜色。

输出格式

如果国旗符合标准就输出 YES,否则输出 NO

题目描述

According to a new ISO standard, a flag of every country should have a chequered field $ n×m $ , each square should be of one of 10 colours, and the flag should be «striped»: each horizontal row of the flag should contain squares of the same colour, and the colours of adjacent horizontal rows should be different. Berland’s government asked you to find out whether their flag meets the new ISO standard.

输入格式

The first line of the input contains numbers $ n $ and $ m $ ( $ 1<=n,m<=100 $ ), $ n $ — the amount of rows, $ m $ — the amount of columns on the flag of Berland. Then there follows the description of the flag: each of the following $ n $ lines contain $ m $ characters. Each character is a digit between 0 and 9, and stands for the colour of the corresponding square.

输出格式

Output YES, if the flag meets the new ISO standard, and NO otherwise.

样例 #1

样例输入 #1

3 3
000
111
222

样例输出 #1

YES

样例 #2

样例输入 #2

3 3
000
000
111

样例输出 #2

NO

样例 #3

样例输入 #3

3 3
000
111
002

样例输出 #3

NO

代码如下

#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>


int n,m;
char flag[110][110];
int main(){
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;++i){
       	scanf("%s",flag[i]);
    }
    for(int i=0;i<n;++i){
        if(i!=n-1&&flag[i][0]==flag[i+1][0]){
            printf("NO\n");
            return 0;
        }
        for(int j=0;j<m;++j){
            if(flag[i][j]!=flag[i][0]){
                printf("NO\n");
                return 0;
            }
        }
    }
    printf("YES\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值