Problem 1. Dance Mooves

Farmer John’s cows are showing off their new dance mooves!

At first, all NN cows (2≤N≤1052≤N≤105) stand in a line with cow ii in the iith position in line. The sequence of dance mooves is given by KK (1≤K≤2⋅1051≤K≤2⋅105) pairs of positions (a1,b1),(a2,b2),…,(aK,bK)(a1,b1),(a2,b2),…,(aK,bK). In each minute i=1…Ki=1…K of the dance, the cows in positions aiai and bibi in line swap. The same KK swaps happen again in minutes K+1…2KK+1…2K, again in minutes 2K+1…3K2K+1…3K, and so on, continuing indefinitely in a cyclic fashion. In other words,

  • In minute 11, the cows at positions a1a1 and b1b1 swap.
  • In minute 22, the cows at positions a2a2 and b2b2 swap.
  • ...
  • In minute KK, the cows in positions aKaK and bKbK swap.
  • In minute K+1K+1, the cows in positions a1a1 and b1b1 swap.
  • In minute K+2K+2, the cows in positions a2a2 and b2b2 swap.
  • and so on ...

For each cow, please determine the number of unique positions in the line she will ever occupy.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains integers NN and KK. Each of the next KK lines contains (a1,b1)…(aK,bK)(a1,b1)…(aK,bK) (1≤ai<bi≤N1≤ai<bi≤N).

OUTPUT FORMAT (print output to the terminal / stdout):

Print NN lines of output, where the iith line contains the number of unique positions that cow ii reaches.

SAMPLE INPUT:

5 4
1 3
1 2
2 3
2 4

SAMPLE OUTPUT:

4
4
3
4
1
  • Cow 11 reaches positions {1,2,3,4}{1,2,3,4}.
  • Cow 22 reaches positions {1,2,3,4}{1,2,3,4}.
  • Cow 33 reaches positions {1,2,3}{1,2,3}.
  • Cow 44 reaches positions {1,2,3,4}{1,2,3,4}.
  • Cow 55 never moves, so she never leaves position 55.

SCORING:

  • Test cases 1-5 satisfy N≤100,K≤200N≤100,K≤200.
  • Test cases 6-10 satisfy N≤2000,K≤4000N≤2000,K≤4000.
  • Test cases 11-20 satisfy no additional constraints.

解题思路

我们声称同一周期中所有奶牛的答案都是相同的。因为每进行 K 次交换,一切都会重复,如果两头奶牛在 K 次交换后一直处于同一位置,那么它们最终会到达相同的位置。 K 次交换后,牛 i 到位置 pi,所以牛 i 和牛 pi 的答案是一样的。这个逻辑扩展到循环中的每一头奶牛(奶牛 pi 的答案等于奶牛 pi 等等)。

#include <bits/stdc++.h>
using namespace std;int N,K;
int A[200001],B[200001]; //input
int P[100001]; //as described in analysis
vector<int>S[100001]; //as described in analysis
int from[100001]; //from[i] = where the cow in position i originated from
int cnt[100001]; //array to keep track of uniquePos
int uniquePos; //# of unique reachable positions//add in S_node
void add(int node){for (int x:S[node]){if (cnt[x]==0)uniquePos++;cnt[x]++;}
}//remove S_node
void remove(int node){for (int x:S[node]){if (cnt[x]==1)uniquePos--;cnt[x]--;}
}bool vis[100001];
queue<int>q; //stores all nodes in current cyclevoid dfs(int node){vis[node]=true;add(node);q.push(node);if (!vis[P[node]])dfs(P[node]);
}int main(){cin>>N>>K;for (int i=0;i<K;i++)cin>>A[i]>>B[i];//initialize from and Sfor (int i=1;i<=N;i++){from[i]=i;S[i].push_back(i);}//simulate the first K swaps, keeping track of where each position can reachfor (int i=0;i<K;i++){S[from[A[i]]].push_back(B[i]);S[from[B[i]]].push_back(A[i]);swap(from[A[i]],from[B[i]]);}//compute array P after first K swapsfor (int i=1;i<=N;i++)P[from[i]]=i;int ans[100001];//run a DFS on each cyclefor (int i=1;i<=N;i++)if (!vis[i]){dfs(i);int tempAns=uniquePos; //the answer //assign the answer for all nodes in the cycle, which we've stored in this queuewhile (!q.empty()){remove(q.front());ans[q.front()]=tempAns;q.pop();}}for (int i=1;i<=N;i++)cout<<ans[i]<<endl;return 0;
}

Problem 2. No Time to Paint

Bessie has recently received a painting set, and she wants to paint the long fence at one end of her pasture. The fence consists of NN consecutive 1-meter segments (1≤N≤1051≤N≤105). Bessie has 26 different colors available, which she labels with the letters 'A' through 'Z' in increasing order of darkness ('A' is a very light color, and 'Z' is very dark). She can therefore describe the desired color she wants to paint each fence segment as a length-NN string where each character is a letter.

Initially, all fence segments are uncolored. Bessie can color any contiguous range of segments with a single color in a single brush stroke as long as she never paints a lighter color over a darker color (she can only paint darker colors over lighter colors).

For example, an initially uncolored segment of length four can be colored as follows:

.... -> BBB. -> BBLL -> BQQL

