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 ...
随机推荐
- Python数据结构学习
列表 Python中列表是可变的,这是它区别于字符串和元组的最重要的特点,一句话概括即:列表可以修改,而字符串和元组不能. 以下是 Python 中列表的方法: 方法 描述 list.append(x ...
- (转载):ganglia之环境搭建部署
转载:http://www.360doc.com/content/19/0211/12/62122823_814215724.shtml 借鉴:https://blog.csdn.net/lswnew ...
- win10下子系统的倒腾记录
本文是对于win10下安装ubuntu 18.04的安装.配置c语言开发环境的记录: 一.win10下面的设置,从win10的1709开始,可以完整的搞定linux子系统,如果win10版本不够的同学 ...
- SpringCloud学习整理
参考文档 [1]: Spring Cloud Ribbon负载均衡
- Spring tools
sts是什么? sts是spring tool suite的缩写,是基于eclipse的.开发spring应用的定制的开发环境. 提供了什么? 实现.调试.运行.部署spring应用的现成的环境.包括 ...
- oracle查询表指定字段类型
查询表某字段类型,如下: SELECT data_type FROM all_tab_cols WHERE table_name = UPPER('SRIS_P_BaseInfo') and colu ...
- Vue的axios如何全局注册
最近用 Vue 写项目的时候,用到 axios ,因为 axios 不能用 Vue.use() ,所以在每个 .vue 文件中使用 axios 时就需要 import , .vue 文件少的话还好说, ...
- VBScript把json字符串解析成json对象的2个方法
这篇文章主要介绍了VBScript把json字符串解析成json对象的2个方法,本文通过MSScriptControl.ScriptControl和jscript实现,需要的朋友可以参考下 asp/v ...
- 001-java 设计模式概述
一.概述 思维导图 GoF(“四人帮”,又称Gang of Four,即Erich Gamma, Richard Helm, Ralph Johnson & John Vlissides) 1 ...
- IfcAxis2Placement3D IFC构件的位置和方向
IfcAxis2Placement3D定义了三维空间中物体的位置和方向,由三部分组成: The attribute Axis defines the Z direction, RefDirection ...