ural 1144. The Emperor's Riddle
1144. The Emperor's Riddle
Memory limit: 4 MB
Background
Problem
Input
Output
Sample
input | output |
---|---|
10 3 4 |
4 |
/**
Create By yzx - stupidboy
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define ft first
#define sd second
#define mk make_pair inline int Getint()
{
int Ret = ;
char Ch = ' ';
bool Flag = ;
while(!(Ch >= '' && Ch <= ''))
{
if(Ch == '-') Flag ^= ;
Ch = getchar();
}
while(Ch >= '' && Ch <= '')
{
Ret = Ret * + Ch - '';
Ch = getchar();
}
return Flag ? -Ret : Ret;
} const int N = , M = ;
int n, m, k, arr[N];
priority_queue<pair<int, int> > index, boxes;
int sum[M], belong[N], tag[N];
int tmp[N];
int type[][M], ranks[M], to[M]; inline void Input()
{
scanf("%d%d%d", &n, &m, &k);
for(int i = ; i <= n; i++) scanf("%d", &arr[i]);
} inline int Check()
{
for(int i = ; i <= m; i++) sum[i] = ;
for(int i = ; i <= n; i++) sum[belong[i]] += arr[i];
int mx = -INF, mn = INF;
for(int i = ; i <= m; i++)
mx = max(mx, sum[i]),
mn = min(mn, sum[i]);
return (mx - mn) - k;
} inline bool Compare(int t, int a, int b)
{
return type[t][a] < type[t][b];
} inline bool CmpUp(int a, int b)
{
return Compare(, a, b);
} inline bool CmpDown(int a, int b)
{
return Compare(, b, a);
} inline void Solve()
{
srand(time());
//sort(arr + 1, arr + 1 + n, greater<int>() );
for(int i = ; i <= m; i++) index.push(mk(-sum[i], i));
for(int i = ; i <= n; i++) boxes.push(mk(arr[i], i));
for(int i = ; i <= n; i++)
{
int u = index.top().sd, v = boxes.top().sd;
index.pop(), boxes.pop();
sum[u] += arr[v], belong[v] = u;
index.push(mk(-sum[u], u));
} int now;
while((now = Check()) > )
{
//printf("%d\n", now);
for(int i = ; i <= n; i++)
tag[i] = rand() & ; for(int t = ; t < ; t++)
for(int i = ; i <= m; i++)
type[t][i] = ;
for(int i = ; i <= n; i++)
type[tag[i]][belong[i]] += arr[i]; for(int t = ; t < ; t++)
{
for(int i = ; i <= m; i++)
ranks[i] = i;
if(t == )
sort(ranks + , ranks + + m, CmpUp);
else sort(ranks + , ranks + + m, CmpDown); for(int i = ; i <= m; i++) to[ranks[i]] = i;
for(int i = ; i <= n; i++)
if(tag[i] == t)
tmp[i] = to[belong[i]];
} for(int i = ; i <= n; i++)
belong[i] = tmp[i];
} int ans = Check() + k;
printf("%d\n", ans);
vector<int> answer[M];
for(int i = ; i <= n; i++) answer[belong[i]].pub(i);
for(int i = ; i <= m; i++)
{
int length = sz(answer[i]);
for(int j = ; j < length - ; j++)
printf("%d ", answer[i][j]);
if(length) printf("%d\n", answer[i].back());
/*int p = 0;
for(int j = 0; j < length - 1; j++)
{
printf("%d ", arr[answer[i][j]]);
p += arr[answer[i][j]];
}
if(length)
{
printf("%d ", arr[answer[i].back()]);
p += arr[answer[i].back()];
}
printf("%d\n", p);*/
}
//sfor(int i = 1; i <= m; i++) printf("%d ", sum[i]);
} int main()
{
freopen("a.in", "r", stdin);
freopen("a.out", "w", stdout);
Input();
Solve();
return ;
}
ural 1144. The Emperor's Riddle的更多相关文章
- URAL 1994 The Emperor's plan 求组合数 大数用log+exp处理
URAL 1994 The Emperor's plan 求组合数 大数用log #include<functional> #include<algorithm> #inclu ...
- URAL 1994 The Emperor's plan
期望$dp$. $dp[i][j]$表示第$1$种人有$i$个,第$2$种人有$j$个的情况下,到达目标状态的期望值.初始化$dp[i][0]=i$. 枚举一下这个状态死多少人,取个$max$,最后$ ...
- POJ 1144
http://poj.org/problem?id=1144 题意:给你一些点,某些点直接有边,并且是无向边,求有多少个点是割点 割点:就是在图中,去掉一个点,无向图会构成多个子图,这就是割点 Tar ...
- 网页闯关游戏(riddle webgame)--H5刮刮卡的原理和实践
前言: 之前编写了一个网页闯关游戏(类似Riddle Game), 除了希望大家能够体验一下我的游戏外. 也愿意分享编写这个网页游戏过程中, 学到的一些知识. 对于刮刮卡, 想必大家都很熟悉, 也很喜 ...
- 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome
题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...
- ural 2071. Juice Cocktails
2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...
- ural 2073. Log Files
2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...
- ural 2070. Interesting Numbers
2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...
- ural 2069. Hard Rock
2069. Hard Rock Time limit: 1.0 secondMemory limit: 64 MB Ilya is a frontman of the most famous rock ...
随机推荐
- 优化MyBatis配置文件中的配置
一.为实体类定义别名,简化sql映射xml文件中的引用 之前,我们在sql映射xml文件中的引用实体类时,需要写上实体类的全类名(包名+类名),如下: <!-- 创建用户(Create) --& ...
- Android Programming: Pushing the Limits -- Chapter 5: Android User Interface Operations
多屏幕 自定义View 多屏幕 @.Android 4.2 开始支持多屏幕. @.举例: public class SecondDisplayDemo extends Activity { priva ...
- 二叉树学习笔记之B树、B+树、B*树
动态查找树主要有二叉查找树(Binary Search Tree),平衡二叉查找树(Balanced Binary Search Tree), 红黑树 (Red-Black Tree ), 都是典型的 ...
- 【JAVA网络流之URL】
一.URL URL对象可以认为是URLConnection对象+Socket对象. Java.lang.Object |-Java.net.URL 常用构造方法: URL(String spec) ...
- 【Java环境变量的配置问题】
首先是JVM.JRE.JDK三者之间的关系: java的跨平台性依赖于Java虚拟机:jvm(Java Virtual Machine),而jre(Java Runtime Environment,中 ...
- ArcGIS中的三种查询
ArcGIS runtime SDK for WPF/Silverlight中的三种常用的查询:QueryTask.FindTask.IdentifyTask都是继承自ESRI.ArcGIS.Clie ...
- cordova+angularJS+ionic
1.创建项目 2.路由 angular.module("starter",['ionic']) // 依赖 ionic 提供的ui-router .config(function ...
- javascript、正则的验证
js验证.手机.固话.邮箱.身份证.网址.日期等 Verification.js文件 /* * 手机号码格式 * 只允许以13.15.18开头的号码 * 如:13012345678.159292243 ...
- 封装自己的printf函数
#include <stdio.h> #include <stdarg.h> //方式一 #define DBG_PRINT (printf("%s:%u %s:%s ...
- 安装PIL
本来直接pip install PIL的,一直说找不到对应的版本,在廖雪峰老师的博客里发现原来PIL只更新到pytyon 2.7 后来就只有Pillow了,而我的版本是2.7.9 用的时候直接from ...