CodeForces - 125C Hobbits' Party(思维+构造)
生活随笔
收集整理的這篇文章主要介紹了
CodeForces - 125C Hobbits' Party(思维+构造)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:有一個派對,現在有 n 個人參加,題目要求我們構造出一種參加方式,滿足條件且可以維持的天數最大:
輸出最大天數并給出構造方案
題目分析:一開始我想讓相鄰兩個人互相在相鄰的天數中出現,比如
1 n
1 2
2 3
...
n-1 n
n 1
但是發現這樣的話第一個人會出現三次,與條件二不符,然后就想不出比較好的構造方案了,看了網上的題解后,雖然知道該怎么構造了,但還是不知道是如何思考的,稍微說一下吧:
假設當前的最大天數 m = 4 ,那么我們需要構造如下矩陣:
1 2 3 4
1 5 6 7
2 5 8 9
3 6 8 10?
4 7 9 10
可能看完后有點懵,下面我一步一步來解釋為什么這樣構造
首先第一行我們直接排列,以及第一列如下排列
1 2 3 4
1
2
3
4
這樣一來就滿足了第一行和其他任意一行滿足條件了,接下來我們考慮第二行
1 2 3 4
1 5 6 7
2 5
3 6
4 7
這樣第二行與其他的每一行也滿足條件了,如此構造下去,我們最終會得到一個 m * ( m - 1 ) 的矩陣,構造起來也比較簡單,沿著對角線分成兩個部分,然后分別構造就是答案了,至于這個 m ,我們用一個while循環找就可以了,因為矩陣中一共有 2 * n 個元素,只要滿足 m * ( m - 1 ) <= 2 * n,找到最大值就行了
代碼:
#include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cstring> #include<algorithm> #include<stack> #include<queue> #include<map> #include<set> #include<cmath> #include<sstream> #include<unordered_map> using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=210;int ans[N][N];int main() { // freopen("input.txt","r",stdin); // ios::sync_with_stdio(false);int n,m=0;scanf("%d",&n);while(m*(m+1)<=2*n)m++;int cnt1=0,cnt2=0;for(int i=1;i<=m-1;i++)for(int j=i;j<=m-1;j++)ans[i][j]=++cnt1;for(int j=1;j<=m-1;j++)for(int i=j+1;i<=m;i++)ans[i][j]=++cnt2;printf("%d\n",m);for(int i=1;i<=m;i++){for(int j=1;j<=m-1;j++)printf("%d ",ans[i][j]);printf("\n");}return 0; }?
總結
以上是生活随笔為你收集整理的CodeForces - 125C Hobbits' Party(思维+构造)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CodeForces - 722C De
- 下一篇: CodeForces - 1000C C