The Power Cube is used as a stash of Exotic Power. There are n cities numbered 1,2,…,n where allowed to trade it. The trading price of the Power Cube in the i-th city is ai dollars per cube. Noswal is a foxy businessman and wants to quietly make a fortune by buying and reselling Power Cubes. To avoid being discovered by the police, Noswal will go to the i-th city and choose exactly one of the following three options on the i-th day:

1. spend ai dollars to buy a Power Cube
2. resell a Power Cube and get ai dollars if he has at least one Power Cube
3. do nothing

Obviously, Noswal can own more than one Power Cubes at the same time. After going to the n
cities, he will go back home and stay away from the cops. He wants to
know the maximum profit he can earn. In the meanwhile, to lower the
risks, he wants to minimize the times of trading (include buy and sell)
to get the maximum profit. Noswal is a foxy and successful businessman
so you can assume that he has infinity money at the beginning.

 
Input
There are multiple test cases. The first line of input contains a positive integer T (T≤250), indicating the number of test cases. For each test case:
The first line has an integer n. (1≤n≤105)
The second line has n integers a1,a2,…,an where ai means the trading price (buy or sell) of the Power Cube in the i-th city. (1≤ai≤109)
It is guaranteed that the sum of all n is no more than 5×105.
 
Output
For
each case, print one line with two integers —— the maximum profit and
the minimum times of trading to get the maximum profit.
 
Sample Input
3
4
1 2 10 9
5
9 5 9 10 5
2
2 1
 
Sample Output
16 4
5 2
0 0

Hint

In the first case, he will buy in 1, 2 and resell in 3, 4. profit = - 1 - 2 + 10 + 9 = 16
In the second case, he will buy in 2 and resell in 4. profit = - 5 + 10 = 5
In the third case, he will do nothing and earn nothing. profit = 0

题意 : 有 n 个城市,在某一个城市可以进行 3 次操作,买一件物品,卖一件物品,或什么也不做,开始的时候有无限的钱,问最终最大的收益是多少?

思路分析 : 贪心 , 从第一个城市开始,手头有东西,能卖则卖,卖不掉就买,最后剩下的退掉,但是卖的地方写法很奇特,就是每卖出去一个,我们就压进队列中两个这个元素,第一次弹出队的时候表示反悔了,第二次弹出队的时候表示当时也是将此物品买了,并没有卖出,为什么是这样,纸上写写就知道了

代码示例 :

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll maxn = 1e5+5;
ll n; struct node
{
ll x, id;
bool operator< (const node &v)const{
if (x == v.x) return id < v.id;
return x > v.x;
}
};
priority_queue<node>que; int main () {
ll t;
ll x; cin >> t;
while(t--){
scanf("%lld", &n);
ll ans = 0, cnt = 0;
while(!que.empty()) que.pop();
for(ll i = 1; i <= n; i++){
scanf("%lld", &x);
if (!que.empty() && que.top().x < x) {
if (que.top().id != 2) cnt++;
//printf("===== %lld\n", que.top().id);
ans += x-que.top().x;
que.push({x, 2}); que.push({x, 1});
que.pop();
}
else {
que.push({x, 0});
}
//printf("++++ %lld %lld\n", i, cnt);
}
printf("%lld %lld\n", ans, cnt*2);
}
return 0;
}

Problem Description
There are N vertices connected by N?1 edges, each edge has its own length.
The set { 1,2,3,…,N } contains a total of N! unique permutations, let’s say the i-th permutation is Pi and Pi,j is its j-th number.
For the i-th permutation, it can be a traverse sequence of the tree with N vertices, which means we can go from the Pi,1-th vertex to the Pi,2-th vertex by the shortest path, then go to the Pi,3-th vertex ( also by the shortest path ) , and so on. Finally we’ll reach the Pi,N-th vertex, let’s define the total distance of this route as D(Pi) , so please calculate the sum of D(Pi) for all N! permutations.

