寒假训练(2)——贪心 D - Moving Tables

题目 https://vjudge.net/contest/149236#problem/D


首先 scanf时就swap 排序 很方便  

for(int j=0; j<N; j++)

{
scanf("%d %d",&begin,&end);
if(begin>end)
 swap(begin,end);
for(int k=(begin+1)/2; k<=(end+1)/2; k++)
 rooms[k]++;
}


k=(begin+1)/2; k<=(end+1)/2; k++

1 3 5

2 4 6

巧妙的避开了 奇偶数要写四个if的麻烦  


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<sstream>
#include<iostream>
#include<math.h>
#include<stdlib.h>
typedef long long ll;
using namespace std;


struct Node
{
int c;
int d;
int count;
}node[500];


bool cmp(Node x,Node y)
{
// if(x.d == y.d ) return x.c < y.c ;
return x.c < y.c ;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int m;
scanf("%d",&m);
for(int j=0;j<m;j++)
{
scanf("%d %d",&node[j].c  ,& node[j].d );
if(node[j].c >node[j].d )
swap(node[j].c ,node[j].d);
}
sort(node, node +m, cmp);
//ll mid =0;
for(int j=0;j<500;j++)
node[j].count =0;
/* 
for(int j=node[0].c ;j<= node[0].d ;j++)
node[j].count ++;
*/
for(int j=0;j<m;j++)
{
/*
if(node[j].c < node[j-1].d && node[j].d >node[j-1].d && node[j-1].c < node[j].c  ) //node[j].c node[j-1].d 在中间 
{
for(int k= node[j].c ;k<=node[j-1].d ;k++)
node[k].count  ++;
}
else if(node[j].c >node[j-1].c && node[j].d <node[j-1].d )// node[j] 整体在中间 
{
for(int k=node[j].c ;k<= node[j].d ;k++)
node[k].count  ++;
}
else if(node[j].c < node[j-1].c && node[j].d < node[j-1].d &&node[j].d >node[j-1].c)//node[j].d node[j-1].c 在中间 
{
for(int k= node[j-1].d ;k<=node[j].c ;k++)
node[k].count  ++;
}
else if(node[j].c < node[j-1].c && node[j].d > node[j-1].d )//node[j-1] 整体在中间 
{
for(int k= node[j-1].c ;k<=node[j-1].d ;k++)
node[k].count  ++;
}
*/
if(node[j].c % 2 == 1 && node[j].d % 2 == 1 ) // 1 5
for(int k=node[j].c ;k<= node[j].d+1 ;k++)
node[k].count  ++;

else if(node[j].c % 2 == 1 && node[j].d % 2 == 0  ) // 1 6
for(int k=node[j].c ;k<= node[j].d ;k++)
node[k].count  ++;

else if(node[j].c % 2 == 0 && node[j].d % 2 == 1  ) // 2 5
for(int k=node[j].c -1 ;k<= node[j].d+1 ;k++)
node[k].count  ++;

else if(node[j].c % 2 == 0 && node[j].d % 2 == 0  ) // 2 6
for(int k=node[j].c -1 ;k<= node[j].d ;k++)  
node[k].count  ++;

/* 
for(int k=node[j].c ;k<= node[j].d ;k++)
node[k].count  ++;
*/

}
int max= 0;
for(int j=0;j<401;j++)
{
//printf("%d\n",node[j].count );
if(node[j].count >max)
max = node[j].count ;
}

printf("%d\n",max*10);
}
return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值