第五道ACM程序题
2016-03-22 23:20
253 查看
1.题目编号:1004
2.简单题意:给出n个你喜欢的节目,并提供所有节目的开始时间和结束时间,需要做的是将这些节目尽可能多地合理安排
3.简单思路形成过程:看到这个题目,首先想到用贪心算法:用一个结构体写出节目的数据类型,包括它的开始时间、结束时间和标号。通过构造一个比较函数按结束时间的大小排序,再用一个函数将合理安排的节目数输出,主函数直接调用函数就可以了
4.感悟:终于写了第一道用贪心算法写的程序了,本专题学的就是贪心算法,通过这道题可以测验自己的贪心算法是不是学到了精髓,还有哪些没有掌握住,虽然wa了很多次,但是每次都能学习一点,还是挺欣慰的,哈哈,学习就是一个反复练习不断出错的过程,我大体看了一下,还有好几道没做的题目都是需要用贪心算法做的,耐着性子去做吧,fighting~
5.AC的代码:
#include <iostream>
#include <algorithm>
#include <memory.h>
using namespace std;
struct perform{
int Ti_s;
int Ti_e;
int index;
};
bool cmp(const perform &a,const perform &b){
if (a.Ti_e<b.Ti_e)
return true ;
return false;
}
void choice (int n,perform a[],bool b[]){
b[1]=true;
int preend=1;
for (int i=2;i<=n;i++){
if (a[i].Ti_s>=a[preend].Ti_e){
b[i]=true;
preend=i;
}
}
}
void acount(bool b[],int n){
int c=0;
for (int i=1;i<=n;i++){
if(b[i]==true)
{c++;}
}
cout<<c<<endl;
}
int main ()
{
perform c[100000];
bool d[100000];
int n;
while (cin >>n&&n!=0){
memset (c,0,sizeof(c));
memset (d,false,sizeof(d));
for(int i=0;i<n;i++){
cin>>c[i].Ti_s>>c[i].Ti_e;
c[i].index=i+1;
}
sort (c,c+n+1,cmp);
choice(n,c,d);
acount(d,n);
}
return 0;
}
2.简单题意:给出n个你喜欢的节目,并提供所有节目的开始时间和结束时间,需要做的是将这些节目尽可能多地合理安排
3.简单思路形成过程:看到这个题目,首先想到用贪心算法:用一个结构体写出节目的数据类型,包括它的开始时间、结束时间和标号。通过构造一个比较函数按结束时间的大小排序,再用一个函数将合理安排的节目数输出,主函数直接调用函数就可以了
4.感悟:终于写了第一道用贪心算法写的程序了,本专题学的就是贪心算法,通过这道题可以测验自己的贪心算法是不是学到了精髓,还有哪些没有掌握住,虽然wa了很多次,但是每次都能学习一点,还是挺欣慰的,哈哈,学习就是一个反复练习不断出错的过程,我大体看了一下,还有好几道没做的题目都是需要用贪心算法做的,耐着性子去做吧,fighting~
5.AC的代码:
#include <iostream>
#include <algorithm>
#include <memory.h>
using namespace std;
struct perform{
int Ti_s;
int Ti_e;
int index;
};
bool cmp(const perform &a,const perform &b){
if (a.Ti_e<b.Ti_e)
return true ;
return false;
}
void choice (int n,perform a[],bool b[]){
b[1]=true;
int preend=1;
for (int i=2;i<=n;i++){
if (a[i].Ti_s>=a[preend].Ti_e){
b[i]=true;
preend=i;
}
}
}
void acount(bool b[],int n){
int c=0;
for (int i=1;i<=n;i++){
if(b[i]==true)
{c++;}
}
cout<<c<<endl;
}
int main ()
{
perform c[100000];
bool d[100000];
int n;
while (cin >>n&&n!=0){
memset (c,0,sizeof(c));
memset (d,false,sizeof(d));
for(int i=0;i<n;i++){
cin>>c[i].Ti_s>>c[i].Ti_e;
c[i].index=i+1;
}
sort (c,c+n+1,cmp);
choice(n,c,d);
acount(d,n);
}
return 0;
}
相关文章推荐
- CSS一些设置用法
- bzoj 3131: [Sdoi2013]淘金
- js正则表达式基础(未完待续)
- read 用法
- scrapy安装问题及解决(win10 x64)
- Oracle数据库学习(二)--值类型操作
- 作业四
- 结对编程—词频统计
- Grub Rescue 修复引导
- 连接SVN,出现如下错误:由于目标机器积极拒绝,无法连接Unable to connect to a repository at URL 禁止访问 (forbidden)
- 16.03.22【练习】用read写一个脚本
- POJ 1679 The Unique MST (次小生成树)
- 使用HBase EndPoint(coprocessor)进行计算
- LeetCode 231 Power of Two
- 在AS中添加第三方jar和.so库
- XtraBackup出现 Can't connect to local MySQL server through socket '/tmp/mysql.sock'
- CodeForces-632A-Grandma Laura and Apples
- 作业:c++作业2-标准体重
- Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds...
- Android Stdio程序在虚拟机运行出现enabled ADB integration to be ebabled错误