题目描述

Farmer John's new barn consists of a huge circle of N stalls (2 <= N <= 3,000,000), numbered 0..N-1, with stall N-1 being adjacent to stall 0.

At the end of each day, FJ's cows arrive back at the barn one by one, each with a preferred stall they would like to occupy. However, if a cow's preferred stall is already occupied by another cow, she scans forward sequentially from this stall until she finds the first unoccupied stall, which she then claims. If she scans past stall N-1, she continues scanning from stall 0.

Given the preferred stall of each cow, please determine the smallest index of a stall that remains unoccupied after all the cows have returned to the barn. Notice that the answer to this question does not depend on the order in which the cows return to the barn.

In order to avoid issues with reading huge amounts of input, the input to this problem is specified in a concise format using K lines (1 <= K <= 10,000) each of the form:

X Y A B

One of these lines specifies the preferred stall for XY total cows: X cows prefer each of the stalls f(1) .. f(Y), where f(i) = (Ai + B) mod N. The values of A and B lie in the range 0...1,000,000,000.

Do not forget the standard memory limit of 64MB for all problems.

约翰的谷仓中有N(2 <= N <=3,000,000)个房间,编号0到N-1,这些房间排布成环状,编号0的和编号N-1的相邻。

每天傍晚,奶牛们一只一只排队回到谷仓,每头奶牛都有一个喜欢的房间,但是,如果它喜欢的房间已被其他奶牛占了,它会向前挨个探索其他房间(如果它探索过了N-1号房间,它会继续探索0号房间,以此继续下去)直到探到第一个没有被占用的房间,这时它会宣布占用这个房间。

告诉你每头奶牛喜欢的房间,当所有奶牛都找到房间后,剩下的没被占用的房间中,编号最小的是哪个。很明显,问题的答案与奶牛进入谷仓的顺序无关。

为避免输入内容过多。本题的输入数据采用一种简洁的方式:一共K(1 <= K <=10,000)行,每行格式如下:

X Y A B

表示有Y批奶牛,每批X头,也就是总共X*Y只奶牛喜欢的房间号。Y批奶牛编号1到Y,第i批X头奶牛喜欢的房间号为(A*i+B) Mod N.

A和B的取值范围为0...1,000,000,000

注意,只有64M的空间。

输入输出格式

输入格式:

  • Line 1: Two space-separated integers: N and K.

  • Lines 2..1+K: Each line contains integers X Y A B, interpreted as
    above. The total number of cows specified by all these lines will be at
    most N-1. Cows can be added to the same stall by several of these
    lines.

输出格式:

  • Line 1: The minimum index of an unoccupied stall.

输入输出样例

输入样例#1:
复制

10 3
3 2 2 4
2 1 0 1
1 1 1 7
输出样例#1: 复制

5

说明

There are 10 stalls in the barn, numbered 0..9. The second line of input states that 3 cows prefer stall (2*1+4) mod 10 = 6, and 3 cows prefer stall (2*2+4) mod 10 = 8. The third line states that 2 cows prefer stall (0*1+1) mod 10 = 1. Line four specifies that 1 cow prefers stall (1*1+7) mod 10 = 8 (so a total of 4 cows prefer this stall).

All stalls will end up occupied except stall 5.

思路

f[i]指向向最近的一个空位;

一个类似并查集的find_father()维护;

代码

 #include<cstdio>
#include<cstring>
const int maxn=3e6+;
int n,k;
int x,y,a,b,c,d;
int f[maxn];
int ff(int k){return f[k]==f[n]?k:f[k]=ff(f[k]);}
int main(){
freopen("empty.in","r",stdin);
freopen("empty.out","w",stdout);
scanf("%d%d",&n,&k);
memset(f,0x7f,sizeof(f));
for(int i=;i<=k;i++){
scanf("%d%d%d%d",&x,&y,&a,&b);
for(int i=;i<=y;i++){
c=(1ll*a*i+b)%n;
for(int j=;j<x;j++){
d=ff((c+j)%n);
f[d]=ff((d+)%n);
}
}
}
printf("%d",ff());
return ;
}

最新文章

  1. jmeter之线程组的使用
  2. Z-STACK在CC2530上同时使用两个串口
  3. 了解vmware tools
  4. 微信自定义菜单view类型获取openid访问网页
  5. ISO 9141-2 and ISO 14230-2 INITIALIZATION and DATA TRANSFER
  6. 父子进程间通信模型实现(popen)
  7. (转载)HTTP URL
  8. 谈谈react-router学习
  9. c语言-转义序列
  10. JNI 对象处理 (转)
  11. redis -list
  12. Tomcat8源码笔记(五)组件Container分析
  13. centos7下安装vnc更改vnc默认端口号
  14. linux下查询java进程以及杀掉其进程
  15. c#基础系列3---深入理解ref 和out
  16. mysql误删数据快速恢复
  17. python学习 day011打卡 迭代器
  18. legend2---开发日志1(legend的数据库整体设计思路是什么)
  19. CSS 仿 iOS 系统通知数字样式
  20. 新手学习爬虫之创建第一个完整的scrapy工程-糗事百科

热门文章

  1. 迭代器模式及php实现
  2. Windows 2016 安装Sharepoint 2016 预装组件失败
  3. 如何在win7、win8、win8.1上安装使用vb6.0
  4. 一个Java编写的小玩意儿---多人在线聊天工具
  5. DAX:New and returning customers
  6. 如何破解密码的哈希值,破解双MD5密码值
  7. H3C S5024P交换机 H3C AR28-31路由器命令
  8. edquota - 编辑用户配额
  9. vueCode 常用代码总结 20190116
  10. ios之数据持久化