Input
There are 10 test cases at most.
The first line of each test case contains one integer N ( 1≤N≤105 ) .
For the next N?1 lines, each line contains three integer X, Y and L, which means there is an edge between X-th vertex and Y-th of length L ( 1≤X,Y≤N,1≤L≤109 ) .

Output
For each test case, print the answer module 109+7 in one line.

Sample Input

3
1 2 1
2 3 1
3
1 2 1
1 3 2

Sample Output

16
24

题意 : 初始状态下给你一棵树,同时给你边权上的值,此时整棵树的值为所有边权的累加,然后让你求 n! 下树的边权的累加的和。

思路分析 :加上 n! 这个限制条件,题目差不多就转换成初始状态下任意点对的距离和,很像,但不完全是

    在这个问题中可以发现的任意一个点对在 n! 中出现的次数是 2*(n-1)! 的,任意打一个表就可以看出,然后在某一个图下,我是计算的时候是枚举任意一条边的,出现的次数等于边左边的点乘以边右边的点,此时在乘以 2*(n-1)! 即可

代码示例 :

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll maxn = 1e5+5;
const ll mod = 1e9+7; ll n;
ll pp[maxn];
void init() {
pp[0] = 1;
for(ll i = 1; i <= 100000; i++){
pp[i] = pp[i-1]*i%mod;
}
}
struct node
{
ll to, cost;
};
vector<node>ve[maxn];
ll size[maxn];
ll ans; void dfs(ll x, ll fa, ll w){
for(ll i = 0; i < ve[x].size(); i++){
ll to = ve[x][i].to;
if (to == fa) continue;
dfs(to, x, ve[x][i].cost);
size[x] += size[to];
}
size[x] += 1; ans += size[x]*(n-size[x])%mod*w%mod*2%mod*pp[n-1]%mod;
ans %= mod;
} int main () {
ll u, v, w;
init(); while(~scanf("%lld", &n)){ for(int i = 1; i <= 100000; i++) ve[i].clear(); for(ll i = 1; i < n; i++){
scanf("%lld%lld%lld", &u, &v, &w);
ve[u].push_back({v, w});
ve[v].push_back({u, w});
}
memset(size, 0, sizeof(size));
ans = 0;
dfs(1, 0, 0);
printf("%lld\n", ans);
}
return 0;
}

YJJ's Salesman
Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1369    Accepted Submission(s): 483

Problem Description
YJJ is a salesman who has traveled through western country. YJJ is always on journey. Either is he at the destination, or on the way to destination.
One day, he is going to travel from city A to southeastern city B. Let us assume that A is (0,0) on the rectangle map and B (109,109). YJJ is so busy so he never turn back or go twice the same way, he will only move to east, south or southeast, which means, if YJJ is at (x,y) now (0≤x≤109,0≤y≤109), he will only forward to (x+1,y), (x,y+1) or (x+1,y+1).
On the rectangle map from (0,0) to (109,109), there are several villages scattering on the map. Villagers will do business deals with salesmen from northwestern, but not northern or western. In mathematical language, this means when there is a village k on (xk,yk) (1≤xk≤109,1≤yk≤109), only the one who was from (xk?1,yk?1) to (xk,yk) will be able to earn vk dollars.(YJJ may get different number of dollars from different village.)
YJJ has no time to plan the path, can you help him to find maximum of dollars YJJ can get.

Input
The first line of the input contains an integer T (1≤T≤10),which is the number of test cases.

In each case, the first line of the input contains an integer N (1≤N≤105).The following N lines, the k-th line contains 3 integers, xk,yk,vk (0≤vk≤103), which indicate that there is a village on (xk,yk) and he can get vk dollars in that village.
The positions of each village is distinct.

Output
The maximum of dollars YJJ can get.

Sample Input

1
3
1 1 1
1 2 2
3 3 1

Sample Output

