题目描述

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 ;
}

[USACO13NOV]空荡荡的摊位Empty Stalls的更多相关文章

  1. Luogu3090 [USACO13NOV]空荡荡的摊位Empty Stalls (动态规划)

    #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...

  2. 解题:USACO13NOV Empty Stalls

    题面 当然可以用并查集做,不过你需要按秩合并+路径压缩(才可能过),因为数据范围十分不友好...... USACO的官方做法更为优秀.首先题目告诉我们牛们加入的前后顺序不影响结果(自己证明也很容易,显 ...

  3. USACO 2013 November Contest Gold 简要题解

    Problem 1. Empty Stalls 扫两遍即可. Problem 2. Line of Sight 我们发现能互相看见的一对点一定能同时看见粮仓的某一段.于是转换成有n段线段,问有多少对线 ...

  4. BZOJ-USACO被虐记

    bzoj上的usaco题目还是很好的(我被虐的很惨. 有必要总结整理一下. 1592: [Usaco2008 Feb]Making the Grade 路面修整 一开始没有想到离散化.然后离散化之后就 ...

  5. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  6. bzoj1651 / P2859 [USACO06FEB]摊位预订Stall Reservations

    P2859 [USACO06FEB]摊位预订Stall Reservations 维护一个按右端点从小到大的优先队列 蓝后把数据按左端点从小到大排序,顺序枚举. 每次把比右端点比枚举线段左端点小的数据 ...

  7. 洛谷 P3088 [USACO13NOV]挤奶牛Crowded Cows 题解

    P3088 [USACO13NOV]挤奶牛Crowded Cows 题目描述 Farmer John's N cows (1 <= N <= 50,000) are grazing alo ...

  8. 题解 P2859 【[USACO06FEB]摊位预订Stall Reservations】

    题目链接: https://www.luogu.org/problemnew/show/P2859 思路: 首先大家会想到这是典型的贪心,类似区间覆盖问题的思路,我们要将每段时间的左端点从小到大排序, ...

  9. [USACO06FEB]摊位预订Stall Reservations(贪心)

    [USACO06FEB]摊位预订Stall Reservations 题目描述 Oh those picky N (1 <= N <= 50,000) cows! They are so ...

随机推荐

  1. AJPFX总结方法重载与方法重写的区别

    方法重载在同一个类中,可以出现同名方法,但是这些同名方法的参数列表必须不同,这样定义方法叫做方法重载.方法重载的特点重载的注意事项重载与返回值无关重载与具体的变量标识符无关重载只与方法名与参数相关重载 ...

  2. AJPFX:不用递归巧妙求出1000的阶乘所有零和尾部零的个数

    package com.jonkey.test; import java.math.BigInteger; public class Test6 { /*** @param args*  需求:求出1 ...

  3. poj2385 Apple Catching

    思路: 简单dp. 实现: #include <iostream> #include <cstdio> #include <cstring> using names ...

  4. DOM编程练习(慕课网题目)

    编程练习 制作一个表格,显示班级的学生信息. 要求: 1. 鼠标移到不同行上时背景色改为色值为 #f2f2f2,移开鼠标时则恢复为原背景色 #fff 2. 点击添加按钮,能动态在最后添加一行 3. 点 ...

  5. Kotlin学习的一些笔记

    Introduction 写在前面 关于本书 这本书适合你吗? 关于作者 介绍 什么是Kotlin? 我们通过Kotlin得到什么 准备工作 Android Studio 安装Kotlin插件 创建一 ...

  6. jmeter 接口测试简介

    前言: 本文主要针对http接口进行测试,使用Jmeter工具实现. Jmter工具设计之初是用于做性能测试的,它在实现对各种接口的调用方面已经做的比较成熟,因此,本次直接使用Jmeter工具来完成对 ...

  7. DROP DOMAIN - 删除一个用户定义的域

    SYNOPSIS DROP DOMAIN name [, ...] [ CASCADE | RESTRICT ] DESCRIPTION 描述 DROP DOMAIN 将从系统表中删除一个用户域. 只 ...

  8. ubuntu卡机

    卡机了用ctrl+alt+t打开终端然后top看后台程序 最后kill -9 + PID就能把最影响问题的程序杀掉 我之前就杀了一个占100%cpu的程序

  9. 暑假集训 || AC自动机

    HDU 2222 题意:给n个模式串和一个字符串,求有多少个模式串在这个字符串中出现 思路:裸题,注意数组开的大小 #include <iostream> #include <cst ...

  10. 【转】解决WPF图片模糊最佳方法(绑定PixelWidth与PixelHeight)

    解决WPF图片模糊最佳方法(绑定PixelWidth与PixelHeight) 转载自:http://www.360doc.com/content/13/1126/09/10504424_332211 ...