题目链接:Codeforces Round #298 (Div. 2)

A. Exam

An exam for n students will take place in a long and narrow room, so the students will sit in a line in some order. The teacher suspects that students with adjacent numbers (i and i + 1) always studied side by side and became friends and if they take an exam sitting next to each other, they will help each other for sure.

Your task is to choose the maximum number of students and make such an arrangement of students in the room that no two students with adjacent numbers sit side by side.

Input

A single line contains integer n (1 ≤ n ≤ 5000) — the number of students at an exam.

Output

In the first line print integer k — the maximum number of students who can be seated so that no two students with adjacent numbers sit next to each other.

In the second line print k distinct integers a1, a2, ..., ak (1 ≤ ai ≤ n), where ai is the number of the student on the i-th position. The students on adjacent positions mustn't have adjacent numbers. Formally, the following should be true: |ai - ai + 1| ≠ 1 for all i from 1 tok - 1.

If there are several possible answers, output any of them.

题意描述:给出n个学生,编号为1~n,要求学生坐在一排并且相邻的学生编号相差不能为1,求出满足这样要求的最大学生数量并输出方案。

算法分析:对于一个大于4的数字,我们可以把它的奇数放在前面,把偶数放在后面,这样很显然不会出现相邻的数字差值为1,然后特判一下1,2,3,4的情况即可。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
using namespace std; int n; int main()
{
while (scanf("%d",&n)!=EOF)
{
if (n==) {printf("1\n1\n");continue; }
if (n==) {printf("1\n1\n");continue; }
if (n==) {printf("2\n1 3\n");continue; }
if (n==) {printf("4\n2 4 1 3\n");continue; }
printf("%d\n",n);
printf("");
for (int i= ;i<=n ;i+=) printf(" %d",i);
for (int i= ;i<=n ;i+=) printf(" %d",i);
printf("\n");
}
return ;
}

B. Covered Path

The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v1 meters per second, and in the end it is v2 meters per second. We know that this section of the route took exactly t seconds to pass.

Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.

Input

The first line contains two integers v1 and v2 (1 ≤ v1, v2 ≤ 100) — the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.

The second line contains two integers t (2 ≤ t ≤ 100) — the time when the car moves along the segment in seconds, d (0 ≤ d ≤ 10) — the maximum value of the speed change between adjacent seconds.

It is guaranteed that there is a way to complete the segment so that:

  • the speed in the first second equals v1,
  • the speed in the last second equals v2,
  • the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
Output

Print the maximum possible length of the path segment in meters.

题意描述:给出初速度v1和末速度v2、运动时间t和相邻秒数之间速度可以增长的最大值d,每一秒钟我们看作速度不变,在秒数之间的速度变化可以看作是瞬间的(即第一秒的速度5,在允许增长的前提下,我们可以看作第二秒的速度已经是8),求出行驶的最远长度。

算法分析:这道题很简单,但在做的时候还是有点绕的。我们首先分析v1和v2的情况:

v1 < v2 :然后还得分两种情况:v1在t秒刚好增长到v2;v1在s(s<t)秒增长到v2,然后在s~t之间先增长后降低。前者速度曲线是直线向上,后者速度曲线是先增长到一个峰值,然后下降。

v1 == v2 :这种情况直接和v1<v2中第二种情形s~t秒内一样。

v1 > v2 :和第一种情况相反。我们从v2到v1这样看,就是和第一种情况一样了。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
const int maxn=+; int n;
int v1,v2,t,d; int main()
{
while (scanf("%d%d%d%d",&v1,&v2,&t,&d)!=EOF)
{
int ans=;
int q=,h=t;
if (v1<v2)
{
while (q<=h)
{
ans += v1;
v1 += d;
q++;
if (q==h) v1=v2;
else if (abs(v1-v2)<=d) break;
}
if (q>=h) {printf("%d\n",ans);continue; }
for ( ;q<h ;q++,h--)
{
ans += v1;
ans += v2;
v1 += d;
v2 += d;
}
if (q==h) ans += min(v1,v2);
printf("%d\n",ans);
}
else if (v1>v2)
{
while (q<=h)
{
ans += v2;
v2 += d;
h--;
if (h==q) v2=v1;
else if (abs(v1-v2)<=d) break;
}
if (q>h) {printf("%d\n",ans);continue; }
for ( ;q<h ;q++,h--)
{
ans += v1;
ans += v2;
v1 += d;
v2 += d;
}
if (q==h) ans += min(v1,v2);
printf("%d\n",ans);
}
else
{
for ( ;q<h ;q++,h--)
{
ans += v1;
ans += v2;
v1 += d;
v2 += d;
}
if (q==h) ans += min(v1,v2);
printf("%d\n",ans);
}
}
return ;
}

C. Polycarpus' Dice

Polycarp has n dice d1, d2, ..., dn. The i-th dice shows numbers from 1 to di. Polycarp rolled all the dice and the sum of numbers they showed is A. Agrippina didn't see which dice showed what number, she knows only the sum A and the values d1, d2, ..., dn. However, she finds it enough to make a series of statements of the following type: dice i couldn't show number r. For example, if Polycarp had two six-faced dice and the total sum is A = 11, then Agrippina can state that each of the two dice couldn't show a value less than five (otherwise, the remaining dice must have a value of at least seven, which is impossible).

For each dice find the number of values for which it can be guaranteed that the dice couldn't show these values if the sum of the shown values is A.

Input