3

题意 : 在一张 10^9 * 10^9 的图中有 1e5 个点,规定只能向右走,向下走,向右下走,只有当往右下走的时候进入格点才会获得此中的分数,问最终最大的得分是多少?

思路分析 : 首先离散一下,将图变成 1e5*1e5 的图,我们一列一列的去维护,对于某一列中的某个点,如果想要得分,一定是他前一列中他所在位置上方的点转移下来的,就按照这个思路写就可以了

代码示例:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll maxn = 1e5+5;
#define lson k<<1
#define rson k<<1|1 ll n;
ll x[maxn], y[maxn];
struct pp
{
ll u, v, w; bool operator< (const pp &q)const{
if (u == q.u) return v < q.v;
return u < q.u;
}
}arr[maxn]; struct node
{
ll l, r;
ll mm;
}t[maxn<<2]; void build(ll l, ll r, ll k){
t[k].l = l, t[k].r = r; t[k].mm = 0;
if (l == r) return;
ll mid = (l+r)>>1;
build(l, mid, lson);
build(mid+1, r, rson);
} ll query(ll l, ll r, ll k){
if (l <= t[k].l && t[k].r <= r){
return t[k].mm;
}
ll mid = (t[k].l+t[k].r)>>1;
ll res = 0;
if (l <= mid) res = max(res, query(l, r, lson));
if (r > mid) res = max(res, query(l, r, rson));
return res;
} void update(ll pos, ll val, ll k){
if (t[k].l == t[k].r) {
t[k].mm = max(t[k].mm, val);
return;
}
ll mid = (t[k].l+t[k].r)>>1;
if (pos <= mid) update(pos, val, lson);
else update(pos, val, rson);
t[k].mm = max(t[lson].mm, t[rson].mm);
} int main () {
ll T; cin >> T;
while(T--){
scanf("%lld", &n);
ll w;
for(ll i = 1; i <= n; i++){
scanf("%lld%lld%lld", &x[i], &y[i], &w);
arr[i] = {x[i], y[i], w};
}
sort(x+1, x+1+n); sort(y+1, y+1+n);
ll ss = unique(x+1, x+1+n)-x;
ll ss2 = unique(y+1, y+1+n)-y;
for(ll i = 1; i <= n; i++){
arr[i].u = lower_bound(x+1, x+ss, arr[i].u)-x;
arr[i].v = lower_bound(y+1, y+ss2, arr[i].v)-y;
}
sort(arr+1, arr+1+n);
// for(ll i = 1; i <= n; i++) {
// prllf("+++ %d %d %d\n", arr[i].u, arr[i].v, arr[i].w);
// }
build(1, n, 1);
for(ll i = 1; i <= n; i++){
ll p = i+1;
while(arr[p].u == arr[i].u) p++;
for(ll j = p-1; j >= i; j--){
ll f = query(1, arr[j].v-1, 1);
update(arr[j].v, arr[j].w+f, 1);
}
i = p-1;
}
printf("%lld\n", t[1].mm);
} return 0;
}