Running short on time, Bessie thinks she may need to leave some consecutive range of fence segments unpainted! Currently, she is considering QQ candidate ranges (1≤Q≤1051≤Q≤105), each described by by two integers (a,b)(a,b) with 1≤a≤b≤N1≤a≤b≤N giving the indices of endpoints of the range a…ba…b of segments to be left unpainted.

For each candidate range, what is the minimum number of strokes needed to paint every fence segment outside those in the range with its desired color while leaving all fence segments inside the range uncolored? Note that Bessie does not actually do any painting during this process, so the answers for each candidate range are independent.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains NN and QQ.

The next line contains a string of length NN characters representing the desired color for each fence segment.

The next QQ lines each contain two space-separated integers aa and bb representing a candidate range to possibly leave unpainted.

OUTPUT FORMAT (print output to the terminal / stdout):

For each of the QQ candidates, output the answer on a new line.

SAMPLE INPUT:

8 2
ABBAABCB
3 6
1 4

SAMPLE OUTPUT:

4
3

In this example, excluding the sub-range corresponding to the desired pattern 

USACO 1月 2020-2021 January Contest Silver 题解相关推荐

  1. USACO 2020~2021 February Contest GOLD 题解(3)

    USACO 2020~2021 二月黄金组 题解(3) 3. Count The Cows As is typical, Farmer John's cows have spread themselv ...

  2. USACO 1月 2021-2022 January Contest Silver银组 题解

    你好啊我又又又来了 要准备usaco的铁铁们可以参考这个文章哦! 想刷好USACO--看这篇文章就够了_GeekAlice的博客-CSDN博客我最近是发现了一个很好用的网站https://blog.c ...

  3. USACO 1月 2021-2022 January Contest Bronze 题解

    目录 你好啊我又又又来了 要准备usaco的铁铁们可以参考这个文章哦!USACO题库 - 比Usaco Training更好用的网站_GeekAlice的博客-CSDN博客https://blog.c ...

  4. USACO 2021 January Contest, BronzeProblem 3. Just Stalling题解

    题目描述 Farmer John 有 N 头奶牛(1≤N≤20),高度为 a1-aN.他的牛栏有 N 个牛棚,高度限制分别为b1-bN(例如,如果 b5=17,那么一头高度不超过 17 的奶牛可以住在 ...

  5. USACO 2012 January Contest, Silver Division Solution

    T1是一道构图然后跑最短路的题. T2是一道裸的dp 然而我看了以后觉得爆搜是一定可以过掉的== 于是我先来一发二进制枚举,然后two points 维护答案.. T3是一道神题 首先要分类讨论,然后 ...

  6. USACO 2021 January Contest, Bronze. Problem 1. Uddered but not Herd

    题目描述 一个鲜为人知的事实是,奶牛拥有自己的文字:「牛文」.牛文由 26 个字母 'a' 到 'z' 组成,但是当奶牛说牛文时,可能与我们所熟悉的 'abcdefghijklmnopqrstuvwx ...

  7. USACO 2021 January Contest, BronzeProblem 3. Just Stalling题解 贪心 排序

    已经鸽了好久了写一篇题解把 题目描述 Farmer John 有N头奶牛(1≤N≤20),高度为a1-aN.他的牛栏有N个牛棚,高度限制分别为b1-bN(例如,如果b5=17,那么一头高度不超过17的 ...

  8. USACO 2018 January Contest

    USACO 2018 January Contest 比赛链接 T1 MooTube 题目链接 题目大意:给定一个图,两个点之间的距离是他们路径上边权的最小值.给定一个起点,求距离大于等于K的点有几个 ...

  9. USACO 2018 FEBURARY CONTEST :SILVER T1

    USACO 2018 February Contest, Silver Problem 1. Rest Stops Farmer John and his personal trainer Bessi ...

最新文章

  1. 2021年春季学期-信号与系统-第九次作业参考答案-第三小题
  2. 解决weblogic页面和控制台乱码问题
  3. BEC listen and translation exercise 26
  4. 云呼叫中心系统: 引领企业通信产业下一春
  5. 关于Arrays类中toArray方法的总结
  6. mysql outfile csv_sql-MySQL导出到outfile:CSV转义字符
  7. python变量标识符_python中的变量和标识符
  8. NoSQL架构实践(三)——以NoSQL为缓存
  9. NAT (PAT)地址转换技术(讲解+配置)
  10. 散列表删除一个元素c语言,分享一个简单高效的哈希表C语言实现
  11. iOS之UI--Quartz2D的入门应用--重绘下载圆形进度条
  12. 使用折半查找法查找数组中的元素
  13. Kettle下载Redisinput插件查询Redis数据
  14. 模糊控制(一)模糊控制简介及数学基础
  15. box-sizing:boder-box
  16. 微信小程序超级占内存_微信小程序占内存吗?
  17. php7hugepage,HugePage简介和KVM中使用HugePage
  18. Bitnami redmine 一键安装包
  19. WooCommerce API Keys的生成和保存机制
  20. tpch测试mysql_数据库系统TPC-H测试方法及结果分析

热门文章

  1. 淘宝主图视频怎么上传?怎么抓取、下载?
  2. Python核心知识框架
  3. python基础知识二
  4. FLV视频和音频解析学习(一)
  5. docker搭建mysql8.0主备(主从复制)
  6. Android滚动字幕公告字过长时横向滚动控件
  7. Go语言编程 (许式伟 等 著)
  8. 2、NC65和NC63方法集(持续更新)
  9. iOS 后台播放静音音频保证应用不会被无端杀掉
  10. pytest_assert断言