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 ...
随机推荐
- create an oauth app
github可以对自己的服务进行oauth认证,创建oauth认证的方式如下: github -> Settings -> Developer settings -> Develop ...
- 将制定目录家到系统PATH环境变量中
1.打开系统文件sudo vim/etc/profile,请输入root密码 2.在appendpath '/usr/local/sbin' appendpath '/usr/local/bin' a ...
- GCC与GDB使用
GCC基本命令 gcc[选项][文件名] -E:仅执行编译预处理(.c->.i) -S:将c代码转换成汇编代码(.i->.s) -c:仅执行编译操作,不进行连接操作(.s->.o) ...
- BigDecimal 相关
一.BigDecimal 精度设置 BigDecimal setScale(int newScale, int roundingMode): newScale:小数位数, RoundingMode是一 ...
- CodeForces - 1175B Catch Overflow!(栈模拟多重for循环)
You are given a function ff written in some basic language. The function accepts an integer value, w ...
- 判断 js 类型的方式
1. typeof 可以判断出'string','number','boolean','undefined','symbol'但判断 typeof(null) 时值为 'object'; 判断数组和对 ...
- OpenTK学习笔记(2)-工作窗口的三种方法创建方法(控制台)
参考资料: 控制台下类的形式创建:http://www.cnblogs.com/podolski/p/7406628.html 总结: 一.控制台下类的形式创建 1.新建控制台应用 2.连网执行Nug ...
- pgpool 的配置文件详解
listen_addresses = 'localhost' # Host name or IP address to listen on: # '*' for all, '' for no TCP/ ...
- 远程控制软件 mRemoteNG,管理多台虚拟机
#下载 1.官网下载:https://mremoteng.org/2.当然也可以到这里下载:https://pan.baidu.com/s/11O_QNM3HudN3IyTiqqHmrQ,提取码:jm ...
- LC 450. Delete Node in a BST
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...