Codeforces 861 B Which floor? 思维
生活随笔
收集整理的這篇文章主要介紹了
Codeforces 861 B Which floor? 思维
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:?http://codeforces.com/contest/861/problem/B
題目描述: 每個樓梯上有相同數量的數, 都是從1開始, 給你m個不矛盾的信息, 問你能不能判斷n在第幾層
解題思路: 這道題我看到的時候覺得只要能算出來一層有多少個數就可以了, 就是暴力層數, 就可以了, 復雜度為O(n^2), 但是要注意的情況就是有的時候就算不知道確切的層數, 我也可以判斷n在幾層, 所以我的想法錯了.....改進就是將所有可能的答案推進vector中, 最后看看可能答案是不是一個數就可以了
代碼:?
#include <iostream> #include <cstdio> #include <map> #include <iterator> #include <string> #include <algorithm> #include <vector> #include <cmath> using namespace std;typedef long long ll;const int maxn = 108; int a[maxn]; int b[maxn]; vector<int> ans; int main() {int n, m;cin >> n >> m;for( int i = 1; i <= m; i++ ) {cin >> a[i] >> b[i];}for( int e = 1; e <= 100; e++ ) {int flag = 1;for( int i = 1; i <= m; i++ ) {int floor = a[i]/e + (a[i]%e!=0);if( floor != b[i] ) {flag = 0;break;}}if( flag ) {ans.push_back(e);}}int flag = 1;vector<int>::iterator it = ans.begin();int res = n/(*it) + (n%(*it)!=0);for( it = ans.begin(); it != ans.end(); it++ ) {if( (n/(*it) + (n%(*it)!=0)) != res ) {flag = 0;break;}}if( flag ) {cout << res << endl;}else {cout << -1 << endl;}return 0; } View Code思考: 在轉化等價條件的時候必須嚴格證明其正確性
轉載于:https://www.cnblogs.com/FriskyPuppy/p/7614705.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的Codeforces 861 B Which floor? 思维的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Pandas基础复习-DataFrame
- 下一篇: iOS 高效的分页加载