17995 Stupid thief

时间限制:1000MS  内存限制:65535K
提交次数:0 通过次数:0

题型: 编程题   语言: 不限定

Description

A stupid thief has just discovered the treasure in a cave. There are N bottles filled with gold coins.
Because of darkness, he can't see the exact number of coins in each bottle. He only knows the range of the coins’ number in each bottle.
The lower bound of i-th bottle is low[i] and the upper bound is up[i].
Now, He decides to take some of the bottles( at least one bottle) . He wants to know how many kinds of possible coins’ number he would take.

输入格式

The input will contain multiple cases.
The first line is a integer T, the number of test cases,at most 100 cases.
Then T cases follow.
The first line of each case, is an integer N (1<=N<=15), that represents the number of bottles.
Then N lines follow, each contains two integers low[i] and up[i] (1<=low[i]<=up[i]<=1000000), that represents the i-th bottle’s lower bound and upper bound.

输出格式

For each test case, print the answer in a single line, which is the number of possible coins’ number he would take.

输入样例

3
1
10 15
2
3 5
4 5
3
2 2
3 5
4 8

输出样例

6
7
14

提示

In the first sample, the possible coin number is 10,11,12,13,14,15;
In the second sample,the possible coin number is 3,4,5,7,8,9,10;
In the three sample,the possible coin number is the range [2,15];

来源

Farmer

http://acm.scau.edu.cn:8000/uoj/mainMenu.html

给定N个区间,N <= 15,然后每个区间就是[L, R]这个范围,要求你任取x个区间,相加后得到的数字有多少种不同的结果。

考虑只选一个区间,那么数字就是[L, R]

选两个的话,[L[1], R[1]] 和 [L[2], R[2]]能得到的结果就是[L[1] + L[2], R[1] + R[2]]

因为区间的数字是连续的,所以意思就是选取最小的和最小的相加就是下界,最大的和最大的相加就是上界。

那么可以暴力枚举所有不同的组合,得到2^n - 1个范围,要对这些范围进行去重即可。

去重就是

[1, 2]和[1, 10]的,算作[1, 10]、因为问的只是种类嘛。

然后首先对L排序,再合并。

保证L最小后,区间合并的概率最大,也是最优。

排序R的话,会有bug

比如排序完后是

1, 2

3, 3

1, 10

等。

数据

3

1 2

3 3

1 10

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = + ;
struct node {
LL L, R;
bool operator < (const struct node & rhs) const {
return L < rhs.L;
}
}query[ << maxn], b[maxn];
int slc[maxn];
void work() {
int n;
scanf("%d", &n);
for (int i = ; i <= n; ++i) {
scanf("%lld%lld", &b[i].L, &b[i].R);
}
int end = ( << n) - ;
int len;
int lenq = ;
for (int i = ; i <= end; ++i) {
len = ;
for (int j = ; j <= n; ++j) {
if (( << (j - )) & (i)) {
slc[++len] = j;
}
}
LL mi = ;
LL mx = ;
for (int j = ; j <= len; ++j) {
mi += b[slc[j]].L;
mx += b[slc[j]].R;
if (mx < ) while();
}
++lenq;
query[lenq].L = mi;
query[lenq].R = mx;
}
sort(query + , query + + lenq);
query[lenq + ].L = query[lenq + ].R = -;
lenq++;
LL ans = ;
LL L = query[].L;
LL R = query[].R;
// for (int i = 1; i <= lenq; ++i) {
// cout << query[i].L << " " << query[i].R << endl;
// }
for (int i = ; i <= lenq; ++i) {
if (i == lenq) {
ans += R - L + ;
break;
}
if (R >= query[i].L) {
R = max(R, query[i].R);
} else {
ans += R - L + ;
R = query[i].R;
L = query[i].L;
}
}
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return ;
}

