1187: 零起点学算法94——今年暑假不AC(Java)
1187:零起点学算法94——今年暑假不AC
Time Limit: 1 Sec Memory Limit: 32 MB 64bit IO Format: %lld
Description
“今年暑假不AC?”
“是的。”
“那你干什么呢?”
“看世界杯呀,笨蛋!”
“@#$%^&*%…”
确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视了。
作为球迷,一定想看尽量多的完整的比赛,当然,作为新时代的好青年,你一定还会看一些其它的节目,比如新闻联播(永远不要忘记关心国家大事)、非常6+7、超级女生,以及王小丫的《开心辞典》等等,假设你已经知道了所有你喜欢看的电视节目的转播时间表,你会合理安排吗?(目标是能看尽量多的完整节目)
Input
输入数据包含多个测试实例,每个测试实例的第一行只有一个整数n(n<=100),表示你喜欢看的节目的总数,然后是n行数据,每行包括两个数据Ti_s,Ti_e (1<=i<=n),分别表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。n=0表示输入结束,不做处理。
提示:Ti_s<=Ti_e
Output
对于每个测试实例,输出能完整看到的电视节目的个数,每个测试实例的输出占一行。
Sample Input
12
1 3
3 4
0 7
3 8
15 19
15 20
10 15
8 18
6 12
5 10
4 14
2 9
0
Sample Output
5
代码如下
import java.util.Scanner;
public class Main {
Scanner sc;
int n, i;
// 存储节目开始时间和结束时间
short[][] time;
// 最终可以看的节目数量
int number;
public Main() {
sc = new Scanner(System.in);
n = sc.nextInt();
while(n > 0) {
// 输入时间
input();
// 排序
sort();
// 统计可以看的节目数量
number = statistics();
System.out.println(number);
n = sc.nextInt();
}
sc.close();
}
/**
* 输入节目的开始时间和结束时间
*/
private void input() {
time = new short[n][2];
for(i = 0; i < n; i++) {
time[i][0] = sc.nextShort();
time[i][1] = sc.nextShort();
}
}
/**
* 节目排序。规则如下:
* 1、结束时间早的放在前面
* 2、结束时间相同,则开始时间早的放在前面
* 算法:选择排序
*/
private void sort() {
int j, min;
for(i = 0; i < n; i++) {
min = i;
for(j = i; j < n; j++) {
if(time[min][1] > time[j][1]) {
min = j;
} else if(time[min][1] == time[j][1]){
if(time[min][0] > time[j][0]) {
min = j;
}
}
}
if(min != i) {
swap(min, i);
}
}
}
/**
* 交换节目位置,即交换时间
* @param a 需要交换的第一组数据的下标
* @param b 需要交换的第二组数据的下标
*/
private void swap(int a, int b) {
short temp;
temp = time[a][0];
time[a][0] = time[b][0];
time[b][0] = temp;
temp = time[a][1];
time[a][1] = time[b][1];
time[b][1] = temp;
}
/**
* @return 返回统计的可以看的节目数量
*/
private int statistics() {
int number = 1;
int previous = 0;
for(i = 1; i < n; i++) {
if(time[i][0] >= time[previous][1]) {
number = number + 1;
previous = i;
}
}
return number;
}
public static void main(String[] args) {
new Main();
}
}
1187: 零起点学算法94——今年暑假不AC(Java)的更多相关文章
- Problem D: 零起点学算法94——输出矩阵
#include<stdio.h> int main() { ][]; while(scanf("%d %d",&n,&m)!=EOF) { ; ;i& ...
- 1164: 零起点学算法71——C语言合法标识符(存在问题)
1164: 零起点学算法71——C语言合法标识符 Time Limit: 1 Sec Memory Limit: 64 MB 64bit IO Format: %lldSubmitted: 10 ...
- 1163: 零起点学算法70——Yes,I can!
1163: 零起点学算法70--Yes,I can! Time Limit: 1 Sec Memory Limit: 64 MB 64bit IO Format: %lldSubmitted: ...
- 1147: 零起点学算法54——Fibonacc
1147: 零起点学算法54--Fibonacc Time Limit: 1 Sec Memory Limit: 64 MB 64bit IO Format: %lldSubmitted: 20 ...
- 1145: 零起点学算法52——数组中删数II
1145: 零起点学算法52--数组中删数II Time Limit: 1 Sec Memory Limit: 64 MB 64bit IO Format: %lldSubmitted: 293 ...
- 1137: 零起点学算法44——多组测试数据输出II
1137: 零起点学算法44--多组测试数据输出II Time Limit: 1 Sec Memory Limit: 64 MB 64bit IO Format: %lldSubmitted: ...
- 1136: 零起点学算法43——多组测试数据输出I
1136: 零起点学算法43--多组测试数据输出I Time Limit: 1 Sec Memory Limit: 128 MB 64bit IO Format: %lldSubmitted: ...
- 1135: 零起点学算法42——多组测试数据(求和)IV
1135: 零起点学算法42--多组测试数据(求和)IV Time Limit: 1 Sec Memory Limit: 64 MB 64bit IO Format: %lldSubmitted ...
- 1134: 零起点学算法41——多组测试数据(a+b)III
1134: 零起点学算法41--多组测试数据(a+b)III Time Limit: 1 Sec Memory Limit: 64 MB 64bit IO Format: %lldSubmitt ...
随机推荐
- Ansible 模式
一.Ansible 命令 1.Ansible 命令执行的方式有两种:Ad-Hoc.Ansible-playbooks,这两种方式没有本质的区别,Ad-Hoc用于临时执行命令:Ansible-playb ...
- meshing-八分之一圆球
原视频下载地址:https://yunpan.cn/cqwiFDCg6PbFj 访问密码 d1c8
- lucene IndexOptions可以设置DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS DOCS,ES里也可以设置
org.apache.lucene.index Enum Constants Enum Constant and Description DOCS_AND_FREQS Only documents ...
- HttpClient学习(三)—— AsyncHttpClient使用
一.介绍 This class support asynchronous and synchronous HTTP requests. AsyncHttpClient 支持同步.异步Http请求. 二 ...
- Linux堆的一些基础知识
目录 堆的概述 什么是堆 堆的基本操作 堆操作背后的系统调用 堆的相关数据结构 微观结构 malloc_chuck chunk相关宏 bin 宏观结构 arena heap_info malloc_s ...
- .NET开发人员的完美.gitignore文件
# Build and Object Folders bin/ obj/ # Nuget packages directory packages/ ## Ignore Visual Studio te ...
- ML_Review_PCA(Ch4)
Note sth about PCA(Principal Component Analysis) ML6月20日就要考试了,准备日更博客,来记录复习一下这次ML课所学习的一些方法. 博客是在参考老 ...
- CentOS 修改固定IP地址
CentOS 修改固定IP地址 参考地址:https://www.cnblogs.com/technology-huangyan/p/9146699.htmlhttps://blog.csdn.net ...
- OpenJudge计算概论-数组逆序重放
/*=============================================================== 数组逆序重放 总时间限制: 1000ms 内存限制: 65536kB ...
- OpenJudge计算概论-分离整数的各个数位
/*================================================================= 分离整数的各个数位 总时间限制: 1000ms 内存限制: 65 ...