2018 CCPC 网络赛的更多相关文章

  1. 2018 CCPC网络赛

    2018 CCPC网络赛 Buy and Resell 题目描述:有一种物品,在\(n\)个地点的价格为\(a_i\),现在一次经过这\(n\)个地点,在每个地点可以买一个这样的物品,也可以卖出一个物 ...

  2. 2018 CCPC网络赛 几道数学题

    1002 Congruence equation 题目链接  : http://acm.hdu.edu.cn/showproblem.php?pid=6439 题解 : https://www.zyb ...

  3. 2018 CCPC网络赛 hdu6444 Neko's loop

    题目描述: Neko has a loop of size n.The loop has a happy value ai on the i−th(0≤i≤n−1) grid. Neko likes ...

  4. 2018 CCPC 网络赛 Buy and Resell

    The Power Cube is used as a stash of Exotic Power. There are n cities numbered 1,2,…,n where allowed ...

  5. 2018 CCPC网络赛 1010 hdu 6447 ( 树状数组优化dp)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447 思路:很容易推得dp转移公式:dp[i][j] = max(dp[i][j-1],dp[i-1][j ...

  6. 【2018 CCPC网络赛 1004】Find Integer(勾股数+费马大定理)

    Problem Description people in USSS love math very much, and there is a famous math problem . give yo ...

  7. 【2018 CCPC网络赛】1001 - 优先队列&贪心

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6438 获得最大的利润,将元素依次入栈,期中只要碰到比队顶元素大的,就吧队顶元素卖出去,答案加上他们期中 ...

  8. 【2018 CCPC网络赛】1009 - 树

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6446 题目给出的数据为一棵树,dfs扫描每条边,假设去掉某条边,则左边 x 个点,右边 n-x 个点, ...

  9. 【2018 CCPC网络赛】1003 - 费马小定理

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6440 这题主要是理解题意: 题意:定义一个加法和乘法,使得 (m+n)p = mp+np; 其中给定 ...

  10. 【2018 CCPC网络赛】1004 - 费马大定理&数学

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6441 Knowledge Point: 1. 费马大定理:当整数n >2时,关于x, y, z的 ...

随机推荐

  1. codeforce 378 div 2 F —— Drivers Dissatisfaction (最小生成树,LCA,倍增)

    官方题解: If you choose any n - 1 roads then price of reducing overall dissatisfaction is equal to min(c ...

  2. Linux创建用户、设置密码、修改用户、删除用户命令

    与大家分享下Linux系统中创建用户.设置密码.修改用户.删除用户的命令,希望对你有所帮助. useradd testuser  创建用户testuserpasswd testuser  给已创建的用 ...

  3. H3C 配置路由器作为FTP服务器端

  4. 最短路算法(floyed+Dijkstra+bellman-ford+SPFA)

    最短路算法简单模板 一.floyed算法 首先对于floyed算法来说就是最短路径的动态规划解法,时间复杂度为O(n^3) 适用于图中所有点与点之间的最短路径的算法,一般适用于点n较小的情况. Flo ...

  5. BGP团体属性的应用案例

    XRV1 ===================================================================== version 15.5service times ...

  6. 超简单!pytorch入门教程(五):训练和测试CNN

    我们按照超简单!pytorch入门教程(四):准备图片数据集准备好了图片数据以后,就来训练一下识别这10类图片的cnn神经网络吧. 按照超简单!pytorch入门教程(三):构造一个小型CNN构建好一 ...

  7. Redis安装(单机及各类集群,阿里云)

    Redis安装(单机及各类集群,阿里云) 前言 上周,我朋友突然悄悄咪咪地指着手机上的一篇博客说,这是你的博客吧.我看了一眼,是之前发布的<Rabbit安装(单机及集群,阿里云>.我朋友很 ...

  8. 基于 WebSocket 的聊天和大文件上传(有进度提示)完美实现

    大家好,好久没有写文章了,当然不是不想写,主要是工作太忙,公司有没有网络环境,不让上网,所以写的就少了.今天是2019年的最后一天,明天就要开始新的一年,当然也希望自己有一个新的开始.在2019年的最 ...

  9. k8s集群———etcd-ssl自签名证书

    etcd集群master节点安装 ,自签名SSL证书 ##安装工具cfssl $ cat cfssl.sh curl -L https://pkg.cfssl.org/R1.2/cfssl_linux ...

  10. 洛谷$P4177\ [CEOI2008]\ order$ 网络流

    正解:网络流 解题报告: 传送门$QwQ$ 开始看感$jio$长得好像和太空飞行计划差不多的,,,然后仔细康康发现还有租操作,,, 按一般的套路碰到这样儿的一般就先按非特殊化的建图然后考虑怎么实现这个 ...