17995 Stupid thief 组合数学的更多相关文章

  1. ARC120F Wine Thief (组合数学)

    题面 有一个长为 N N N 的序列,相邻的两个数中只能选一个,总共选 k k k 个数,一种方案的价值为选的 k k k 个数的和,问所有合法方案的价值总和,答案对 998244353 取模. 1 ...

  2. codeforces 632+ E. Thief in a Shop

    E. Thief in a Shop time limit per test 5 seconds memory limit per test 512 megabytes input standard ...

  3. Codeforces632E Thief in a Shop(NTT + 快速幂)

    题目 Source http://codeforces.com/contest/632/problem/E Description A thief made his way to a shop. As ...

  4. poj 3734 Blocks 快速幂+费马小定理+组合数学

    题目链接 题意:有一排砖,可以染红蓝绿黄四种不同的颜色,要求红和绿两种颜色砖的个数都是偶数,问一共有多少种方案,结果对10007取余. 题解:刚看这道题第一感觉是组合数学,正向推了一会还没等推出来队友 ...

  5. 组合数学or not ---- n选k有重

    模板问题: 1. 取物品 (comb.pas/c/cpp) [问题描述] 现在有n个物品(有可能相同),请您编程计算从中取k个有多少种不同的取法.[输入] 输入文件有两行,第一行包含两个整数n,k(2 ...

  6. 组合数学(全排列)+DFS CSU 1563 Lexicography

    题目传送门 /* 题意:求第K个全排列 组合数学:首先,使用next_permutation 函数会超时,思路应该转变, 摘抄网上的解法如下: 假设第一位是a,不论a是什么数,axxxxxxxx一共有 ...

  7. uestc1888 Birthday Party    组合数学,乘法原理

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=25539#problem/G 题目意思: 有n个人,每个人有一个礼物,每个人能拿 ...

  8. UVA 11076 Add Again 计算对答案的贡献+组合数学

    A pair of numbers has a unique LCM but a single number can be the LCM of more than one possiblepairs ...

  9. POJ3252——Round Number(组合数学)

    Round Numbers DescriptionThe cows, as you know, have no fingers or thumbs and thus are unable to pla ...

随机推荐

  1. Java聊天室[长轮询]

    今天看到有人分享java实现的聊天室,想起很久以前还在热衷于java的时候也做过一个web聊天室,不拿出来晒晒,可能再也不为人知了,单纯是一个兴趣作品,稳定性不好,也没有考虑连接数和并发的问题,拿出来 ...

  2. codeforces B. Ciel and Flowers 解题报告

    题目链接:http://codeforces.com/problemset/problem/322/B 题目意思:给定红花.绿花和蓝花的朵数,问组成四种花束(3朵红花,3朵绿花,3朵蓝花,1朵红花+1 ...

  3. struts2中<s:if>标签的使用

    转载:http://blog.sina.com.cn/s/blog_5f9938640100v2kr.html A:<s:if>判断字符串的问题: 1.判断单个字符:<s:if te ...

  4. Java网络编程Socket通信

        TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的.可靠的.基于字节流的传输层通信协议     UDP (User Datagram Proto ...

  5. liunx环境下安装tomcat

    第一:下载tomcat的安装包

  6. 怎样编写高质量的 Java 代码

    代码质量概述 怎样辨别一个项目代码写得好还是坏?优秀的代码和腐化的代码区别在哪里?怎么让自己写的代码既漂亮又有生命力?接下来将对代码质量的问题进行一些粗略的介绍.也请有过代码质量相关经验的朋友提出宝贵 ...

  7. for循环的一个注意点

    unsigned int i =10; for(i;i > 0; i--) { xxxxx } 因为i是unsigned int 类型的,永远不可能小于0,也就是说是个死循环了.

  8. html5 滚动小球

    <html> <head> <meta charset="utf-8"/> </head> <body onkeydown=& ...

  9. storyBoard学习教程一(页面跳转)

    今天为了给伙伴作一篇storyBoard快速编程的教程,所以才写下了这篇博客. 有过storyBoard 编程经验的伙伴还是不要阅读本篇博客了,我自己认为,太基础太简单了,为了方便别人学习使用,我还是 ...

  10. Kefa and Watch

    题意: 维护一个长度为n的字符串,两种操作: 1.将 [l,r] 的字符变为 c 2.询问 d 是否为 $S(l,r)$ 的周期 解法: 首先分析如何令 [l,r] 的周期为d,利用循环串的性质得: ...