The first line contains two integers n, A (1 ≤ n ≤ 2·105, n ≤ A ≤ s) — the number of dice and the sum of shown values where s = d1 + d2 + ... + dn.

The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 106), where di is the maximum value that the i-th dice can show.

Output

Print n integers b1, b2, ..., bn, where bi is the number of values for which it is guaranteed that the i-th dice couldn't show them.

题意描述:有n个骰子d1,d2,,,,dn,第i个骰子有di面(值为1~di),通过n个骰子摇出一个值A,然后求出每个骰子不可能出现的值的个数。

算法分析:如果有5个骰子,前4个骰子最小值的和为4,最大值的和为10,现在摇出的值A为13,那么第5个骰子出现的最小值必须得是3,最大值为9。根据每个骰子都这样的想法,然后就可以AC了。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
typedef long long LL;
const LL maxn=+; LL n,A;
LL an[maxn]; int main()
{
while (scanf("%I64d%I64d",&n,&A)!=EOF)
{
LL sum=;
for (LL i= ;i<=n ;i++)
{
scanf("%I64d",&an[i]);
sum += an[i];
}
LL l=n-;
for (LL i= ;i<=n ;i++)
{
l=n-;
LL r=sum-an[i];
LL right=A-l;
LL left= r>=A ? : A-r;
LL num=right-left+;
if (i>) printf(" ");
if (right<=an[i]) printf("%d",an[i]-num);
else if (left<=an[i] && an[i]<right) printf("%d",left-);
else printf("%d",an[i]);
}
printf("\n");
}
return ;
}

Codeforces Round #298 (Div. 2) A、B、C题的更多相关文章

  1. Codeforces Round #524 (Div. 2)(前三题题解)

    这场比赛手速场+数学场,像我这样读题都读不大懂的蒟蒻表示呵呵呵. 第四题搞了半天,大概想出来了,但来不及(中途家里网炸了)查错,于是我交了两次丢了100分.幸亏这次没有掉rating. 比赛传送门:h ...

  2. Codeforces Round #298 (Div. 2) E. Berland Local Positioning System 构造

    E. Berland Local Positioning System Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.c ...

  3. Codeforces Round #298 (Div. 2) D. Handshakes 构造

    D. Handshakes Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/534/problem ...

  4. Codeforces Round #298 (Div. 2) C. Polycarpus' Dice 数学

    C. Polycarpus' Dice Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/534/p ...

  5. Codeforces Round #298 (Div. 2) B. Covered Path 物理题/暴力枚举

    B. Covered Path Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/534/probl ...

  6. Codeforces Round #298 (Div. 2) A. Exam 构造

    A. Exam Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/534/problem/A Des ...

  7. Codeforces Round #298 (Div. 2) B. Covered Path

    题目大意: 一辆车,每秒内的速度恒定...第I秒到第I+1秒的速度变化不超过D.初始速度为V1,末速度为V2,经过时间t,问最远能走多远. 分析 开始的时候想麻烦了.讨论了各种情况.后来发现每个时刻的 ...

  8. CodeForces Round #298 Div.2

    A. Exam 果然,并没有3分钟秒掉水题的能力,=_=|| n <= 4的时候特判.n >= 5的时候将奇数和偶数分开输出即可保证相邻的两数不处在相邻的位置. #include < ...

  9. Codeforces Round #298 (Div. 2)--D. Handshakes

    #include <stdio.h> #include <algorithm> #include <set> using namespace std; #defin ...

随机推荐

  1. 3282: Tree(LCT)

    3282: Tree Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 2249  Solved: 1042[Submit][Status][Discus ...

  2. hdu1950Bridging signals(求最长上升自序列nlogn算法)

    Bridging signals Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. Neural Network

    逻辑回归用神经网络节点的方式表示 前面已经介绍过逻辑回归的模型,样本为(x,y) 其中y的值为1或0,假设x有2个特征,则对应关系如下图所示.  实际情况是需要求需要三个参数,因此输入层需要添加一个 ...

  4. PostgreSQL 配置内存参数

    对于任何数据库软件,内存配置项都是很重要的配置项.在 PostgreSQL 主要有以下几个内存配置参数. shared_buffers: integer 类型,设置数据库服务器将使用的共享内存缓冲区数 ...

  5. 【LeetCode】汉明距离(Hamming Distance)

    这道题是LeetCode里的第461道题. 题目描述: 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x,  ...

  6. Hadoop第三课

    1.3Hadoop基础知识 1.3.1术语解释 1.Hadoop1.0 • 第一代Hadoop,由分布式文件系统HDFS 和分布式计算框架MapReduce组成 • HDFS由一个NameNode和多 ...

  7. 集训队日常训练20181117 DIV2

    大佬们一顿操作猛如虎,拼命AC强啊 4262: 区间异或  Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByteTotal ...

  8. JAVA 程序监控基础简述

    最近在项目中自感程序木有问题,也没有什么错误日志出来.但就是有人反映服务慢,有时连不上的情况.为了解决这么妖的问题只能去详细的看看运行中的程序到底出了什么情况,这时如果有个比较好的监控工具可以监控运行 ...

  9. 现代互联网的TCP拥塞控制(CC)算法评谈

    动机 写这篇文章本质上的动机是因为前天发了一个朋友圈,见最后的写在最后,但实际上,我早就想总结总结TCP拥塞控制算法点点滴滴了,上周总结了一张图,这周接着那些,写点文字. 前些天,Linux中国微信公 ...

  10. 报错:3 字节的 UTF-8 序列的字节 3 无效。

    错误如下: Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with n ...